从十六进制值中提取单个整数值

时间:2016-11-19 23:25:50

标签: c

这是我的十六进制代码:

42 4D C6 00 00 00 00 00 00 00 76 00 00 00 28 00 
00 00 0A 00 00 00 0A 00 00 00 01 00 04 00 00 00 
00 00 50 00 00 00 12 0B 00 00 12 0B 00 00 10 00 
00 00 10 00 00 00 FF 00 00 00 00 FF 00 00 00 00 
42 00 5A 5A 84 00 00 00 FF 00 FF 00 FF 00 00 FF 
FF 00 08 FF FF 00 5A FF FF 00 FF FF FF 00 FF FF 
FF 00 FF FF FF 00 FF FF FF 00 FF FF FF 00 FF FF 
FF 00 FF FF FF 00 92 59 00 16 47 00 00 00 25 90 
01 64 61 00 00 00 59 90 11 64 61 00 00 00 99 00 
16 48 11 00 00 00 90 01 64 61 11 00 00 00 00 16 
64 61 00 00 00 00 01 16 46 10 09 00 00 00 11 64 
41 00 99 00 00 00 16 64 11 09 95 00 00 00 66 48 
10 09 53 00 00 00

我知道像素“赋值”从第一行(10像素宽)开始:

92 59 00 16 47 00 00 00

我需要计算图像中每种颜色的次数,但是我无法拉出单个整数值(即:只有9,然后只有2,然后只有5,依此类推)。我能够提取的唯一值是“92”然后是“59”然后“00”......

这是我对该段的代码(偏移量为118,剩余的总十六进制值为80):

 int nbr_each[NBRCOLOURS];
    int ch, pixel;

            fseek(fptr, 118, SEEK_SET);
            for (count = 0; count < 81; count++)
            {
                pixel = fgetc(fptr);
                nbr_each[pixel] = nbr_each[pixel] + 1;
            }

1 个答案:

答案 0 :(得分:0)

fgetc会为你提供个性化的角色。

first = fgetc(fptr);   // '9'
second = fgetc(fptr);  // '2'
space = fgetc(fptr);   // ' '

然后通过减去&#39; 0&#39;来将每个数字转换为数字0..9:

first -= '0';
second -= '0';

然后计算每个数字,如下所示:

nbr_each[first]++;
nbr_each[second]++;