Format multiple characters by char code? Topic is solved
-
- Posts: 469
- Joined: 03 Dec 2018, 20:02
Format multiple characters by char code?
I read in the docs that Format("{:c}", charcode) can be used to infer a single character by its character code. What is the correct way to get multiple characters from a string of character codes like "\u4f60\u597d" that is common in Javascript? Do I have to use RegExReplace() for this?
Re: Format multiple characters by char code?
Hallo,
yes, if the solution is something like this: it will run easiest with RegEx.
yes, if the solution is something like this:
Code: Select all
q::MsgBox,% Chr(0x4f60) Chr(0x597d)
Re: Format multiple characters by char code?
Code: Select all
Send % decode("\u00a9\u2191") ; ©↑
Send % decode("\u4f60\u597d") ; 你好
decode(uCode) { ; Decode JavaScript Unicode
Return RegExReplace(uCode, "i)\\u([0-9A-F]{1,6})", "{U+$1}")
}
-
- Posts: 469
- Joined: 03 Dec 2018, 20:02
Re: Format multiple characters by char code?
Thanks y'all for the pointers. I repurposed the Deref() function from the docs to decode the char code string.
Code: Select all
msgbox % decode_utf8("\u4f60\u597d") ; 你好
decode_utf8(charcode_string) {
spo := 1
out := ""
while (fpo:=RegexMatch(charcode_string, "i)\\u([0-9A-F]{1,6})", m, spo))
{
out .= SubStr(charcode_string, spo, fpo-spo)
spo := fpo + StrLen(m)
if (m1)
out .= Chr("0x" m1)
}
return out SubStr(charcode_string, spo)
}
Re: Format multiple characters by char code? Topic is solved
Shorter:or:
Code: Select all
msgbox % decode_utf8("\u4f60\u597d") ; 你好
decode_utf8(charcode_string) {
Global Out := ""
Return, RegExReplace(charcode_string
, "i)(\\u)([0-9A-F]{1,6})(?COut)") Out
} Out(m) {
Global Out .= Chr("0x" m2)
}
Code: Select all
MsgBox,% (Out:="") RegExReplace("\u4f60\u597d", "i)(\\u)([0-9A-F]{1,6})(?COut)")
; 你好
Out(m) {
Global Out .= Chr("0x" m2)
}
-
- Posts: 469
- Joined: 03 Dec 2018, 20:02
Re: Format multiple characters by char code?
@Rohwedder Thanks. Apologies for the late reply, I didn't check the thread after finding the solution.
Also recently I've discovered that RegEx is faster than iterating the string. I thought it was the opposite
Also recently I've discovered that RegEx is faster than iterating the string. I thought it was the opposite