2017-08-15 135 views
0

我想将表示缓冲区的字符串转换为缓冲区正在编码的字符串。举例来说,如果我有串将缓冲区的字符串表示形式转换为缓冲区

var str1 = "hello,there" 

然后我用Buffer.from()

buf1 = Buffer.from(str1) 
<Buffer 68 65 6c 6c 6f 2c 74 68 65 72 65> 

然后将其转换为一个缓冲区,如果我把字符串

str2 = "68656c6c6f2c7468657265" 

而且将其转换回给我再缓冲区:

<Buffer 68 65 6c 6c 6f 2c 74 68 65 72 65> 

或者简单(因为你可以用的ToString缓冲区转换回字符串())

"hello,there" 

回答

0

缓冲区toString function接受的编码类型。在这种情况下,使用"hex"

buf1.toString("hello,there", "hex") // "68656c6c6f2c7468657265" 

Buffer.from function也接受的编码类型作为其第二个参数:

Buffer.from("68656c6c6f2c7468657265", "hex") // "hello,there" 
相关问题