我有下面的.NET代码通过首先将字符串转换为字节数组来将字符串转换为Base64编码。我在Stack Overflow上尝试了不同的答案,以将字节数组转换为字符串,然后将btoa()函数用于JavaScript中的base64编码。但是,我没有得到下面共享的确切编码值。
对于字符串值,
BBFDC43D-4890-4558-BB89-50D802014A97
我需要Base64编码,
PcT9u5BIWEW7iVDYAgFKlw ==
.NET代码:
String str = "BBFDC43D-4890-4558-BB89-50D802014A97"
Guid guid = new Guid(str);
Console.WriteLine(guid); // bbfdc43d-4890-4558-bb89-50d802014a97
Byte[] bytes = guid.ToByteArray();
Console.WriteLine(bytes); // System.Byte[]
String s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
Console.WriteLine(s); // PcT9u5BIWEW7iVDYAgFKlw==
当前,我尝试使用以下代码,但未产生预期的结果:
function strToUtf8Bytes(str) {
const utf8 = [];
for (let ii = 0; ii < str.length; ii++) {
let charCode = str.charCodeAt(ii);
if (charCode < 0x80) utf8.push(charCode);
else if (charCode < 0x800) {
utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
} else if (charCode < 0xd800 || charCode >= 0xe000) {
utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
} else {
ii++;
// Surrogate pair:
// UTF-16 encodes 0x10000-0x10FFFF by subtracting 0x10000 and
// splitting the 20 bits of 0x0-0xFFFFF into two halves
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(ii) & 0x3ff));
utf8.push(
0xf0 | (charCode >> 18),
0x80 | ((charCode >> 12) & 0x3f),
0x80 | ((charCode >> 6) & 0x3f),
0x80 | (charCode & 0x3f),
);
}
}
return utf8;
}
const str = "BBFDC43D-4890-4558-BB89-50D802014A97";
const strByteArr = strToUtf8Bytes(str);
const strBase64 = btoa(strByteArr);
// NjYsNjYsNzAsNjgsNjcsNTIsNTEsNjgsNDUsNTIsNTYsNTcsNDgsNDUsNTIsNTMsNTMsNTYsNDUsNjYsNjYsNTYsNTcsNDUsNTMsNDgsNjgsNTYsNDgsNTAsNDgsNDksNTIsNjUsNTcsNTU=
答案 0 :(得分:1)
您的代码存在问题是Guid的表示形式。在C#代码中,您正在将“ BBFDC43D-4890-4558-BB89-50D802014A97”转换为UUID,这是一个128位数字。在JavaScript代码中,您正在做其他事情。您遍历字符串并计算字符串的字节数组。他们根本不平等。 现在您必须选择
答案 1 :(得分:1)
您的问题是由以下原因引起的:
btoa()
使用ASCII编码guid.ToByteArray();
不使用ASCII编码如果您这样修改C#代码:
String str = "BBFDC43D-4890-4558-BB89-50D802014A97";
//Guid guid = new Guid(str);
//Console.WriteLine(guid);
// bbfdc43d-4890-4558-bb89-50d802014a97
//Byte[] bytes = guid.ToByteArray();
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);
//Console.WriteLine(bytes); // System.Byte[]
String s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
Console.WriteLine(s);
您将获得以下输出:
QkJGREM0M0QtNDg5MC00NTU4LUJCODktNTBEODAyMDE0QTk3
与btoa()
函数返回的字符串相同:
var rawString = "BBFDC43D-4890-4558-BB89-50D802014A97";
var b64encoded = btoa(rawString);
console.log(b64encoded);
输出:
QkJGREM0M0QtNDg5MC00NTU4LUJCODktNTBEODAyMDE0QTk3
更新-由于您无法修改C#代码
您应该结合使用Piotr's answer和this SO answer
来修改Javascript代码。function guidToBytes(guid) {
var bytes = [];
guid.split('-').map((number, index) => {
var bytesInChar = index < 3 ? number.match(/.{1,2}/g).reverse() : number.match(/.{1,2}/g);
bytesInChar.map((byte) => { bytes.push(parseInt(byte, 16)); })
});
return bytes;
}
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
var str = "BBFDC43D-4890-4558-BB89-50D802014A97";
var guidBytes = guidToBytes(str);
var b64encoded = arrayBufferToBase64(guidBytes);
console.log(b64encoded);
输出:
PcT9u5BIWEW7iVDYAgFKlw==
答案 2 :(得分:0)
您的字符串是一个十六进制值,可用于创建GUID。然后使用以下命令将GUID转换为字节数组:
Byte[] bytes = guid.ToByteArray();
GUID是一个16字节的值,可以表示为十六进制值。当您将此GUID转换为字节数组时,将获得该值的16个字节,而不是十六进制值的字节表示。
在提供的JavaScript函数中,您正在做其他事情:您正在将字符串直接转换为字节数组。
在C#中,您可以使用Encoding
进行等效操作:
String str = "BBFDC43D-4890-4558-BB89-50D802014A97";
Byte[] bytes = Encoding.UTF8.GetBytes(str);