如何在 C++ 中将 Base64 字符串转换为十六进制字符串?
我有一个表示 base64 值的字符串。我想将此字符串从 base64 转换为十六进制。我在 Ubuntu 10.10 上使用 C++ 工作。我有以下代码:
std::string ssir = "DNQwSinfOUSSWd+U04r23A==";
std::string dec=(base64_decode(ssir));
std::stringstream ss;
for (int i=0; i<dec.size(); ++i) {
if (i != 0) ss << ':';
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(dec[i]);//(int)(dec[i]);
}
std::string res;
res= ss.str();
cout<<"the ress is: "<<res<<std::endl;
结果是:
0c:ffffffd4:30:4a:29:ffffffdf:39:44:ffffff92:59:ffffffdf:ffffff94:ffffffd3:ffffff8a:ffffffff6:ffffffdc
除了那些ffffffff之外,它是正确的。我可以做什么来解决这个问题?如果我希望将十六进制结果写入 std::vector
I have a string that represents a value in base64. I want to convert this string from base64 to hexadecimal. I'm working in C++ on Ubuntu 10.10. I have the following code:
std::string ssir = "DNQwSinfOUSSWd+U04r23A==";
std::string dec=(base64_decode(ssir));
std::stringstream ss;
for (int i=0; i<dec.size(); ++i) {
if (i != 0) ss << ':';
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(dec[i]);//(int)(dec[i]);
}
std::string res;
res= ss.str();
cout<<"the ress is: "<<res<<std::endl;
The result is:
0c:ffffffd4:30:4a:29:ffffffdf:39:44:ffffff92:59:ffffffdf:ffffff94:ffffffd3:ffffff8a:fffffff6:ffffffdc
It is correct, besides those ffffffff. What can I do to fix this? If I want my hexadecimal result to be written to a std::vector<unsigned char> x
, what do I have to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用此转换:
int
是 32 位宽,而您只想输出 8 位,这就是char
的宽度。大概std::hex
在格式化数字时考虑输入类型。 (虽然我不太确定符号扩展来自哪里,因为您正在转换为无符号类型......)Try using this cast instead:
int
is 32 bits wide and you only want to output 8 bits, which is how widechar
is. Presumablystd::hex
is considering the input type when formatting the number. (Though I'm not quite sure where the sign extension is coming from, since you are casting to an unsigned type...)您可以尝试另一种字符串到十六进制转换方法。我写了两个函数。 str_to_hex - 是我的方法。 str_to_hex2 - 是你的。我省略了base64编码。然后我调用了我的函数和你的函数 1M 次。 str_to_hex 的执行时间为
str_to_hex2 的执行时间为:
Ubuntu 10.04,64位,g++ 4.4.3,-O3选项。
测试程序代码如下。
You can try another string to hex conversion method. I wrote two functions. The str_to_hex - is my method. str_to_hex2 - is your. I omit base64 encoding. And then I invoked 1M times my function and your. The execution time for str_to_hex is
And the execution time for str_to_hex2 is:
Ubuntu 10.04, 64bit, g++ 4.4.3, -O3 option.
The code of testing programm is below.
我不确定这是否是最好的解决方案,但这就是我的做法。
I am not sure if this is the best solution but this is how I did it.
@cdhowie 的帖子几乎是正确的。但是,如果您转换为
char
(有符号或无符号),标准流运算符将尝试写出该字符的 ASCII 值。您将需要另一个强制转换:或显式截断值:(
在这两种情况下,外部
int
不需要是unsigned
;在第一种情况下,signed int
足够宽,可以容纳所有unsigned char
值,在第二个中,您显式地将值设置为正值。)@cdhowie's post is nearly correct. However, if you cast to a
char
(signed or unsigned), the standard stream operators will attempt to write out the ASCII value of that character. You will either need another cast:or to explicitly truncate the value:
(In both cases, your outer
int
does not need to beunsigned
; in the first case,signed int
is wide enough to hold allunsigned char
values, and in the second, you are explicitly making the value positive.)