Thread: converting hex to dec

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    converting hex to dec (edited)

    hey guys i've got a conversion here but it is not displaying correctly.

    it is making the large hexadecimal numbers into negative decimal numbers.

    also when displaying a hex number with a 0 at the front it isn't leaving displaying the 0, i need it to.
    (its reading these numbers from a file.

    this is the code.
    thanks guys..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    FILE * inputdata;
    
    void openfile(void);
    
    
    
    
    int main()
    {
    
    	openfile();
    	
    	return (0);
    }
    
    
    
    
    void openfile(void)
    {
    
    
    	unsigned int hex;
    	int i=0;
    
    	inputdata = fopen("numbers.txt","r");
    
    	if (inputdata==NULL)
    	{ 
    		printf("File cannot be opened for reading");
    	} 
    
    	printf("Line #	Hex		Decimal		Binary\n");
    
    	while (fscanf(inputdata,"%x",&hex) != EOF)
    	{
    		
    		
    		printf("%i	%x	%d\n",i , hex , hex);
    		i++;
    		
    
    	}
    }
    Last edited by jibbles; 08-07-2004 at 02:59 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to have your function prototypes backwards. main always returns an int, and since you don't actually do anything with the return value of open_file (which is very poorly named, IMO), it may as well be void.

    The globals are sloppy, and you don't provide any example of your input file either.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    edited now. thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (fscanf(inputdata,"%x",&hex) != EOF)
    Only works if your file consists only of hex numbers
    Anything unexpected will cause your program to loop forever

    while (fscanf(inputdata,"%x",&hex) == 1)
    is a better test

    > it is making the large hexadecimal numbers into negative decimal numbers.
    Use %u in place of %d

    > also when displaying a hex number with a 0 at the front it isn't leaving displaying the 0
    How about
    %#x
    when printing the hex then

    Or
    0x%x
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    salem thanks for your help.
    still have a problem with the last thing..

    i'm reading hex numbers from a file, for example 00032ad45
    and the %#x hasn't fixed me there..
    i'll keep trying but any help would be great.

    all numbers i'm importing from the file are 32 bit numbers

    eg 12a44d72

    thanks

    ______________
    edit : worked it out.
    thanks anyway!

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    ok, i'm attempting to change my code to read into a 2 dimensional array now. so i'll go into more depth.

    there is a 40 of 32 bit hexadecimal numbers in a file.

    eg.
    12a44d72
    4d72608f
    .
    .
    .
    980c2983

    so i'm attempting to read those from the files into a two dimensional array, but i've got an error which i can't figure out.

    this is the new updated code.

    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 hex[40][8],i,j;
    
    	inputdata = fopen("numbers.txt","r");
    
    	if (inputdata==NULL)
    	{ 
    		printf("File cannot be opened for reading");
    	} 
    
    	printf("Line #	Hex		Decimal		Binary\n");
    
    	for (i=0; i<40; i++)
    	{
    		if (hex == " ")
    		{
    			break;
    		}
    
    		for (j=0; j<8; j++)
    		{
    			fscanf(inputdata,"%d", hex[i][j]);	
    		}
        }
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you want your output to match your input, then store it in a string
    Use fgets() to read the file in the first instance, then use sscanf() to extract information from the resulting string.

    There is no point in storing all your input in an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    sorry the reason i want to store it in an array is to then convert it into binary, which i'm not all that sure about anyway.

    but i was thinking that if its in a two dimensional array i can use a switch statement to look at each individual number and then assign a binary value to match..
    if that makes sense..

    you wouldn't know a better way would you?

    you've been very helpful so far, thanks.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What would be the purpose of the two dimensions then? Also, you're saying you're going to read each individual character in the file then? It is most definately easier to just read the entire value at once, as you're doing with fscanf or the fgets/sscanf combo, than it is to read each individual character and assemble the number yourself.

    I'm still not sure of what you intend to do with the two dimensions. Perhaps you could throw together a bit of pseudocode to explain your intentions?

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    well i'm not sure of the best way to convert the hexadecimal into binary code.
    so i thought what i would do is look at each part of the number individually..
    eg.

    12a44d72
    would be broken into blocks of
    |1|2|a|4|4|d|7|2|
    and then converted like
    |0001||0010|1010|0100|0100| etc..

    but there is possibly a better way of doing it?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        FILE *fp = stdin;   // replace with fopen() call when you're ready
        while ( fgets( buff, sizeof buff, fp ) != NULL ) {
            int iHex;
            char sHex[20];
            if ( sscanf( buff, "%x", &iHex ) == 1 ) {
                printf( "As an int, we have %d\n", iHex );
            }
            if ( sscanf( buff, "%s", sHex ) == 1 ) {
                printf( "As a string, we have %s\n", sHex );
            }
        }
        return 0;
    }
    Perhaps something along the lines of a for loop and strlen(sHex) would be in order
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    To break the hex number up you could "mask" it by using the & operator and some bit shifting.

    example:

    Code:
    hexNumber = 0xdab3;
    
    nibble1 = (hexNumber & 0xF000) >> 12;
    nibble2 = (hexNumber & 0x0F00) >> 8;
    nibble3 = (hexNumber & 0x00F0) >> 4;
    nibble4 = (hexNumber & 0x000F);
    now nibble1 should contain "d" all by itself.
    nibble2 should be a
    nibble3 b
    and nibble4 3

    once youve broken them up you could do a loop and convert to binary like so

    Code:
    for(int loop = 8; loop >= 0; loop--) {
      int iBytes = (nibble1 >> loop) & 1;
      printf("%d", iBytes);
    }
    I am not promising that the info is 100% correct, but you should get the idea
    Last edited by Vicious; 08-07-2004 at 03:40 PM.

  13. #13
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Quote Originally Posted by Vicious
    Code:
    for(int loop = 8; loop >= 0; loop--) {
      int iBytes = (nibble1 >> loop) & 1;
      std::cout << iBytes;
    }
    I am sure that works and all, but std::cout? This is the C forum.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    lol, wooops, i'll edit that

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    guys that stuff looks really technical and i can't really understand it..
    i'll definently spend alot of today trying to make something like you have suggested work, or possibly you could help me with this idea..

    this is my current code.

    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 hex;
    	int i=0;
    
    	inputdata = fopen("numbers.txt","r");
    
    	printf("Line #	Hex		Decimal		Binary\n");
    
    	
    	while (fscanf(inputdata,"%x",&hex) == 1)
    	{
    	
    	printf("%i	%8x	%u\n",i , hex , hex, hex);
    	i++;
    
    	}
    }
    and i realised that with this for loop i'm able to convert the numbers into binary like i need to, but i'm not able to work it into my printf. i couldn't figure out how.

    Code:
    for(i = 0; i < 32; i++)
    printf("%d", (value >> (31 - i)) & 1);
    i tried to put it inside the printf then i decided thats probably not even possible, then i was trying to work it into storing it into a new variable, but i just really couldn't grasp the concept of what was needed to acheive this. so if anyone could explain how i could use that to convert to binary that would be great. it does work, i just can't work it properly.

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, 11: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