[SOLVED] newLISP unicode problem

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: [SOLVED] newLISP unicode problem

Re: newLISP unicode problem

Post by tmplinshi » 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:

Re: newLISP unicode problem

Post by lexikos » 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.

[SOLVED] newLISP unicode problem

Post by tmplinshi » 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

Top