2017-09-13 10 views
0

Mac에서 dmg를 작성하려면 텍스트를 16 진수로 변환하려고합니다.
나는 hexadecalals가 127보다 큰 ascii 문자들에 대해 mac과 windows에서 같은 문자를 참조하지 않는 것처럼 보입니다. 그리고 기본 javascript 함수는 "windows"번역본만을 제공합니다.
나 16 진수로 "맥"번역 ... 내가 지금까지하고 있어요Mac에서 텍스트를 16 진수로 변환

필요합니다

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 i 
    let result = "" 

    for (i = 0; i < str.length; i++) { 
    result += unicodeToHex(str.charCodeAt(i)) 
    } 

    return result 
} 

function unicodeToHex(unicode: number) { 
    const hex = unicode.toString(16) 
    return ("0" + hex).slice(-2) 
} 

내가 전달하는 경우 : 프랑스어 EAE
내가 얻을 : (46) (72) (61) (e) E7 61 69 73 E9 E0의 E8
하지만 난 다시 읽을 때, 내가 얻을 : FranÀais È ‡ Ë
내가 기대하고있어 g 동부 : 46 72 61 6E 8D 61 69 73 8E 88 8F
그래서 다시 읽는 준다 : 46 72 61 6E (E7) 61 69 73 E9 E0의 E8이 그 장에 대응

:
https://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-mac.htm https://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-pc.htm

그럼에도 불구하고 OS를 기반으로하는 16 진수로 변환되는 npm 패키지를 찾지 못했습니다. 또는 여전히 불안한 js 기능을 찾을 수 없었습니다.
나는 아이디어가 부족하고있어 막이해야 할 다음에

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" //? 
} 

하지만 난 정말하지 않도록하고 싶습니다 ...

+0

charCodeAt는 유니 코드 값을 반환합니다. 이는 1 바이트 트리밍과 관련이 있습니다. 노드에 문자열의 16 진수 값만 원한다면. 이것을 시도해보십시오 ->'새로운 버퍼 ('Français éàè'). toString ('hex')'= '4672616ec3a761697320c3a9c3a0c3a8', 즉 16 바이트입니다. 11 문자열입니다. – Keith

+0

그것은 논리적 인 것처럼 보이지만 여전히 내 dmg에서는 작동하지 않습니다 .. 나는 éàè 대신에 쓰레기를 얻습니다 : √ß√âß ß®_ 나는 그것이 dmg가 그것을 알지 못하기 때문에 그것이라고 생각하게 만듭니다 .8 ? – ether

+1

'dmg는 utf8을 모릅니다'매우 가능 .. .. 그렇다면 utf8을 선택한 코드 페이지로 변환하는 도구가 있습니다 .. dmg 파일의 코드 페이지를 찾을 수 있다면 -> https://www.npmjs.com/package/codepage – Keith

답변

0

내가 할 수있는 방법을 발견했습니다, 감사 @Keith에 명시된대로 코드 페이지

const cptable = require("codepage") 
function hexEncode(str: string, lang: string, langWithRegion: string) { 
    let code 
    let hex 
    let i 
    const macCodePages = getMacCodePage(lang, langWithRegion) 
    let result = "" 

    for (i = 0; i < 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 was a problem while trying to convert a char to 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 { 
    for (j = 0; j < macCodePages.length; j++) { 
     code = cptable[macCodePages[j]].enc[str[i]] 
     if (code !== undefined) { 
     break 
     } 
    } 
    } 

    return code 
}