简体   繁体   English

std :: hex和std :: setw无法使用某些字符

[英]std::hex and std::setw not working with some characters

What I'm trying to do is converting a string's bytes into hexadecimal format. 我正在尝试将字符串的字节转换为十六进制格式。
Based on this answer (and many others consistent) I've tried the code: 基于此答案 (以及许多其他一致的答案 ),我尝试了代码:

#include <sstream>
#include <iomanip>
#include <iostream>

int main ()
{
   std::string inputText = u8"A7°";

   std::stringstream ss;
   // print every char of the string as hex on 2 values
   for (unsigned int i = 0; i < inputText.size (); ++i)
   {
      ss << std::hex << std::setfill ('0') << std::setw (2) << (int) inputText[i];
   }

   std::cout << ss.str() << std::endl;
}

but with some characters coded in UTF 8 it does't work. 但是某些字符以UTF 8编码则无法正常工作。
For Instance, in strings containing the degrees symbol ( ° ) coded in UTF8, the result is: ffffffc2ffffffb0 instead of c2b0 . 对于Instance,在包含以UTF8编码的度数符号(°)的字符串中,结果为: ffffffc2ffffffb0而不是c2b0
Now I would expect the algorithm to work on individual bytes regardless of their contents and furthermore the result seems to ignore the setw(2) parameter. 现在,我希望该算法能够对单个字节起作用,而不管其内容如何,​​而且结果似乎忽略了setw(2)参数。
Why does I get such a result? 为什么会得到这样的结果?

(run test program here ) (在此处运行测试程序)

As Pete Becker already hinted in a comment, converting a negative value to a larger integer fills the higher bits with '1'. 正如皮特·贝克尔(Pete Becker)曾在评论中暗示的那样,将负值转换为较大的整数会用“ 1”填充较高的位。 The solution is to first cast the char to unsigned char before casting it to int : 解决方案是先将charunsigned char然后再将其转换为int

#include <string>
#include <iostream>
#include <iomanip>

int main()
{
    std::string inputText = "-12°C";
    // print every char of the string as hex on 2 values
    for (unsigned int i = 0; i < inputText.size(); ++i)
    {
       std::cout << std::hex << std::setfill('0')  
                 << std::setw(2) << (int)(unsigned char)inputText[i];
    }
}

setw sets the minimal width, it does not truncate longer values. setw设置最小宽度,它不会截断更长的值。

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

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