hex to int

This is a discussion on hex to int within the C Programming forums, part of the General Programming Boards category; hi i'm trying to make a program that can convert hex to int.. if user enters whole bunch of hex ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    hex to int(decimal)

    hi
    i'm trying to make a program that can convert hex to int..
    if user enters whole bunch of hex then the program read those as character string and read string by 2 characters and convet 2 digit hex to int..


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    //int sub1(char digit);
    unsigned char sub2(char digit[2]);
    void sub3(char *hex, unsigned char *dec);
    
    main()
    {
    	char hex[] = "B22101";
    	unsigned char dec;
    	int len;
    
    	len = strlen(hex);
    	printf("%s\n", hex);
    	printf("%d\n", len);
    	sub3(hex, &dec);
    	//printf("%d", dec);
    }
    
    void sub3(char *hex, unsigned char *dec)
    {
    	int len;
    	int i;
    
    	len = strlen(hex);
    	printf("len = %d\n", len);
    	printf("hex = %s\n", hex);
    
    	for(i=0; i<len/2; i++)
    	{
    		dec[i] = sub2(hex[2*i]);
    	}
    }
    
    unsigned char sub2(char *digit[2])
    {
    //	int i1, i2;
    	printf("%s", digit);
    return 0;
    }
    but my source code gives me 3 warnings..
    at dec[i] = sub2(hex[2*i]);

    warnings are..
    warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
    warning C4024: 'sub2' : different types for formal and actual parameter 1
    warning C4028: formal parameter 1 different from declaration
    Last edited by jyk1981; 06-24-2004 at 04:37 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I need to put this on a page I can link to:

    How to do base conversions the easy way:
    1) initalize a variable to keep track of the total to 0
    2) begin loop
    3) multiple the total by the base ( ie: total *= 8; for octo)
    4) read symbol and add its value to the total
    5) if there are more characters to be read goto #2
    6) display total.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    in psudo code:
    Code:
    make integer variable x
    make character variable temp
    assign 0 to x
    beginloop:
      assign temp the next symbol
      if ( symbol is exit condition )
        goto endloop
      x = x * base
      x = x + temp
      goto beginloop
    endloop

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't suppose you've thought about using sscanf().
    Code:
    char hex[] = "B22101";
    int dec;
    
    sscanf(hex, "%x", &dec);
    printf("%d\n", dec);

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As for the warnings, your prototype and definition for sub2() don't match:
    >unsigned char sub2(char *digit[2])
    unsigned char sub2(char digit[2])

    And I think the call here:
    dec[i] = sub2(hex[2*i]);
    Would instead be:
    dec[i] = sub2(&hex[2*i]);

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int sub1(char digit);
    unsigned char sub2(char digit[2]);
    void sub3(char *hex, unsigned char *dec);
    
    main()
    {
    	char hex[] = "B22101";
    	unsigned char dec;
    	int len;
    
    	len = strlen(hex);
    	//printf("%s\n", hex);
    	//printf("%d\n", len);
    	while(len!=0){
    	sub3(hex, &dec);
    	len = len - 2;
    	}
    
    	printf("%d\n", dec);
    }
    
    void sub3(char *hex, unsigned char *dec)
    {
    	int len;
    	int i;
    
    	len = strlen(hex);
    	//printf("len = %d\n", len);
    	//printf("hex = %s\n", hex);
    
    	for(i=0; i<len/2; i++)
    	{
    		dec[i] = sub2(&hex[2*i]);
    	}
    	// printf("%s\n", dec);
    }
    
    unsigned char sub2(char digit[2])
    {
    	int i1, i2;
    	//printf("digit = %s\n", digit);
    	i1 = sub1(digit[0]);
    	i2 = sub1(digit[1]);
    	//printf("i1= %d\n", i1);
    	//printf("i2 = %d\n", i2);
    	return i1*16+i2;
    }
    
    int sub1(char digit)
    {
    	switch(digit)
    	{
    	case'0':
    		return 0;
    	case'1':
    		return 1;
    	case'2':
    		return 2;
    	case'3':
    		return 3;
    	case'4':
    		return 4;
    	case'5':
    		return 5;
    	case'6':
    		return 6;
    	case'7':
    		return 7;
    	case'8':
    		return 8;
    	case'9':
    		return 9;
    	case'A':
    		return 10;
    	case'B':
    		return 11;
    	case'C':
    		return 12;
    	case'D':
    		return 13;
    	case'E':
    		return 14;
    	case'F':
    		return 15;
    }
    }
    thank you for the help..
    ok..
    now it compiles and seems like to work..
    but..
    some how it only convert first 2 hex number..
    in this case B2 to 178 and store 178 in dec[]
    then it converts 21 and 01 but not store in dec[]
    i added while loop to repeat the sub3.. but same thing happens..

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I sugest shortning jyk1981's sub1 function to:

    Code:
    int sub1(char digit){
    	if('0'<=digit && digit<='9')
    		return digit-'0';
    	if('A'<=digit && digit<='F')
    		return digit-'A'+10;
    	if('a'<=digit && digit<='f')
    		return digit-'a'+10;
    	return -1;
    }
    And this isn't case sensitive.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't do either of you any good to return -1 on error, if you don't bother checking the return value of the function to make sure it isn't -1...

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

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Of course Quzah, but the error must be known, either way...
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    #define HEXSIGNED 0x01
    #define HEXUNSIGNED 0x02
    
    int hexcharToInt(char digit){
    	if('0'<=digit && digit<='9')
    		return digit-'0';
    	if('A'<=digit && digit<='F')
    		return digit-'A'+10;
    	if('a'<=digit && digit<='f')
    		return digit-'a'+10;
    	return -1;
    }
    
    int hexToInt(char *h, int flags){
    	int i, result=0, isnegative=0;
    	int tmp;
    	
    	switch(flags){
    	case HEXSIGNED:
    		tmp = hexcharToInt(h[0]);
    		if(tmp == -1) return 0;
    
    		if(tmp & 0x80)//has negative signal
    			isnegative=1;
    		else
    			isnegative=0;
    		break;
    	case HEXUNSIGNED:
    		isnegative=0;
    		break;
    	default:
     		return 0;
    	}
    	
    	if(isnegative){ //parse the number in complement to 2
    		for(i=0;h[i] && h[i]=='f' && i<8;i++);//skip f's
    		for(;h[i] && i<8;i++){
    			tmp = hexcharToInt(h[i]);
    			if(tmp == -1) return -result-1;
    			result=result*0x10+0x0f-tmp;   		
    		}
    		result = -result-1;
    	}else
    		for(i=0;h[i] && i<8;i++){
    			tmp = hexcharToInt(h[i]);
    			if(tmp == -1) return result;
    			result=result*0x10+tmp;
    		}
    	
    	return result;
    }
    
    int main(){
    	char s[9];
    	int i, k;
    	
    	for(i=-300, k=0;i<300;i++, k=(k==20?0:k+1)){
    		sprintf(s, "%x", i);	
    		printf("%d\t%2s\t%d\n", i, s, hexToInt(s, HEXSIGNED));
    		if(k==20)
    			getch();
    	}
    	return 0;
    }
    This way negative number are also parsed, and the function break on the first non hex char. I've included a little main for testing.
    NOTE: one of the for cicles termination condition is i>=8 because a 32bit integer has only 8 4-bit block, or, 8 hex chars. If you dont' want this, remove it.
    Last edited by xErath; 06-25-2004 at 12:36 AM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course Quzah, but the error must be known, either way...
    I'm not sure how to parse that sentence. My point was, in the example before your code, the negative number would be processed anyway:
    Code:
    unsigned char sub2(char digit[2])
    {
    	int i1, i2;
    	i1 = sub1(digit[0]);
    	i2 = sub1(digit[1]);
    	return i1*16+i2;
    }
    Of course, in their example, they should have paid attention to their warnings, and they would have noticed a "control reaches end of non-void function".

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

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    What I was saying is that if a char isn't allow in a hex number then an error should be reported, both when you want or not, to know that there was a error.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by swoopy
    I don't suppose you've thought about using sscanf().
    Code:
    char hex[] = "B22101";
    int dec;
    
    sscanf(hex, "%x", &dec);
    printf("%d\n", dec);
    What's the fun about that???

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by xErath
    What's the fun about that???
    That's true, and in addition you don't learn much about HEX. It does make for a good verification source though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

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