AutoHotkey Community

It is currently May 27th, 2012, 6:11 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: BinaryToString UTF-16
PostPosted: November 4th, 2010, 3:11 am 
Can you let me know how to convert UTF-16(LE) encoded hexadecimal value to UTF-16(LE) string? (LE : little endian)

This is exactly same as {U+nnnn} in Autohotkey_L or 'BinaryToString("0xnnnn",2)' function in AutoIt.
(nnnn is hexadecimal value which is encoded as UTF-16 little endian)

But, there's only "SendInput" function in Autohotkey_L.
I don't want to send string to anywhere,
and I prefer to use Autohotkey basic(first) or Autohotkey_L(second), not AutoIt, if possible.

Actually, I'm gonna get a filename from Windows 7 registry.
But, this filename is written as REG_BINARY type
and encoded as UTF-16 little endian that Windows uses.

After getting a filename, I'll use this filename as following:

//---
RegRead, filename_hex, HKEY_CURRENT_USER, ...
filename := BinaryToUTF16String(filename_hex) ------ (0)

loop, read, %filename% ------ (1)
...
fileappend, %text%, %filename% ------ (2)
...
---//

All I need is function 'BinaryToUTF16String' of part '(0)'.
Example of filename_hex: nnnnmmmmoooopppp....
Each four hexadecimal digits (nnnn, mmmm, oooo, pppp, ...) means one UTF-16 character seperately.
A length of filename_hex is four times of length of filename.

I've already seen this link
http://www.autohotkey.com/forum/topic58 ... t=hextobin
and tried 'Hex2Bin' and 'Utf8ToAnsi' function and 'StrGet' of Autohotkey_L but failed.

I think this is not a difficult problem,
but I'm just a beginner so I beg your help.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2010, 6:34 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
What you need is HexToBin. This works on AHK Basic:
Code:
; ========= Example of usage ============

Src = 480065006C006C006F002C00200077006F0072006C0064002100

VarSetCapacity(Dest, StrLen(Src) // 2 + 2, 0)
HexToBin(Src, Dest)
DllCall("MessageBoxW", "uint", 0, "uint", &Dest, "uint", 0, "uint", 0)


; =========== Function ==================

HexToBin(ByRef Hex, ByRef Bin)
{
    HexCode =
    (LTrim Join
        568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0
        e104880f8a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a1647
        4684d275cd5b5f5ec3
    )
    VarSetCapacity(BinCode, StrLen(HexCode) // 2)
    Loop % StrLen(HexCode) // 2
        NumPut("0x" . SubStr(HexCode, 2 * A_Index - 1, 2)
                        , BinCode, A_Index - 1, "Char")
    DllCall(&BinCode, "UInt", &Bin, "UInt", &Hex, "CDecl")
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2010, 7:37 am 
I really appreciate your reply.

As I said, I've already tried Laszlo's 'HexToBin' function you introduced in another post 'Hexadecimal UTF-8 coding'.
But I couldn't check a proper result because I used 'MsgBox' embedded in AHK basic.
Now I got a proper result 'Hello, world!' through 'MessageBoxW' as you replied.

One more thing,
As I said, my goal was to use this filename in my AHK basic script like example (1) or (2) of my post.
Thank to your reply, I realized UTF-16 encoded string should be converted to ansi string to be used in AHK basic script.
So I used 'Unicode2Ansi' function of a post titled 'Helper script to convert from one to another codepages' (http://www.autohotkey.com/forum/topic17343.html),
and got my goal as follows.

//-----
RegRead, filename_hex, HKEY_CURRENT_USER, ...

VarSetCapacity(filename_bin, StrLen(filename_hex) // 2 + 2, 0)
HexToBin(filename_hex, filename_bin)
;DllCall("MessageBoxW", "uint", 0, "uint", &filename_bin, "uint", 0, "uint", 0)
Unicode2Ansi(filename_bin, filename_ansi)

loop, read, %filename_ansi%
...
fileappend, %text%, %filename_ansi%
...

-----//

Thank you.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, HotkeyStick, Wicked and 56 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group