我有一个 C++/UnrealEngine 客户端,可以与现有的 C# 服务器和现有的 JavaScript 服务器通信(全部在 Windows 上)。
我的问题是,当我在 C++ 中对 Guid 进行 Base64 编码时,我从 C# 和 JS 得到不同的结果。据我所知,它与 Guid 字符串的编码方式直接相关,如this SO answer中所述。
我无法真正更改 C# 和 JS 服务器,因为它们还有其他依赖系统,那么我需要在 C++(或 Unreal)中做什么来确保获得相同的输出?
//The Guid as a literal string
"3F73B3E6-7351-416F-ACA3-AE639A3F587F"
//C# & JSEncoded
"5rNzP1Fzb0Gso65jmj9Yfw"
//C++ Encoded
"P3Oz5nNRQW+sowAArmOaPw"
另外使用在线转换器(为了自我检查)我得到了相同的两个不同结果,因此似乎没有真正的标准化方法。
//https://rcfed.com/Utilities/Base64GUID
//https://toolslick.com/conversion/data/guid
"5rNzP1Fzb0Gso65jmj9Yfw"
//https://www.fileformat.info/tool/guid-base64.htm
"P3Oz5nNRQW+sowAArmOaPw"
用于转换的代码如下:
//In C#
Guid myGuid = new Guid("3F73B3E6-7351-416F-ACA3-AE639A3F587F");
//Encodes / & + for URL & truncates the trailing "==" padding
string strBase64Guid = Convert.ToBase64String(myGuid.ToByteArray()).Substring(0, 22).Replace("/", "_").Replace("+", "-");
//In JavaScript
//https://stackoverflow.com/questions/55356285/how-to-convert-a-string-to-base64-encoding-using-byte-array-in-javascript
var strGuid = GuidToBase64("3F73B3E6-7351-416F-ACA3-AE639A3F587F");
function GuidToBase64(guid){
//Guid to ByteArray
var buffer = [];
guid.split('-').map((number, index) => {
var bytesInChar = index < 3 ? number.match(/.{1,2}/g).reverse() : number.match(/.{1,2}/g);
bytesInChar.map((byte) => { buffer.push(parseInt(byte, 16)); })
});
var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
//Encodes / & + for URL & truncates the trailing "==" padding
return base64String.slice(0, 22).replace("/", "_").replace("+", "-");
}
//In C++ (Unreal Engine)
FGuid& myGuid = FGuid("3F73B3E6-7351-416F-ACA3-AE639A3F587F");
FString& strGuid = myGuid.ToString(EGuidFormats::Short);
//Copyright Epic Games, Inc. (AFAIK I'm allowed to paste snippets from the sources, just not the whole thing. UE's source it's all on GitHub anyway.)
//Truncated [Function overloads, & Guid format handling]
uint32 Bytes[4] = { NETWORK_ORDER32(A), NETWORK_ORDER32(B), NETWORK_ORDER32(C), NETWORK_ORDER32(D) };
TCHAR Buffer[25];
int32 Len = FBase64::Encode(reinterpret_cast<const uint8*>(Bytes), sizeof(Bytes), Buffer);
TArrayView<TCHAR> Result(Buffer, Len);
//Truncated [Sanitizes '+' & '/' and cuts the '==' padding]
}
5rNzP1Fzb0Gso65jmj9Yfw
解码为:
E6 B3 73 3F 51 73 6F 41 AC A3 AE 63 9A 3F 58 7F
P3Oz5nNRQW+sowAArmOaPw
解码为:
3F 73 B3 E6 73 51 41 6F AC A3 00 00 AE 63 9A 3F
您的 C++ 代码已反转前 4 个字节。反转了接下来的两对字节并完全破坏了最后 6 字节组。