写入十六进制字符串时出现问题

发布于 2024-08-28 08:29:44 字数 966 浏览 3 评论 0原文

这是我的代码

/*
gcc -c -Wall -g main.c
gcc -g -lm -o main main.o
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void stringToHex(const char* string, char* hex) {
 int i = 0;

 for(i = 0; i < strlen(string)/2; i++) {
  printf("s%x", string[2*i]); //for debugging
  sprintf(&hex[i], "%x", string[2*i]);
  printf("h%x\n", hex[i]); //for debugging
 }
}

void writeHex(char* hex, int length, FILE* file, long position) {
 fseek(file, position, SEEK_SET);
 fwrite(hex, sizeof(char), length, file);
}

int main(int argc, char** argv) {
 FILE* pic = fopen("hi.bmp", "w+b");
 const char* string = "f2";
 char hex[strlen(string)/2];

 stringToHex(string, hex);
 writeHex(hex, strlen(string)/2, pic, 0);




 fclose(pic);
 return 0;
}

,我希望它将十六进制数字 0xf2 保存到文件中(不过稍后我将不得不编写更大/更长的数字)。 程序打印出——

s66h36

当我使用 hexedit 查看该文件时,我在其中看到数字“36”。

为什么我的代码不起作用?谢谢!

Here is my code

/*
gcc -c -Wall -g main.c
gcc -g -lm -o main main.o
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void stringToHex(const char* string, char* hex) {
 int i = 0;

 for(i = 0; i < strlen(string)/2; i++) {
  printf("s%x", string[2*i]); //for debugging
  sprintf(&hex[i], "%x", string[2*i]);
  printf("h%x\n", hex[i]); //for debugging
 }
}

void writeHex(char* hex, int length, FILE* file, long position) {
 fseek(file, position, SEEK_SET);
 fwrite(hex, sizeof(char), length, file);
}

int main(int argc, char** argv) {
 FILE* pic = fopen("hi.bmp", "w+b");
 const char* string = "f2";
 char hex[strlen(string)/2];

 stringToHex(string, hex);
 writeHex(hex, strlen(string)/2, pic, 0);




 fclose(pic);
 return 0;
}

I want it to save the hexadecimal number 0xf2 to a file (later I will have to write bigger/longer numbers though).
The program prints out -

s66h36

And when I use hexedit to view the file I see the number '36' in it.

Why is my code not working? Thanks!

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

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

发布评论

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

评论(3

﹉夏雨初晴づ 2024-09-04 08:29:45

使用muddybruin的答案我构建了这个-

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void stringToHex(char* string, int* hex) {
    int i = 0;
    char tmpByte[3] = {'\0', '\0', '\0'};

    for(i = 0; i < strlen(string)/2; i++) {
        memcpy(tmpByte, &string[2*i], 2);
        sscanf(tmpByte, "%x", &hex[i]);
    }
}

void writeHex(int* hex, int length, FILE* file, long position) {
    int i = 0;

    fseek(file, position, SEEK_SET);
    for(i = 0; i < length; i++) {
        fwrite(&hex[i], sizeof(char), 1, file);
        fseek(file, -3/4*sizeof(int), SEEK_CUR);
    }
}

int main(int argc, char** argv) {
    FILE* pic = fopen("hi.bmp", "w+b");
    char* string = "424D460000000000000036000000280000000200000002000000010018000000000010000000130B0000130B000000000000000000000000AAAFFFFF0000FF000000FF000000";
    int* hex = calloc(1000, sizeof(int));

    stringToHex(string, hex);
    writeHex(hex, strlen(string)/2, pic, 0);

    fclose(pic);
    return 0;
}

不是世界上最好的代码,但它有效:)。

Using muddybruin's answer I built this-

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void stringToHex(char* string, int* hex) {
    int i = 0;
    char tmpByte[3] = {'\0', '\0', '\0'};

    for(i = 0; i < strlen(string)/2; i++) {
        memcpy(tmpByte, &string[2*i], 2);
        sscanf(tmpByte, "%x", &hex[i]);
    }
}

void writeHex(int* hex, int length, FILE* file, long position) {
    int i = 0;

    fseek(file, position, SEEK_SET);
    for(i = 0; i < length; i++) {
        fwrite(&hex[i], sizeof(char), 1, file);
        fseek(file, -3/4*sizeof(int), SEEK_CUR);
    }
}

int main(int argc, char** argv) {
    FILE* pic = fopen("hi.bmp", "w+b");
    char* string = "424D460000000000000036000000280000000200000002000000010018000000000010000000130B0000130B000000000000000000000000AAAFFFFF0000FF000000FF000000";
    int* hex = calloc(1000, sizeof(int));

    stringToHex(string, hex);
    writeHex(hex, strlen(string)/2, pic, 0);

    fclose(pic);
    return 0;
}

Not the best code in the world, but it works :).

月竹挽风 2024-09-04 08:29:44

当它处理“f”时,它将其转换为字符f,即ascii 102,即十六进制66。这就是为什么你得到答案的“s66”部分。

%x 以十六进制表示形式打印出一个整数。

我认为您想要 sscanf( string, "%x", &hexInt )

这会将 string 作为十六进制字符串读取,并将其值保存在 int hexInt 中。

When it processes "f", it converted it to the character f, which is ascii 102, which is hex 66. That's why you got the "s66" part of your answer.

%x prints out an integer in its hexadecimal representation.

I think you want sscanf( string, "%x", &hexInt )

That'll read in string as a hexadecimal string and save its value in the int hexInt.

带刺的爱情 2024-09-04 08:29:44

从您的代码中根本看不出您真正想要完成的任务。您从包含十六进制数字的字符串开始,然后(显然)尝试将转换为十六进制...

通常,您会执行以下操作:

int x = 0xf2;

printf("%0x", x);

It's not (at all!) apparent from your code what you really want to accomplish. You're starting with a string containing hexadecimal digits, and then (apparently) trying to convert that to hexadecimal...

Normally, you'd do something like this:

int x = 0xf2;

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