c中的十六进制到ASCII

发布于 2025-01-04 12:16:40 字数 1223 浏览 3 评论 0原文

这是我的逻辑,在 C 中将 HEX 转换为 ASCII:

for (i=0;i<ArraySize;i++)
    {
        /*uses a bitwise AND to take the top 4 bits from the byte,
         0xF0 is 11110000 in binary*/
        char1 = Tmp[i] & 0xf0;
        char1 = char1 >> 4;
        /*bit-shift the result to the right by four bits (i.e. quickly divides by 16)*/
        if (char1 >9)
        {
            char1 = char1 - 0xa;
            char1 = char1 + 'A';
        }
        else
        char1 = char1 + '0';
        Loc[j]=char1;
        j++;
        /*means use a bitwise AND to take the bottom four bits from the byte,
        0x0F is 00001111 in binary*/
        char1 = Tmp[i] & 0x0f;
        if (char1 >9)
        {
            char1 = char1 - 0xa;
            char1 = char1 + 'A';
        }
        else
        char1 = char1 + '0';
        Loc[j]=char1;
        j++;
        Loc[j]=0;
    }

Temp 和 Loc 是字符串缓冲区。已定义并有数据。它无法正常工作。我正在从某个文件读取 temp 数据(示例 fread)。它在特定点停止读取文件。如果我先改变

0xf0

0x0f

以下是读取文件的方式:

BytesRead = fread (Tmp,1,Bytes,PrcFile);

然后读取整个文件。我找不到丢失的东西。你能在这方面帮助我吗? 谢谢

Here is my logic, to convert HEX to ASCII conversion in C:

for (i=0;i<ArraySize;i++)
    {
        /*uses a bitwise AND to take the top 4 bits from the byte,
         0xF0 is 11110000 in binary*/
        char1 = Tmp[i] & 0xf0;
        char1 = char1 >> 4;
        /*bit-shift the result to the right by four bits (i.e. quickly divides by 16)*/
        if (char1 >9)
        {
            char1 = char1 - 0xa;
            char1 = char1 + 'A';
        }
        else
        char1 = char1 + '0';
        Loc[j]=char1;
        j++;
        /*means use a bitwise AND to take the bottom four bits from the byte,
        0x0F is 00001111 in binary*/
        char1 = Tmp[i] & 0x0f;
        if (char1 >9)
        {
            char1 = char1 - 0xa;
            char1 = char1 + 'A';
        }
        else
        char1 = char1 + '0';
        Loc[j]=char1;
        j++;
        Loc[j]=0;
    }

Temp and Loc are string buffers. Defined and has data. It is not working properly. I am reading data in temp from some file (sample fread). It stop reading file at particular point. If I change first

0xf0

to

0x0f

Here is how file is being read:

BytesRead = fread (Tmp,1,Bytes,PrcFile);

Then it reads whole file. I am not able to find what is missing. Can you please help me in this regards.
Thanks

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

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

发布评论

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

评论(2

夜血缘 2025-01-11 12:16:40

这不是一个答案,而是一个观察 - 使用它,因为它格式化代码

static char lookup[] = { '0', '1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
int j = 0;
for (i=0; i<ArraySize; ++i)
{
   loc[j++] = lookup[(Tmp[i] & 0xf0) >> 4];
   loc[j++] = lookup[Tmp[i] & 0xf];
}
loc[j] = 0;

使代码更快更简单。

This is not an answer but an observation - using this since it formats code

static char lookup[] = { '0', '1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
int j = 0;
for (i=0; i<ArraySize; ++i)
{
   loc[j++] = lookup[(Tmp[i] & 0xf0) >> 4];
   loc[j++] = lookup[Tmp[i] & 0xf];
}
loc[j] = 0;

makes the code a lot quicker and simpler.

千鲤 2025-01-11 12:16:40

尽管埃德已经提供了一个更短的解决方案,但我试图找出问题所在,因为你的代码“看起来”是正确的。

让我猜一下:char1 是有符号的(例如类型“char”)。

然后会发生这样的情况:

  • 文件中>127的字节在&0xf0期间保持其符号,

  • >> 期间签名4 是一个有符号移位,它使位模式将位设置保留在最高有效位

  • 然后您比较 >9 ,但情况并非如此,因为符号位仍然设置

  • 然后你添加+'0'现在可以导致你有一个值为 0 的字节而不是
    '0'-'9' 或 'A'-'F' 之间的值。

  • 打印时终止字符串

Even though Ed already provided a shorter solution, i tried to figure out what was wrong because your code "looked" correct.

Let me guess: char1 is signed (e.g. type "char").

It then happens, that:

  • a byte in your file that is >127 keeps its sign during &0xf0,

  • and >> 4 is a signed shift which makes the bit-pattern keep the bit set in the most significant bit

  • then you compare >9 which is not the case because the sign-bit is still set

  • then you add +'0' which can now lead to you having a byte with value 0 instead of
    something between '0'-'9' or 'A'-'F'.

  • which terminates the string while printing

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