英文:
+= operator adding unexpected 'ff' byte to string in c++
问题
Here's the translated code part:
我有这段代码:
```cpp
#include <iostream>
void test(std::string in) {
for (short i = 0; i < in.length(); i++)
std::cout << std::hex << (unsigned short)in[i];
std::cout << "\n";
std::string msg = in;
for (short i = 0; i < msg.length(); i++)
std::cout << std::hex << (unsigned short)msg[i];
std::cout << "\n";
msg += (char)128;
for (short i = 0; i < msg.length(); i++)
std::cout << std::hex << (unsigned short)msg[i];
}
int main() {
test("123456");
}
我期望的输出是:
313233343536
313233343536
31323334353680
但实际输出如下:
313233343536
313233343536
313233343536ff80
很明显,+=
运算符做了我没有考虑到的事情。我在一台64位机器上使用Code::Blocks。我应该如何修复它?
英文:
I have this code:
#include<iostream>
void test(string in){
for(short i=0; i<in.length(); i++)
cout<<hex<<(unsigned short)in[i];
cout<<"\n";
string msg=in;
for(short i=0; i<msg.length(); i++)
cout<<hex<<(unsigned short)msg[i];
cout<<"\n";
msg+=(char)128;
for(short i=0; i<msg.length(); i++)
cout<<hex<<(unsigned short)msg[i];
}
int main(){
test("123456");
}
I expect the output to be:
313233343536
313233343536
31323334353680
But instead, it is the following:
313233343536
313233343536
313233343536ff80
It's clear that the += operator does something that i didn't count with. I use Code::Blocks on a 64-bit machine. How can I fix it?
答案1
得分: 4
Your compiler uses a signed char
type, which has a value range of [-128, 127]
.
msg+=(char)128;
This line adds a character with a value of -128
, represented in binary as 0b1000'0000
.
When you read this character in (unsigned short)msg[i]
, it undergoes a promotion, padding the value with 1 bits until the width of the target type is reached. Then, the conversion to unsigned short
occurs, resulting in 0b1111'1111'1000'0000
, which is equivalent to 0xff80
.
To resolve this, you can cast to unsigned char
first:
for (short i = 0; i < msg.length(); i++)
cout << hex << static_cast<unsigned short>(static_cast<unsigned char>(msg[i]));
英文:
Your compiler uses an signed char
type, i.e. the range of values is [-128, 127]
.
msg+=(char)128;
adds a char with value -128
which is represented binary 0b1000'0000
.
When you read this char in (unsigned short)msg[i]
, the value first undergoes a promotion padding the value with 1 bits until the width of the target type is reached and then the conversion to unsigned short
happens leaving you with (0b1111'1111'1000'0000
= 0xff80
)
0b1111'1111'1000'0000
^^^^ ^^^^ bits from sign extension
^^^^ ^^^^ 128
To fix this you can cast to unsigned char
first:
for (short i = 0; i < msg.length(); i++)
cout << hex << static_cast<unsigned short>(static_cast<unsigned char>(msg[i]));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论