# converting hex to dec

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 08-07-2004
Vicious
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.
• 08-07-2004
sean
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.
• 08-07-2004
jibbles
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 :)
• 08-07-2004
swoopy
Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> FILE * inputdata; void readfromfile(void); int main() {         readfromfile();                 return (0); } void readfromfile(void) {         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++;         } }```
• 08-07-2004
Vicious
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.
• 08-07-2004
jibbles
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!
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12