如何在 C++ 中将十六进制数转换为二进制数?

发布于 2024-07-12 23:36:52 字数 243 浏览 3 评论 0原文

我正在学习 C++ 初级课程,并且想在十六进制表示形式和二进制形式之间转换字母。 我可以使用以下方法打印出十六进制数字:

for(char c = 'a'; c <= 'z'; c++){
    cout << hex << (int)c;
}

但我不能对二进制做同样的事情。 没有可以用来将十进制数转换为二进制数的 std::bin

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回心转意 2024-07-19 23:36:52

就像这样:

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;
    }

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;
    }
作业与我同在 2024-07-19 23:36:52

C++ 中没有二进制 io 操纵器。 您需要手动执行转换,可能使用位移运算符。 实际的转换并不是一项困难的任务,因此应该在 C++ 初学者的能力范围内(而事实上它不包含在标准库中可能不会:))

编辑:很多其他人都提出了示例,所以我将给出我的首选方法

void OutputBinary(std::ostream& out, char character)
{
  for (int i = sizeof(character) - 1; i >= 0; --i)
  {
    out << (character >> i) & 1;
  }
}

,这也可以模板化为任何数字类型。

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 :))

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.

静谧 2024-07-19 23:36:52

对于一些变化,您还可以使用 16 元素查找表来完成。

For bit of variety, you can also do it using a 16 element look up table.

煮茶煮酒煮时光 2024-07-19 23:36:52

您可以轻松地编写十六进制字符与其二进制“半字节”之间的映射:

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";
};

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";
};
゛时过境迁 2024-07-19 23:36:52

你可以这样做:

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;


        }
    }
}

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;


        }
    }
}
深爱不及久伴 2024-07-19 23:36:52

这听起来像是一项作业,在这种情况下你真的应该向你的老师寻求帮助。 从长远来看,从互联网上寻求家庭作业的解决方案并不能真正帮助你(除非你要进入项目管理)。

在评论中回答 chustar(OQ'er),我必须同意,如果您了解如何做到这一点,它如何/为什么起作用,以及如何在将来自己弄清楚,那么是的,那就是一件好事。

然而,他标记为“正确”的答案证明了这一论点是谎言。 它只包含代码,并以“Like so”开头。 很明显,OQ'er 所寻找的并不是解释,而是有人为他编写代码。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文