在mac

时间:2017-09-13 19:28:53

标签: javascript node.js macos encoding hex

我正在尝试将文本转换为十六进制字符,以便在mac上构建dmg 由于十六进制似乎没有在mac和windows上引用ascii字符的相同字符,因此我被烧毁了。 127.似乎基本的javascript函数只给出了“windows”翻译 我需要将“mac”翻译为十六进制......

到目前为止,我正在这样做:

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)
}

如果我传入:Françaiséàè
我明白了: 46 72 61 6e e7 61 69 73 e9 e0 e8
但当我读回来时,我得到了:FranÀaisÈ‡Ë 我期待得到: 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

尽管如此,我还是找不到基于操作系统转换为十六进制的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" //?
}

但我真的想避免这样......

1 个答案:

答案 0 :(得分: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
}