# Thread: converting hex to dec

1. just look at the for loop I made above up there. you can change 8 to however many bits you need.

also you might want to start the loop at 32 and count backwards, if not your bits will be backwards.

2. I would recommend looking at a book that has a chapter on binary operations. Up until last week I was compeltely clueless about how to do all this and why you would need bit shifting, but I learned all about flags and masking and now it makes a lot more sense to me. Before you continue on this program I would recommend looking at some tutorials on things like masking bits, etc... The book I was looking at was called "Practical C Programming" and it's one of those "In a Nutshell" books with the monochrome animals on the front.

3. thanks sean, thats a pretty good idea.. i'll definently look up some tutorials, and i have a nice fat c book with me
a first book of ANSI C (third edition)
although i have trouble understanding it..

but thanks to everyone who has helped..
i should be able to get this done

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

FILE * inputdata;

int main()
{

return (0);
}

{

unsigned int value;
int i=0, j;

inputdata = fopen("data\\numbers.txt","r");

printf("Line #	Hex		Decimal		Binary\n");

while (fscanf(inputdata,"%x",&value) == 1)
{

printf("%i	%8x	%-16u",i , value , value);
for(j = 0; j < 32; j++)
printf("%d", (value >> (31 - j)) & 1);
printf("\n");
i++;

}
}```

5. It is ALWAYS good to have a good book close by. You never know when you might need to look something up.

Sam's Teach Yourself C in 24 Hours is a really good C book and has a very good explanation of bit shifting and binary in general.

6. thanks heaps! i wasn't thinking straight with that.. i was trying to put the for loop to add it ahead of the first printf, i couldn't think liek the program..
it really wasn't that hard.. but i can't thank you enough!