Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active October 28, 2015 09:56
Show Gist options
  • Save tmplinshi/48196e26f0864003ed42 to your computer and use it in GitHub Desktop.
Save tmplinshi/48196e26f0864003ed42 to your computer and use it in GitHub Desktop.
; utf8_to_utf16.lsp
; http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=9&t=3656&p=18353#p18353
;
; Example:
; (import "user32.dll" "MessageBoxW")
; (MessageBoxW 0 (utf8->16 "中文") (utf8->16 "title") 0)
(constant 'SIZEOF_WCHAR 2) ; assumption
(constant 'CP_UTF8 65001)
(define (utf8->16 lpMultiByteStr , cchWideChar lpWideCharStr ret)
(if (not MultiByteToWideChar)
(begin
(import "kernel32.DLL" "MultiByteToWideChar")
)
)
; calculate the size of buffer (in WCHAR's)
(setq cchWideChar
(
MultiByteToWideChar
CP_UTF8 ; from UTF-8
0 ; no flags necessary
lpMultiByteStr
-1 ; convert until NULL is encountered
0
0
)
)
; allocate the buffer
(setq lpWideCharStr (dup " " (* cchWideChar SIZEOF_WCHAR)))
; convert
(setq ret
(
MultiByteToWideChar
CP_UTF8 ; from UTF-8
0 ; no flags necessary
lpMultiByteStr
-1 ; convert until NULL is encountered
lpWideCharStr
cchWideChar
)
)
(if (> ret 0) lpWideCharStr nil)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment