Quick Conversion Question
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:
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
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.