简体   繁体   English

Node.js中String的Base64编码

[英]Base64 encoding of String in Node.js

I try to encode a string in both browser and server but I have different base64 encoding for a specific string 我尝试在浏览器和服务器中都对字符串进行编码,但是对于特定字符串,我有不同的base64编码

Here is my string: "£aº©S=³hPó c¨¸" (Hexa: 00a3006100ba00a900940053003d00b30068005000f300900020006300a800b8 ) 这是我的字符串:“£a = S =³hPóc¨¸”(十六进制:00a3006100ba00a900940053003d00b30068005000f300900020006300a800b8)

Client-side: I encode this String using btoa() and I have : o2G6qZRTPbNoUPOQIGOouA== and this is the result I expect. 客户端:我使用btoa()对该字符串进行编码,并且具有: o2G6qZRTPbNoUPOQIGOouA== ,这是我期望的结果。

Server-side: I code this String using Buffer according to this answer in Node.js i have : 服务器端:根据我在Node.js中的答案 ,使用Buffer对该字符串进行编码:

var ciphertext = ... // myString
console.log(ciphertext.hexEncode()); // 00a3006100ba00a900940053003d00b30068005000f300900020006300a800b8  
console.log(Buffer.from(ciphertext, 'utf8').toString('base64')) // wqNhwrrCqcKUUz3Cs2hQw7PCkCBjwqjCuA==
console.log(Buffer.from(ciphertext, 'ucs2').toString('base64')) // owBhALoAqQCUAFMAPQCzAGgAUADzAJAAIABjAKgAuAA=

I managed to obtain the base64 encoding I expect using the node-package base-64 我设法使用node-package base-64获得了我期望的base64编码

I still don't know why, so if someone has a clue 我仍然不知道为什么,所以如果有人有线索

var base64 = require('base-64');

var ciphertext = ...; //myString
var encoded = base64.encode(bytes);
console.log(encoded); // o2G6qZRTPbNoUPOQIGOouA==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM