Thread: really got stuck with unsigned chars

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    really got stuck with unsigned chars

    Hi,
    I have program that converts 16 hex chars to 8-ascii characters
    while testing, i have noted that the program generates a large unsigned chars figures such 266,239, 160...
    How can I get the corresoonding ascii characters from above unsigned chars ?
    here is the snippet:

    Code:
    code
      
    /* prototype */
    ByteType ASCIZ(ByteType *Addr);     
    
    ByteType ASCIZ(ByteType *Addr)
    {
    	int i, v1, v2;
    ByteType a1, a2, v,y;
    
    
    	/* Convert 16-HEXA-character key to be 8-ASCII-character key. */
    	for (i = 0; i < 8; i++)
    	{
    		a1 = *Addr++;
    		if (i == 7)
    			a2 = *Addr;
            else
    			a2 = *Addr++;
    		/* Convert two HEXA characters into their decimal value. */
    		v1 = Convert(a1);
    		v2 = Convert(a2);
    
            
           /* Convert the two decimal value to be one ASCII character. */
    		v = ((v1 * 16) + v2);
    	}
    	
    	  return  v;
    }
    
    
    
    int Convert(ByteType val )
    {
       if ((val >= 0  ) && (val <= '9' ))
            
          return val;           
       
       else ...
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: really got stuck with unsigned chars

    Originally posted by Abdi
    i have noted that the program generates a large unsigned chars figures such 266,239, 160...
    Unsigned char is 8 bits (0-255)...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    28
    Sorry, monster
    it is 166 not 266, that was my error

  4. #4
    *
    Guest
    That's right. 0-255. If the byte (char) is signed, then it is -127 to +127. That's because the high-bit is used to give sign. Otherwise, unsigned bytes (unsigned chars) are assumed positive.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What is the type of ByteType?

    --

    Testing can be done as follows, create a variable like this:

    ByteType X [9] = "FFFFFFFF";

    Then pass this to the function ASCIZ as follows:

    ASCIZ (X);

    --

    In the function you are increasing the pointer. This is dangerous, better would be to have a local pointer in the function, called X_ptr or something, which points to the elements of the array. Or just use the indices of the array.

    --

    It seems you're having two hexadecimal values, like 0x05 and 0x06, which you want to combine into one ASCII character. 0x05 and 0x06 are between 0 and 9, so you add them to 16*5+6 = 86.
    And that value is returned.

    I do not really understand the purpose of the function. Can you give an example input with the correct output?

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    28
    Hi shiro,

    here is the desired effect.

    if X = "3132333446474849", then I expect an output something like:

    the corresponding characters are 0
    the corresponding characters are 1
    the corresponding characters are 2
    the corresponding characters are 3
    the corresponding characters are F
    the corresponding characters are G
    the corresponding characters are H
    the corresponding characters are I

    shiro you said use a local pointer X_ptr , I don't get what you mean ?

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What Shiro means is: use a local pointer called X_ptr and let it point to Addr.
    Then work with the X_ptr in stead of Addr.
    This way the original pointer (Addr) is not changed.

    Code:
    ByteType ASCIZ(ByteType *Addr)
    {
      ByteType *X_ptr = Addr;
      int i, v1, v2;
      ByteType a1, a2, v,y;
    
      /* Convert 16-HEXA-character key to be 8-ASCII-character key. */
      for (i = 0; i < 8; i++)
      {
        a1 = *X_ptr++;
        if (i == 7)
          a2 = *X_ptr;
        else
          a2 = *X_ptr++;
        
        /* Convert two HEXA characters into their decimal value. */
        v1 = Convert(a1);
        v2 = Convert(a2);
    
        /* Convert the two decimal value to be one ASCII character. */
        v = ((v1 * 16) + v2);
      }
      return  v;
    }

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The easiest way to solve this problem would be with a simple lookup table. Define a table of strings and hex-numbers like:

    Code:
    typedef struct
    {
        char *string;
        int  hexvalue;
    } string_hex_s;
    
    string_hex_s string_hex_table [..] =
      {
      {"30", 0x30},
      {"31", 0x31},
      {"32", 0x32},
      {"33", 0x33},
      {"34", 0x34}
      ....
      };
    Each time read two chars of the original string into a string, search for the string in the table and you find the hex-value immediately.

    Another method, comparable to your method. Read two characters at once. Then use atoi to convert the string to integer and finally convert to hex-value.

    Converting from decimal to hex can be done like the following example, the way you calculate it is not correct:

    445 / 16 = 27 R 13
    27 / 16 = 1 R 11
    1 / 16 = 0 R 1

    1= 1, 11 = B and 13 = D, so 445 = 1BD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM