2

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

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
QMG
  • 719
  • 2
  • 7
  • 16

2 Answers2

2

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

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

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • Yes, that seemed to be the problem. Try your code and change the declaration of `char1` to `unsigned char`. – Bernd Elkemann Feb 10 '12 at 12:23
  • I tried to run it on different files but it reads same number of bytes from files and them stop. No matter file size is 30 K or 100K – QMG Feb 10 '12 at 12:23
  • How many characters are read? Is the two arrays of the correct lengths? – Ed Heal Feb 10 '12 at 12:33
  • Ed, I have run you code it is also behaving the same as my old code. It reads exactly 16384 bytes no matter what is the size of file. [for sure file should be greater than this number of bytes]. Actually I have setup to read different number of bytes base upon setup defined. It reads 16382 characters correctly and then it reads only 2 character, although I asked to read 29 characters. – QMG Feb 10 '12 at 12:45