gpt4 book ai didi

c++ - 如何将保存在字节数组中的 UUID 转换为字符串 (c++)

转载 作者:行者123 更新时间:2023-11-27 23:03:23 25 4
gpt4 key购买 nike

我有一个像这样保存为 16 字节数据的 UUID:

   unsigned char uuid[16]; //128-bits unique identifier

我想将它转换为 std::string。我可能可以编写一个循环来考虑所有字节并生成字符串,但我正在寻找更简单/更快的解决方案。有没有这样的解决方案?

最佳答案

如果你真的想避免循环,你总是可以这样做:

char str[37] = {};
sprintf(str,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
);

不是很优雅,但我认为这是没有循环和平台依赖性的最好的。

关于c++ - 如何将保存在字节数组中的 UUID 转换为字符串 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25566056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com