简体   繁体   English

问题将缓冲区转换为c中的十六进制字符串

[英]the issue convert buffer to hex string in c

I'm trying to convert buffer to hex string in c as the below, the below image is the input file 我正在尝试将缓冲区转换为c中的十六进制字符串,如下所示,下图是输入文件 在此处输入图片说明 it's a binary file. 这是一个二进制文件。

#include "stdio.h"
#include "stdlib.h"

#include "string.h"
#include "fcntl.h"


#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

#define  BUFF_SIZE   5     

int main()
{
    char     buff[BUFF_SIZE];
    int      fd;
    ssize_t  rd_size;
    FILE *rfp;
    FILE *ofp;
    ofp = fopen("output.txt", "w");
    rfp = fopen("test.bin", "rb");


         while ( 4== (fread(buff, 1, 4, rfp)))
        {
            fprintf(ofp, "%02X%02X%02X%02X \n", buff[0], buff[1], buff[2], buff[3]);
        }

    fclose(ofp);
    fclose(rfp);
    return 0;
}

then I use above code, I've got bin to hex file but I've got the problem at result. 然后我使用上面的代码,我将bin放入了十六进制文件,但是结果出了问题。

04002B2B 
000001FFFFFFFF 
00030003 
00000000 
00000000 
00000000 
00000300 
00000000 
00000000 
00000000 
00000000 
00000000 
00000000 
00000000 
00500050 
00500050 
00500050 
00500050 
00000000 
FFFFFF80002000 
00000000 
08700F00 
00000000 
00000000 
00000001 
00000002 
00000003 
00000004 
00000005 
00000006 
00000007 
FFFFFF800FFFFFFFF01E 
087007FFFFFF80 
00320032 
0BFFFFFFB80820 
00050005 
2DFFFFFFC7114D 
00FFFFFFC20118 
00001B58 

As you can see that the above file, especially I don't want to "000001FFFFFFFF " this output. 如您所见,上面的文件,特别是我不想“ 000001FFFFFFFF”此输出。 But I don't know what am I supposed to do 但是我不知道该怎么办

update 更新

I want to run in the linux. 我想在linux中运行。 but if I make a execute file, I got the segmant error. 但是,如果我创建了一个执行文件,则会收到segmant错误。 Can you let me know what am I supposed to do? 你能让我知道我该怎么办吗?

The major problem here is that your char type seems to be signed (if char is signed or unsigned is implementation specific). 这里的主要问题是您的char类型似乎是带signed (如果char是带signed还是unsigned是实现特定的)。 That means that values larger equal or larger than 0x80 will be treated as negative values (with two's complement systems). 这意味着大于或等于0x80值将被视为负值(使用二进制补码系统)。

And when you pass such a value to printf and the value is promoted to an int it is sign-extended. 并且,当您将这样的值传递给printf并将该值提升int它将被符号扩展。 So 0xff becomes 0xffffffff , which is then printed. 因此0xff变为0xffffffff ,然后将其打印出来。

If you use unsigned char for your values instead, then values like 0xff are not extended when being promoted to unsigned int . 如果改为使用unsigned char作为值,则当提升为unsigned int时,不会扩展0xff类的值。 So 0xff is promoted to 0x000000ff , and the leading zeroes are not printed. 因此,将0xff提升为0x000000ff ,并且不打印前导零。


Another issue is that you are using the wrong format for printf . 另一个问题是您为printf使用了错误的格式。 The format "%x" is for unsigned int and not unsigned char . 格式"%x"用于unsigned int而非unsigned char If you read eg this printf (and family) reference you will see a big table with all the formatting code and the different size prefixes for different types. 如果您阅读printf (和系列)参考,您将看到一个大表,其中包含所有格式代码和不同类型的不同大小前缀。 To print an unsigned char you should use the hh prefix, as in "%hhx" . 要打印unsigned char ,应使用hh前缀,如"%hhx"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM