coffeeScript native转ascii

pad = (num, len)->
  numStr = num + ''
  while numStr.length < len
    numStr = '0' + numStr
  numStr

native2ascii = (str)->
  chars = ''
  for s in str
    code = s.charCodeAt()
    if code < 128
      chars += s
    else
      chars += '\\u' + pad code.toString(16)
  chars

你可能感兴趣的:(coffeeScript native转ascii)