converting hex to dec

This is a discussion on converting hex to dec within the C Programming forums, part of the General Programming Boards category; just look at the for loop I made above up there. you can change 8 to however many bits you ...

  1. #16
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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. #17
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #18
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Added your code to output in binary.
    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++;
    
    	}
    }

  5. #20
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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. #21
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    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!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. hex to dec to binary
    By vijlak in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 09:38 AM
  3. converting an int into hex and memcpy
    By puppy in forum C Programming
    Replies: 10
    Last Post: 07-03-2006, 11:39 AM
  4. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  5. Converting hex to dec problems
    By Dragoon_42 in forum C Programming
    Replies: 4
    Last Post: 10-16-2003, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21