nodejs - Buffer, File, String 编码,读和写的区别
nodejs - differences between encoding on Buffer, File, String , reading and writing
我想检查我的系统是否能够处理客户提供的文件。
由于我的客户可以给我未知编码的文件,我想创建一些测试文件以了解是否可以在不损坏的情况下读取文件。
我的系统 运行 在 nodejs 上 12.x
我读了
- this thread about encoding and charset
- this nodejs official documentation about buffers
我不能很好地理解在 writeFile 上使用编码之间的区别
fs.writeFileSync("ascii.xml", Buffer.from("hello"), {'encoding':'ascii'})
toString 编码
fs.writeFileSync("ascii.xml", Buffer.from("hello").toString(encoding="ascii"))
在 toString 和 File 上编码
fs.writeFileSync("ascii.xml", Buffer.from("hello").toString(encoding="ascii"), {'encoding':'ascii'})
在 Buffer 上编码等等
fs.writeFileSync("ascii.xml", Buffer.from("hello",'ascii').toString(encoding="ascii"), {'encoding':'ascii'})
例如我无法理解为什么这段代码
fs.writeFileSync("base64.xml", Buffer.from("string",'utf8').toString(encoding="base64"),{'encoding':'base64'})
生成一个包含单词“string”的文件
但是这个
fs.writeFileSync("base64.xml", Buffer.from("string",'utf8').toString(encoding="base64"))
生成一个包含单词“string”的 base64 表示的文件:从 base64 缓冲区生成的 base64 编码字符串的 base64 编码文件构建了我放入字符串中的相同字符?
感谢任何建议。
让我们了解基础知识:
- 当您调用
Buffer.from("hello")
时,您将字符串 hello
映射(编码)为字节。
(注意hello
默认被解释为UTF8编码的字符串,所以上面的表达式等价于Buffer.from("hello", "utf8")
)。
结果缓冲区是:
<Buffer 68 65 6c 6c 6f>
.
- 当您调用
Buffer.from("hello").toString()
时,您将 hello
映射(编码)为字节,然后将这些字节映射(解码)回字符串。
再次注意,UTF8 是默认值,因此上面的表达式等效于 Buffer.from("hello").toString("utf8")
以及 Buffer.from("hello", "utf8").toString("utf8")
.
结果字符串不出所料:
"hello"
.
但是,将其更改为 Buffer.from("hello").toString("base64")
,您将得到:
aGVsbG8=
.
现在,让我们前进到fs.writeFileSync
:
fs.writeFileSync(file, data[, options])
写入一个文件,即:bytes。换句话说,当 data
是一个字符串时,writeFileSync
将该字符串映射(编码)为字节,然后将这些字节写入文件。
同样,默认编码是 UTF8,但是当您传递一些其他编码时,data
将使用此编码映射(编码)为字节。
顺便说一句,当你“打开文件”时,无论是使用一些文本编辑器还是调试器本身,你都可能(不知道)使用 UTF8 编码。
希望您现在可以理解:
fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64"))
将 hello
映射(编码)为字节(使用 UTF8 编码),然后将这些字节映射(解码)为字符串(使用 Base64 编码),这是 aGVsbG8=
.
这是您打开在 last 片段中创建的文件时看到的内容。
fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64"), "base64")
将 hello
映射(编码)为字节(使用 UTF8 编码),然后将这些字节映射(解码)为字符串(使用 Base64 编码),然后映射(将此字符串编码)为字节(使用 Base64 编码)。这就是您在 倒数第二个 代码段中所做的。
为了说明,请注意这一系列“映射”等同于:
var bytes = Buffer.from(Buffer.from("hello").toString("base64"), "base64")
给出:
<Buffer 68 65 6c 6c 6f>
.
然后,当您打开文件时,您(即可能是您的文本编辑器)默认将这些字节读取为 UTF8,相当于:
bytes.toString() // 'bytes' is defined above
给出:
"hello"
.
我想检查我的系统是否能够处理客户提供的文件。 由于我的客户可以给我未知编码的文件,我想创建一些测试文件以了解是否可以在不损坏的情况下读取文件。
我的系统 运行 在 nodejs 上 12.x
我读了
- this thread about encoding and charset
- this nodejs official documentation about buffers
我不能很好地理解在 writeFile 上使用编码之间的区别
fs.writeFileSync("ascii.xml", Buffer.from("hello"), {'encoding':'ascii'})
toString 编码
fs.writeFileSync("ascii.xml", Buffer.from("hello").toString(encoding="ascii"))
在 toString 和 File 上编码
fs.writeFileSync("ascii.xml", Buffer.from("hello").toString(encoding="ascii"), {'encoding':'ascii'})
在 Buffer 上编码等等
fs.writeFileSync("ascii.xml", Buffer.from("hello",'ascii').toString(encoding="ascii"), {'encoding':'ascii'})
例如我无法理解为什么这段代码
fs.writeFileSync("base64.xml", Buffer.from("string",'utf8').toString(encoding="base64"),{'encoding':'base64'})
生成一个包含单词“string”的文件
但是这个
fs.writeFileSync("base64.xml", Buffer.from("string",'utf8').toString(encoding="base64"))
生成一个包含单词“string”的 base64 表示的文件:从 base64 缓冲区生成的 base64 编码字符串的 base64 编码文件构建了我放入字符串中的相同字符?
感谢任何建议。
让我们了解基础知识:
- 当您调用
Buffer.from("hello")
时,您将字符串hello
映射(编码)为字节。
(注意hello
默认被解释为UTF8编码的字符串,所以上面的表达式等价于Buffer.from("hello", "utf8")
)。
结果缓冲区是:
<Buffer 68 65 6c 6c 6f>
. - 当您调用
Buffer.from("hello").toString()
时,您将hello
映射(编码)为字节,然后将这些字节映射(解码)回字符串。
再次注意,UTF8 是默认值,因此上面的表达式等效于Buffer.from("hello").toString("utf8")
以及Buffer.from("hello", "utf8").toString("utf8")
.
结果字符串不出所料:
"hello"
.
但是,将其更改为Buffer.from("hello").toString("base64")
,您将得到:
aGVsbG8=
.
现在,让我们前进到fs.writeFileSync
:
fs.writeFileSync(file, data[, options])
写入一个文件,即:bytes。换句话说,当data
是一个字符串时,writeFileSync
将该字符串映射(编码)为字节,然后将这些字节写入文件。
同样,默认编码是 UTF8,但是当您传递一些其他编码时,data
将使用此编码映射(编码)为字节。
顺便说一句,当你“打开文件”时,无论是使用一些文本编辑器还是调试器本身,你都可能(不知道)使用 UTF8 编码。
希望您现在可以理解:
fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64"))
将hello
映射(编码)为字节(使用 UTF8 编码),然后将这些字节映射(解码)为字符串(使用 Base64 编码),这是aGVsbG8=
.
这是您打开在 last 片段中创建的文件时看到的内容。fs.writeFileSync("myFile.xml", Buffer.from("hello","utf8").toString("base64"), "base64")
将hello
映射(编码)为字节(使用 UTF8 编码),然后将这些字节映射(解码)为字符串(使用 Base64 编码),然后映射(将此字符串编码)为字节(使用 Base64 编码)。这就是您在 倒数第二个 代码段中所做的。为了说明,请注意这一系列“映射”等同于:
var bytes = Buffer.from(Buffer.from("hello").toString("base64"), "base64")
给出:
<Buffer 68 65 6c 6c 6f>
.然后,当您打开文件时,您(即可能是您的文本编辑器)默认将这些字节读取为 UTF8,相当于:
bytes.toString() // 'bytes' is defined above
给出:
"hello"
.