如何将四个一字节的数转换为一个四字节数

时间:2023-01-12 08:06:21

      最近在研究图像显示这块,想在/dev/graphics/fb0上显示图像,在研究时需要一个图片的十六进制数值,找了好久,找了一个软件可以实现该功能。

如何将四个一字节的数转换为一个四字节数    Img2Lcd.exe 这个软件可以实现,一张图片转换后的结果如下:

const unsigned char gImage_1233[128640] = { /* 0X00,0X20,0XF0,0X00,0X86,0X00,0X00,0XE4, */
0X5D,0X31,0X12,0X00,0X5D,0X31,0X12,0X00,0X5D,0X31,0X12,0X00,0X5D,0X31,0X0F,0X00,
0X5E,0X31,0X10,0X00,0X5D,0X2E,0X12,0X00,0X5D,0X2F,0X11,0X00,0X5E,0X31,0X0F,0X00,
0X62,0X33,0X14,0X00,0X64,0X36,0X16,0X00,0X67,0X39,0X17,0X00,0XA3,0X42,0X02,0X00,
0XA4,0X42,0X02,0X00,0XA4,0X42,0X02,0X00,0XA4,0X41,0X03,0X00,0XA7,0X42,0X03,0X00,
0XA3,0X42,0X06,0X00,0XA5,0X44,0X02,0X00,0XAB,0X41,0X04,0X00,0XA7,0X44,0X03,0X00, 。。。。};

看到了吗?是char类型的,可是我要的是int类型的,可惜这个软件没有这个功能,如何是好呢。于是自己写了个简单的文件操作代码,在这个转换基础之上转换在做处理,代码如下:

 1 /*************************************************************************
 2     > File Name: main.c
 3     > Author: winfu
 4     > Mail: wenfuandyou@163.com 
 5     > Created Time: Thu 07 Jul 2016 10:08:36 AM CST
 6  ************************************************************************/
 7 #include<stdio.h>
 8 #include<string.h>
 9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 
13 int main()
14 {
15     int fd1 = open("./file.txt",O_RDWR);
16     int fd2 = open("./file_bak.txt",O_CREAT|O_WRONLY,0666);
17     int num;
18     if(fd1 < 0 || fd2 < 0)
19     {
20         perror("open");
21         return -1;
22     }
23     ssize_t res = 1;
24     char buf4[4] = {0};
25     char buf2[2] = {0};
26     int i = 0;
27     char c = ',';
28     char cn = '\n';
29     printf("------------\n");
30     do{
31             i++;
32             res = read(fd1,buf4,sizeof(buf4));
33             printf("%s",buf4);
34             write(fd2,buf4,sizeof(buf4));
35             
36             lseek(fd1, 3,SEEK_CUR);
37             res = read(fd1,buf2,sizeof(buf2));
38             printf("%s",buf2);
39             write(fd2,buf2,sizeof(buf2));
40             
41             lseek(fd1, 3,SEEK_CUR);
42             res = read(fd1,buf2,sizeof(buf2));
43             printf("%s",buf2);
44             write(fd2,buf2,sizeof(buf2));
45             
46             lseek(fd1, 3,SEEK_CUR);
47             res = read(fd1,buf2,sizeof(buf2));
48             printf("%s\t",buf2);
49             write(fd2,buf2,sizeof(buf2));
50             write(fd2,&c,1);
51             lseek(fd1, 1,SEEK_CUR);
52             num++;
53             if(i==4)
54             {
55                 i = 0;
56                 printf("\n");
57                 write(fd2,&cn,1);
58                 lseek(fd1, 1,SEEK_CUR);
59             }
60     }while(res);
61     printf("------------\n");
62     printf("num = %d\n",num);
63     close(fd1);
64     close(fd2);
65     return 0;
66 }

将转Img2Lcd.exe转换好的文件去掉头尾 存放到file.txt文档中,将file文档放在该程序的路径下,最后生成file_bak.txt,该文件存放的是转换后的数据:

0X5D311200,0X5D311200,0X5D311200,0X5D310F00,
0X5E311000,0X5D2E1200,0X5D2F1100,0X5E310F00,
0X62331400,0X64361600,0X67391700,0XA3420200,
0XA4420200,0XA4420200,0XA4410300,0XA7420300,
0XA3420600,0XA5440200,0XAB410400,0XA7440300,
0XA9440300,0XAC420500,0XA9440300,0XA8440300,
0XA9450400,0XA3460400,0XAC460500,0XA9450900 ….

这个数据正好是我想要的4字节。