Thread: Quick Conversion Question

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    Quick Conversion Question

    Hi:

    I have this function, converting the ASCII value in to a corresponding HEX and then doing a bitshift by 4. It does octal.


    iReturnValue = (iReturnValue <<= 4) | ( (cmd[I] % '0') - 7 * (cmd[I] / 'A') );

    if I want to change this to convert from to an integer from hexascii what needs to be changed?

    iReturnValue = (iReturnValue >>= 4) | ( (cmd[I] % '0') - 7 * (cmd[I] / 'A') );

    I thought this above, but it does not seem to work.
    -------------------
    The whole thing looks like this:

    Code:
    /*
    char* cmd = 
    "\002\006\105\060\060\060\060\060\060\060\060\061\106\003"; //Declaration of the buffer
    	printf("Integer Value = %d\n", getValue(cmd));	//Retrieving the integer value from the last 4 bytes
    
    
    int getValue(char* cmd)
    {
    	int iReturnValue = 0;					//Declaration and initialization of the returnValue variable.
    
    	//Loop to iterate through the last 4 values in the array
    	for(int I = g_ciBufferPrefixLength - 1; I < g_ciBufferLength - 1; I++)	
    //Loop to iterate
    	{
    		//Converting the ASCII value in to a corresponding HEX and then doing a bitshift by 4
    
    		iReturnValue = (iReturnValue <<= 4) | ( (cmd[I] % '0') - 7 * (cmd[I] / 'A') );//Octal Input
    	}
    
    	return iReturnValue;
    }*/

    This works as it is, but what if the last 4 bytes were Hex not octal, that is what I am trying to do. I want the same result. The current output for the above is int 31. I want it to be 31 if the last 4 bytes were 30 30 31 46 instead of 60 60 61 106 like they are now. The char (cmd) buffer is the input(see code above). The return value is the int output.


    Thanks,
    Doug
    Last edited by DougC; 01-10-2003 at 05:31 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you give an example of the input data, and show what you want to end up with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can you give an example of the input data, and show what you want to end up with.
    I think this may be an example.

    Why not try the standard library routine strtol?
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    /*
    #define NDEBUG
    */
    
    short foo(const char *buffer, int offset)
    {
        char text[5] = {0};
        memcpy(text, &buffer[offset], sizeof(text) - 1);
    #ifndef NDEBUG
        printf("text = \"%s\", ", text);
    #endif
        return (short)strtol(text, NULL, 16);
    }
    
    int main(void)
    {
        const char buffer[14] =
        {
            2, 6, 060, 060, 060, 060, 060, 060, 060, 0106, 0106, 0105, 061, 3
        };
        int i;
        for ( i = 2; i < 10; i++ )
        {
            short result = foo(buffer, i);
            printf("result = %hd\n", result);
        }
        return 0;
    }
    My output:
    Code:
    text = "0000", result = 0
    text = "0000", result = 0
    text = "0000", result = 0
    text = "0000", result = 0
    text = "000F", result = 15
    text = "00FF", result = 255
    text = "0FFE", result = 4094
    text = "FFE1", result = -31

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Dave_Sinkula
    Why not try the standard library routine
    Probably because there is no standard function called strtol. ANSI provides no such function.

    [edit]
    It's been brought to my attention that there is actually an ANSI strtol. Ah well, it's not the first time my point of reference has been wrong. Perhaps I'll use a new reference. Shrug.
    [/edit]

    Quzah.
    Last edited by quzah; 01-10-2003 at 07:26 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  2. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  3. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  4. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:58 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM