C++ char 十六进制值转字节

发布于 2024-11-30 03:46:48 字数 253 浏览 0 评论 0原文

我在字符指针中有一个十六进制值(例如“F3”),我想将其转换为字节,因为我希望将其放入数组中。我知道有很多解决方案,但它们都不是我想要的。

提前致谢!

编辑:

好吧,也许我还没有写完所有内容。 我现在拥有的:

char aChar[5];
itoa (j, aChar, 16);

j 现在是 3,我只想要它的字节数。 Atoi,scanf 没有帮助,这些是不同的解决方案。

I have a hex value in a char pointer (for example 'F3'), and I would like to convert it to byte, because I want it to put in an array. I know that there are many solutions, but they are not what I wanted.

Thanks in advance!

EDIT:

Okay, maybe I have not written everything.
What I have now:

char aChar[5];
itoa (j, aChar, 16);

j is now 3, and I just want it in byte. Atoi, scanf doesn't help, those are the different solutions.

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

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

发布评论

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

评论(4

原谅我要高飞 2024-12-07 03:46:49

由于您已标记此 C++ 而不是 C,因此我不会使用任何 C 函数(除了 assert() 来演示行为、边缘条件等等等 )。这是一个示例文件。我们将其称为 hex2byte.cpp:

#include <sstream>
#include <cassert>

unsigned char hex2byte(const char* hex)
{
    unsigned short byte = 0;
    std::istringstream iss(hex);
    iss >> std::hex >> byte;
    return byte % 0x100;
}

int main()
{
    const char* hex = "F3";
    assert(hex2byte(hex) == 243);
    assert(hex2byte("") == 0);
    assert(hex2byte("00") == 0);
    assert(hex2byte("A") == 10);
    assert(hex2byte("0A") == 10);
    assert(hex2byte("FF") == 255);
    assert(hex2byte("EEFF") == 255);
    assert(hex2byte("GG") == 00);
    assert(hex2byte("a") == 10);
    assert(hex2byte("0a") == 10);
    assert(hex2byte("f3") == 243);
    assert(hex2byte("ff") == 255);
    assert(hex2byte("eeff") == 255);
    assert(hex2byte("gg") == 00);
}

制作它:

% make hex2byte
g++ -Wall -Wextra -Wshadow -pedantic -Weffc++ -Werror hex2byte.cpp -o hex2byte

运行它:

% ./hex2byte

无断言。添加错误处理以适应口味(例如检查何时hex == NULL等等)。

Since you've tagged this C++ and not C, I'm not going to use any C functions (except assert() to demonstrate the behaviour, edge conditions, et cetera). Here's a sample file. Let's call it hex2byte.cpp:

#include <sstream>
#include <cassert>

unsigned char hex2byte(const char* hex)
{
    unsigned short byte = 0;
    std::istringstream iss(hex);
    iss >> std::hex >> byte;
    return byte % 0x100;
}

int main()
{
    const char* hex = "F3";
    assert(hex2byte(hex) == 243);
    assert(hex2byte("") == 0);
    assert(hex2byte("00") == 0);
    assert(hex2byte("A") == 10);
    assert(hex2byte("0A") == 10);
    assert(hex2byte("FF") == 255);
    assert(hex2byte("EEFF") == 255);
    assert(hex2byte("GG") == 00);
    assert(hex2byte("a") == 10);
    assert(hex2byte("0a") == 10);
    assert(hex2byte("f3") == 243);
    assert(hex2byte("ff") == 255);
    assert(hex2byte("eeff") == 255);
    assert(hex2byte("gg") == 00);
}

Make it:

% make hex2byte
g++ -Wall -Wextra -Wshadow -pedantic -Weffc++ -Werror hex2byte.cpp -o hex2byte

Run it:

% ./hex2byte

No assertions. Add error handling to taste (such as checking for when hex == NULL, et cetera).

找个人就嫁了吧 2024-12-07 03:46:49

字节通常只是一个无符号字符

myArray[n] = (unsigned char)*p;

或者您的意思是您有十六进制值的字符串表示形式?

A byte is usually simply an unsigned char

myArray[n] = (unsigned char)*p;

Or do you mean that you have a string representation of an hex value?

⒈起吃苦の倖褔 2024-12-07 03:46:49

给定一个 char *"F3"

char *hexstr = "F3";

然后你可以这样做:

unsigned char byteval =
    (((hexstr[0] >= 'A' && hexstr[0] <= 'Z') ? (10 + hexstr[0] - 'A') : 
    (hexstr[0] >= 'a' && hexstr[0] <= 'z') ? (10 + hexstr[0] - 'a') : 
    (hexstr[0] >= '0' && hexstr[0] <= '9') ? (hexstr[0] - '0') : 0) << 4) |
    ((hexstr[1] >= 'A' && hexstr[1] <= 'Z') ? (10 + hexstr[1] - 'A') : 
    (hexstr[1] >= 'a' && hexstr[1] <= 'z') ? (10 + hexstr[1] - 'a') : 
    (hexstr[1] >= '0' && hexstr[1] <= '9') ? (hexstr[1] - '0') : 0);

我很抱歉它的丑陋;我确信它可以改进。

您可以将其变成一个函数:

inline unsigned char hextobyte(const char *s) {
    return
    (((s[0] >= 'A' && s[0] <= 'Z') ? (10 + s[0] - 'A') : 
    (s[0] >= 'a' && s[0] <= 'z') ? (10 + s[0] - 'a') : 
    (s[0] >= '0' && s[0] <= '9') ? (s[0] - '0') : 0) << 4) |
    ((s[1] >= 'A' && s[1] <= 'Z') ? (10 + s[1] - 'A') : 
    (s[1] >= 'a' && s[1] <= 'z') ? (10 + s[1] - 'a') : 
    (s[1] >= '0' && s[1] <= '9') ? (s[1] - '0') : 0);
}

Given a char * with "F3":

char *hexstr = "F3";

Then you can do this:

unsigned char byteval =
    (((hexstr[0] >= 'A' && hexstr[0] <= 'Z') ? (10 + hexstr[0] - 'A') : 
    (hexstr[0] >= 'a' && hexstr[0] <= 'z') ? (10 + hexstr[0] - 'a') : 
    (hexstr[0] >= '0' && hexstr[0] <= '9') ? (hexstr[0] - '0') : 0) << 4) |
    ((hexstr[1] >= 'A' && hexstr[1] <= 'Z') ? (10 + hexstr[1] - 'A') : 
    (hexstr[1] >= 'a' && hexstr[1] <= 'z') ? (10 + hexstr[1] - 'a') : 
    (hexstr[1] >= '0' && hexstr[1] <= '9') ? (hexstr[1] - '0') : 0);

I'm sorry for its ugliness; I'm sure it can be improved.

You can turn this into a function:

inline unsigned char hextobyte(const char *s) {
    return
    (((s[0] >= 'A' && s[0] <= 'Z') ? (10 + s[0] - 'A') : 
    (s[0] >= 'a' && s[0] <= 'z') ? (10 + s[0] - 'a') : 
    (s[0] >= '0' && s[0] <= '9') ? (s[0] - '0') : 0) << 4) |
    ((s[1] >= 'A' && s[1] <= 'Z') ? (10 + s[1] - 'A') : 
    (s[1] >= 'a' && s[1] <= 'z') ? (10 + s[1] - 'a') : 
    (s[1] >= '0' && s[1] <= '9') ? (s[1] - '0') : 0);
}
忆沫 2024-12-07 03:46:49

我至少可以想到五种方法:

  1. 使用 sscanf%x
  2. 使用 strtol 使用正确的基数
  3. 使用 istringstream (尽管您必须从 unsigned Short 向下转换为 unsigned char)
  4. boost::spirit
  5. 手滚动循环来逐个字符地解析

您列出的所有方法都不起作用。但你的问题仍然不太清楚,你有一个字符,你以某种方式使用 itoa 将其转换为十六进制,现在你想转换为字节!?!演员阵容有什么问题吗?例如unsigned char byte = static_cast(charvalue);

I can think of at least five ways:

  1. using sscanf with %x
  2. using strtol with the correct base
  3. using istringstream (though you'll have to down cast from unsigned short to unsigned char)
  4. boost::spirit
  5. hand rolled loop to parse character-by-character

None of the ways you listed would work. But your question still isn't really clear, you have a char which somehow you converted to hex using itoa and now you want to convert to a byte!?! what's wrong with a cast? e.g. unsigned char byte = static_cast<unsigned char>(charvalue);

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