I am taking a beginning C++ class, and would like to convert letters between hex representations and binary. I can manage to print out the hex numbers using:
我正在开始C ++类,并希望在十六进制表示和二进制之间转换字母。我可以设法打印出十六进制数字:
for(char c = 'a'; c <= 'z'; c++){
cout << hex << (int)c;
}
But I can't do the same for binary. There is no std::bin
that I can use to convert the decimal numbers to binary.
但我不能对二进制文件做同样的事情。没有std :: bin可用于将十进制数转换为二进制数。
6 个解决方案
#1
Like so:
for(char c = 'a'; c <= 'z'; c++){
std::bitset<sizeof(char) * CHAR_BIT> binary(c); //sizeof() returns bytes, not bits!
std::cout << "Letter: " << c << "\t";
std::cout << "Hex: " << std::hex << (int)c << "\t";
std::cout << "Binary: " << binary << std::endl;
}
#2
There isn't a binary io manipulator in C++. You need to perform the coversion by hand, probably by using bitshift operators. The actual conversion isn't a difficult task so should be within the capabilities of a beginner at C++ (whereas the fact that it's not included in the standard library may not be :))
在C ++中没有二进制io操纵器。您需要手动执行转换,可能是使用bitshift运算符。实际转换并不是一项艰巨的任务,因此应该在C ++初学者的能力范围内(而事实上它不包含在标准库中可能不是:))
Edit: A lot of others have put up examples, so I'm going to give my preferred method
编辑:很多其他人都举了例子,所以我打算给出我喜欢的方法
void OutputBinary(std::ostream& out, char character)
{
for (int i = sizeof(character) - 1; i >= 0; --i)
{
out << (character >> i) & 1;
}
}
This could also be potentially templated to any numeric type.
这也可能被模板化为任何数字类型。
#3
For bit of variety, you can also do it using a 16 element look up table.
对于各种各样,您也可以使用16元素查找表来完成。
#4
You can easily write a mapping between the hex charachters an their binary 'nibbles':
您可以轻松地在十六进制字符和二进制“半字节”之间编写映射:
std::string HexCharToNibble( char c ) {
switch (c) {
case '0': return "0000";
case '1': return "0001";
//... fill in the rest
case 'f': return "1111";
default: assert(false); return "bad input";
};
#5
You can do something like this:
你可以这样做:
for(char c = 'a'; c <= 'z'; c++){
// char is 8 bits. print 4 bits
// at a time, starting with the MSB
for (int i = 4; i>=0; i-=4) {
switch (((int)c >> i) & 0xf) {
case 0:
cout << "0000";
break;
case 1:
cout << "0001";
break;
.
.
.
case 0xf:
cout << "1111";
break;
}
}
}
#6
This sounds like an assignment, in which case you really should ask your teacher for help. Soliciting solutions to homework from the internet isn't really going to help you in the long run (unless you are going into project management).
这听起来像是一项任务,在这种情况下,你真的应该向老师寻求帮助。从互联网上寻求家庭作业的解决方案从长远来看并不会对你有所帮助(除非你进入项目管理)。
Answering chustar (the OQ'er) in the comments, I'd have to agree that if you understand how to do it, how/why it works,and how to figure it out yourself in the future, then yes, that would be a good thing.
在评论中回答chustar(OQ'er),我必须同意如果你明白怎么做,它是如何/为什么有效,以及如何在将来弄明白,那么是的,那将是一件好事。
However, the answer he marked "correct" puts the lie to that argument. It contains nothing but code, prefaced with the words "Like so". It's pretty clear that what the OQ'er was looking for was not for an explanation, but for someone to write his code for him.
然而,他标记为“正确”的答案是谎言。它只包含代码,前面加上“Like so”字样。很明显,OQ'er所寻找的并不是为了解释,而是为了有人为他编写代码。
#1
Like so:
for(char c = 'a'; c <= 'z'; c++){
std::bitset<sizeof(char) * CHAR_BIT> binary(c); //sizeof() returns bytes, not bits!
std::cout << "Letter: " << c << "\t";
std::cout << "Hex: " << std::hex << (int)c << "\t";
std::cout << "Binary: " << binary << std::endl;
}
#2
There isn't a binary io manipulator in C++. You need to perform the coversion by hand, probably by using bitshift operators. The actual conversion isn't a difficult task so should be within the capabilities of a beginner at C++ (whereas the fact that it's not included in the standard library may not be :))
在C ++中没有二进制io操纵器。您需要手动执行转换,可能是使用bitshift运算符。实际转换并不是一项艰巨的任务,因此应该在C ++初学者的能力范围内(而事实上它不包含在标准库中可能不是:))
Edit: A lot of others have put up examples, so I'm going to give my preferred method
编辑:很多其他人都举了例子,所以我打算给出我喜欢的方法
void OutputBinary(std::ostream& out, char character)
{
for (int i = sizeof(character) - 1; i >= 0; --i)
{
out << (character >> i) & 1;
}
}
This could also be potentially templated to any numeric type.
这也可能被模板化为任何数字类型。
#3
For bit of variety, you can also do it using a 16 element look up table.
对于各种各样,您也可以使用16元素查找表来完成。
#4
You can easily write a mapping between the hex charachters an their binary 'nibbles':
您可以轻松地在十六进制字符和二进制“半字节”之间编写映射:
std::string HexCharToNibble( char c ) {
switch (c) {
case '0': return "0000";
case '1': return "0001";
//... fill in the rest
case 'f': return "1111";
default: assert(false); return "bad input";
};
#5
You can do something like this:
你可以这样做:
for(char c = 'a'; c <= 'z'; c++){
// char is 8 bits. print 4 bits
// at a time, starting with the MSB
for (int i = 4; i>=0; i-=4) {
switch (((int)c >> i) & 0xf) {
case 0:
cout << "0000";
break;
case 1:
cout << "0001";
break;
.
.
.
case 0xf:
cout << "1111";
break;
}
}
}
#6
This sounds like an assignment, in which case you really should ask your teacher for help. Soliciting solutions to homework from the internet isn't really going to help you in the long run (unless you are going into project management).
这听起来像是一项任务,在这种情况下,你真的应该向老师寻求帮助。从互联网上寻求家庭作业的解决方案从长远来看并不会对你有所帮助(除非你进入项目管理)。
Answering chustar (the OQ'er) in the comments, I'd have to agree that if you understand how to do it, how/why it works,and how to figure it out yourself in the future, then yes, that would be a good thing.
在评论中回答chustar(OQ'er),我必须同意如果你明白怎么做,它是如何/为什么有效,以及如何在将来弄明白,那么是的,那将是一件好事。
However, the answer he marked "correct" puts the lie to that argument. It contains nothing but code, prefaced with the words "Like so". It's pretty clear that what the OQ'er was looking for was not for an explanation, but for someone to write his code for him.
然而,他标记为“正确”的答案是谎言。它只包含代码,前面加上“Like so”字样。很明显,OQ'er所寻找的并不是为了解释,而是为了有人为他编写代码。