javascript - Converting text to hexadecimal on mac -
i trying convert text hexadecimal characters in order build dmg on mac.
i'm getting burned fact hexadecimals not seem refer same characters on mac , windows ascii characters > 127. , seems basic javascript functions give "windows" translation.
need "mac" translation hexadecimal...
i'm doing far:
const filedata = await parsejson(readfile(item.file, "utf-8")) const buttonsstr = labeltohex(filedata.lang) function labeltohex(label: string) { return hexencode(label).tostring().touppercase() } function hexencode(str: string) { let let result = "" (i = 0; < str.length; i++) { result += unicodetohex(str.charcodeat(i)) } return result } function unicodetohex(unicode: number) { const hex = unicode.tostring(16) return ("0" + hex).slice(-2) }
if pass in: français éàè
i get: 46 72 61 6e e7 61 69 73 e9 e0 e8
but when read back, get: franÀais ȇË
i'm expecting get: 46 72 61 6e 8d 61 69 73 8e 88 8f
so reading gives: 46 72 61 6e e7 61 69 73 e9 e0 e8
this corresponds sheets:
https://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-mac.htm https://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-pc.htm
nonetheless, not able find either npm package translate hex based on os or obscure js function still haven't stumbled upon?
i'm running out of ideas , do:
function unicodetohex(unicode: number) { if (unicode < 128) { const hex = unicode.tostring(16) return ("0" + hex).slice(-2) } if (unicode === 233) { return "8e" }//é if (unicode === 224) { return "88" }//à if (unicode === 232) { return "8f" }//è return "3f" //? }
but avoid that...
i've found way it, code pages, stated @keith
const cptable = require("codepage") function hexencode(str: string, lang: string, langwithregion: string) { let code let hex let const maccodepages = getmaccodepage(lang, langwithregion) let result = "" (i = 0; < str.length; i++) { try { code = getmaccharcode(str, i, maccodepages) if (code === undefined) { hex = "3f" //? } else { hex = code.tostring(16) } result += hex } catch (e) { debug("there problem while trying convert char hex: " + e) result += "3f" //? } } return result } function getmaccodepage(lang: string, langwithregion: string) { switch (lang) { case "ja": //japanese return [10001] //apple japanese case "zh": //chinese if (langwithregion === "zh_cn") { return [10008] //apple simplified chinese (gb 2312) } return [10002] //apple traditional chinese (big5) case "ko": //korean return [10003] //apple korean case "ar": //arabic case "ur": //urdu return [10004] //apple arabic case "he": //hebrew return [10005] //apple hebrew case "el": //greek case "elc": //greek return [10006] //apple greek case "ru": //russian case "be": //belarussian case "sr": //serbian case "bg": //bulgarian case "uz": //uzbek return [10007] //apple macintosh cyrillic case "ro": //romanian return [10010] //apple romanian case "uk": //ukrainian return [10017] //apple ukrainian case "th": //thai return [10021] //apple thai case "et": //estonian case "lt": //lithuanian case "lv": //latvian case "pl": //polish case "hu": //hungarian case "cs": //czech case "sk": //slovak return [10029] //apple macintosh central europe case "is": //icelandic case "fo": //faroese return [10079] //apple icelandic case "tr": //turkish return [10081] //apple turkish case "hr": //croatian case "sl": //slovenian return [10082] //apple croatian default: return [10000] //apple macintosh roman } } function getmaccharcode(str: string, i: number, maccodepages: any) { let code = str.charcodeat(i) let j if (code < 128) { code = str.charcodeat(i) } else if (code < 256) { //codepage 10000 = mac os roman code = cptable[10000].enc[str[i]] } else { (j = 0; j < maccodepages.length; j++) { code = cptable[maccodepages[j]].enc[str[i]] if (code !== undefined) { break } } } return code }
Comments
Post a Comment