C++带有 std::hex 的字符串流
我正在工作中研究代码。我有以下代码。下面的代码中最后一条语句的含义是什么?
bOptMask = true;
std::string strMask;
strMask.append(optarg);
std::stringstream(strMask) >> std::hex >> iMask >> std::dec;
除了上述问题之外:我有字符串输入,我需要知道如何使用上面的 C++ 流而不是 atoi()
将其转换为整数。
我面临的问题是,如果我
strOutput.append(optarg);
cout << "Received option for optarg is " << optarg << endl;
std::stringstream(strOutput) >> m_ivalue ;
cout << "Received option for value is " << m_ivalue << endl;
为上面的代码提供输入,如果我使用参数“a”运行,我的输出第一行为“a”,第二行输出为0。我不确定为什么,谁能解释一下吗?
I am looking into code at work. I am having following code. In following code what is the meaning of the last statement?
bOptMask = true;
std::string strMask;
strMask.append(optarg);
std::stringstream(strMask) >> std::hex >> iMask >> std::dec;
In addition to the above question: I have string input and I need to know how to convert it to an integer using C++ streams as above instead of atoi()
.
The problem I am facing is if I give input
strOutput.append(optarg);
cout << "Received option for optarg is " << optarg << endl;
std::stringstream(strOutput) >> m_ivalue ;
cout << "Received option for value is " << m_ivalue << endl;
For the above code, if I am running with argument "a" I am having an output with first line as "a" and a second line output as 0. I am not sure why, can any one explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后一条语句创建一个临时字符串流,然后使用它将十六进制格式的字符串解析为 iMask。
但它也有缺陷,因为无法检查流是否成功,并且当您处理临时流时,最后一个流没有实现任何目标。
更好的方法是将 stringstream 创建为非临时的,最好使用 istringstream,因为您仅使用它来将 string 解析为 int,然后检查转换是否成功。
如果您的字符串流现在要解析十进制整数,则只需将模式设置回十进制。如果它要解析更多的十六进制,你也可以读取它们,例如,如果你从文件中有一堆它们。
如何处理错误取决于您。
std::hex
和std::dec
是流的
部分的一部分,指示文本的格式化方式。 hex 表示“十六进制”,dec 表示“十进制”。默认情况下,整数使用十进制,指针使用十六进制。由于我不知道的原因,没有用于打印 float 或 double 的十六进制表示形式,即没有“十六进制点”,尽管 C99 支持它。The last statement creates a temporary stringstream and then uses it to parse the string as hexadecimal format into iMask.
There are flaws with it though, as there is no way to check that the streaming succeeded, and the last stream achieves nothing as you are dealing with a temporary.
Better would be to create the stringstream as a non-temporary, ideally using istringstream as you are only using it to parse string to int, and then checking whether the conversion succeeds.
You only need to set the mode back to decimal if your stringstream is now about to parse a decimal integer. If it is going to parse more hex ones you can just read those in too, eg if you have a bunch of them from a file.
How you handle errors is up to you.
std::hex
andstd::dec
are part of the<iomanip>
part of streams that indicate the way text should be formatted. hex means "hexadecimal" and dec means "decimal". The default is to use decimal for integers and hexadecimal for pointers. For reasons unknown to me there is no such thing as a hex representation for printing float or double, i.e. no "hexadecimal point" although C99 sort-of supports it.该代码采用字符串
optarg
并将其视为十六进制,将其转换为整数并将其存储在 iMask 中。如果删除 std::hex 修饰符,您可以将输入解析为十进制。不过,我通常使用 boost 的 lexical_cast 来实现这一点。例如:
The code takes the string
optarg
and, treating it as hex, converts it to an integer and stores it in iMask.If you remove the std::hex modifier you can parse the input as decimal. However, I usually use boost's lexical_cast for this. For example:
此代码使用 操纵器 将流设置为期望以 16 进制读取整数(十六进制,使用数字 0123456789ABCDEF),然后从字符串中提取十六进制数字,将其存储在 iMask 中,并使用另一个操纵器将字符串流设置回默认值,即期望整数以十进制形式写入。
This code uses manipulators to set the stream to expect integers to be read in base 16 (hexadecimal, using the digits 0123456789ABCDEF), then extracts a hexadecimal number from the string, storing it in iMask, and uses another manipulator to set the string stream back to the default of expecting integers to be written in decimal form.