Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi: I have this function, conv

Status
Not open for further replies.

DougCa

Technical User
Nov 1, 2002
13
0
0
US

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 % '0') - 7 * (cmd / 'A') );

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

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

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

char* cmd =
&quot; \002\006\105\060\060\060\060\060\060\060\060\061\106\003&quot;; //Declaration of the buffer
printf(&quot;Integer Value = %d\n&quot;, 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 % '0') - 7 * (cmd / '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. cmd is the input (buffer) and the return value is the output. The sample input is in cmd and the sample output is 31. I would like to change the sample input to 30 30 31 46 and still get 31 for the output. I hope this is clear.

Thanks very much,
Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top