Nodejs编码和16进制hex
编码主要都是跟着buffer走的
- http://nodejs.cn/api/buffer.html
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
- buffer可以认为是typedarray的Uint8Array的一种实现, 对应于c语言的uint8_t
- 不要new, 哈哈, 正合吾意.
- 用这三个函数:
Buffer.from()
、Buffer.alloc()
、和Buffer.allocUnsafe()
编码:
'ascii'
- 仅支持 7 位 ASCII 数据。如果设置去掉高位的话,这种编码是非常快的。'utf8'
- 多字节编码的 Unicode 字符。许多网页和其他文档格式都使用 UTF-8 。'utf16le'
- 2 或 4 个字节,小字节序编码的 Unicode 字符。支持代理对(U+10000 至 U+10FFFF)。'ucs2'
-'utf16le'
的别名。'base64'
- Base64 编码。当从字符串创建Buffer
时,按照 RFC4648 第 5 章的规定,这种编码也将正确地接受“URL 与文件名安全字母表”。'latin1'
- 一种把Buffer
编码成一字节编码的字符串的方式(由 IANA 定义在 RFC1345 第 63 页,用作 Latin-1 补充块与 C0/C1 控制码)。'binary'
-'latin1'
的别名。'hex'
- 将每个字节编码为两个十六进制字符。
如果一个文件我们不知道编码, 那么就要尝试最后的三种编码
16进制hex编码显示
const s="测试"
//charCodeAt可以
log(s.charCodeAt(0).toString(16))
//codePointAt也可以
for(var y=0; y<s.length; y++){
log(s.codePointAt(y).toString(16))
}
//貌似用正则更加简单明了.
function ASCIIToHexa (str) {
if (typeof str !== 'string') str += '';
return str.replace(/\w/g , (text) => text.charCodeAt().toString(16));
}
// int to hex
let yourNumber=100
hexString = yourNumber.toString(16); //int to hex string
yourNumber = parseInt(hexString, 16); // hex string back to int, reverse the process
log(hexString + " ====== " + yourNumber);
// hex to ascII
var input = '32343630';
var output = new Buffer(input, 'hex');
log(input + " -> " + output); // Result: 32343630 -> 2460
//或者下面用正则
var asciiVal = "32343630".match(/.{1,2}/g).map(function(v){
return String.fromCharCode(parseInt(v, 16));
}).join('');
log(asciiVal);
//一堆%的中文转码的处理, 仅仅处理了百分号
let ss=encodeURIComponent('中文编码').split('%').join('');
log(ss)
中文处理 iconv-lite
require('fs').createReadStream('4season.mp3').setEncoding('binary')
console.log('Line from file:', iconv.decode(line, 'gbk'))
var lineReader = require('readline').createInterface({
var iconv = require('iconv-lite');
// Convert from an encoded buffer to js string.
str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
// Convert from js string to an encoded buffer.
buf = iconv.encode("Sample input string", 'win1251');
// Check if encoding is supported
iconv.encodingExists("us-ascii")
先新建一个binary的buffer再去处理是否能有更好的结果?
参考
- https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings
16进制的直接表示:
- A decimal integer literal consists of a sequence of digits without a leading 0 (zero).
- A leading 0 (zero) on an integer literal, or a leading 0o (or 0O) indicates it is in octal. Octal integers can include only the digits 0-7.
- A leading 0x (or 0X) indicates a hexadecimal integer literal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. (The case of a character does not change it’s value, e.g. 0xa = 0xA = 10 and 0xf = 0xF = 15.)
- A leading 0b (or 0B) indicates a binary integer literal. Binary integers can only include the digits 0 and 1.
0, 117 and -345 (decimal, base 10)
015, 0001 and -0o77 (octal, base 8)
0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16)
0b11, 0b0011 and -0b11 (binary, base 2)
按位运算
很多时候, 这个很有用, 因为傻乎乎的直接计算总是显得很愚蠢, 实际上也确实很愚蠢. mdn上面的内容真的很棒, 直接去看就好了.
- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
>> 右移, << 左移. 适合做拼字节的运算.
|按位或, &按位与. 适合做掩码.
从int到char
String.fromCharCode(97) //这个能得到'a'
"ABC".charCodeAt(0) //这个是逆函数
{
"31": "", "32": " ", "33": "!", "34": "\"", "35": "#",
"36": "$", "37": "%", "38": "&", "39": "'", "40": "(",
"41": ")", "42": "*", "43": "+", "44": ",", "45": "-",
"46": ".", "47": "/", "48": "0", "49": "1", "50": "2",
"51": "3", "52": "4", "53": "5", "54": "6", "55": "7",
"56": "8", "57": "9", "58": ":", "59": ";", "60": "<",
"61": "=", "62": ">", "63": "?", "64": "@", "65": "A",
"66": "B", "67": "C", "68": "D", "69": "E", "70": "F",
"71": "G", "72": "H", "73": "I", "74": "J", "75": "K",
"76": "L", "77": "M", "78": "N", "79": "O", "80": "P",
"81": "Q", "82": "R", "83": "S", "84": "T", "85": "U",
"86": "V", "87": "W", "88": "X", "89": "Y", "90": "Z",
"91": "[", "92": "\\", "93": "]", "94": "^", "95": "_",
"96": "`", "97": "a", "98": "b", "99": "c", "100": "d",
"101": "e", "102": "f", "103": "g", "104": "h", "105": "i",
"106": "j", "107": "k", "108": "l", "109": "m", "110": "n",
"111": "o", "112": "p", "113": "q", "114": "r", "115": "s",
"116": "t", "117": "u", "118": "v", "119": "w", "120": "x",
"121": "y", "122": "z", "123": "{", "124": "|", "125": "}",
"126": "~", "127": ""
}
==和===
- ==很多时候很诡异.
- === 是所有情况下都建议使用的方法.
- https://stackoverflow.com/questions/3586775/what-is-the-correct-way-to-check-for-string-equality-in-javascript
- Mr. Douglas Crockford: http://www.youtube.com/watch?v=hQVTIJBZook
- Kyle Simpson: https://github.com/getify/You-Dont-Know-JS
- 这个结论有趣, 不要试图去理解js: I still recommend Crockford’s talk for developers who don’t want to invest the time to really understand Javascript—it’s good advice for a developer who only occasionally works in Javascript.
- 别用new, 哈哈哈这个建议深得我心.
- 举个例子: 0==”” //is true https://medium.com/@ludico8/identity-vs-equality-battle-of-understanding-vs-758d396e922