[SOLVED] newLISP unicode problem

Discuss other programming languages besides AutoHotkey
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

[SOLVED] newLISP unicode problem

27 Oct 2015, 05:47

newLISP: http://www.newlisp.org/

I just found this incredible scripting language. But I'm having problems dealing with unicode.

Code: Select all

(import "user32.dll" "MessageBoxW")
(MessageBoxW 0 "text" "title" 0)
It displays incorrect strings:
Image

If I use (MessageBoxW 0 (unicode "content") (unicode "title") 0), the result turns a little better, that the first character has displayed:
Image

Anyone can help, please! Thanks in advance.

OS Version: win7 x64, Chinese language
newLISP Version: newLISP v.10.6.2 32-bit on Win32 IPv4/6 UTF-8 libffi
Last edited by tmplinshi on 28 Oct 2015, 04:49, edited 1 time in total.
lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: newLISP unicode problem

28 Oct 2015, 03:10

I haven't used newLISP, but was interested in what difference there is to LISP and how they handle FFI, so I've had a look.

I think you've misunderstood what the unicode function does:
Converts ASCII/UTF-8 character strings in str to UCS-4–encoded Unicode of 4-byte integers per character.
It is UTF-32, not UTF-16. So "c" is 0x63 00 00 00 instead of 0x63 00.

I don't see a function for UTF-16, but I suppose you can call MultiByteToWideChar.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: newLISP unicode problem

28 Oct 2015, 04:45

Thanks lexikos! The MultiByteToWideChar works :). I found below code from newLISP forum:

utf8_to_utf16.lsp

Code: Select all

; utf8_to_utf16.lsp
; http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=9&t=3656&p=18353#p18353

(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)
)
test.lsp

Code: Select all

; test.lsp (Save this file with UTF-8 encoding)
(module "utf8_to_utf16.lsp")
(import "user32.dll" "MessageBoxW")
(MessageBoxW 0 (utf8->16 "中文") (utf8->16 "title") 0)
(exit)
Put the utf8_to_utf16.lsp into <path of newlisp.exe>\modules\ folder
Then run with newlisp.exe test.lsp.:superhappy:

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 4 guests