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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert hex to dec or dec to hex

Status
Not open for further replies.

Jiko

Technical User
Apr 5, 2002
35
SE
Are there any functions I can use or do I have to write them myself? All kind of convertions between hex,dec,bin,ASCII.
I have strings with 32 byte that i count and do a checksum in the end.
ex. 0102030405060708090A0B0C0D0ECS
I also convert 2 byte to 1 and represent them in ASCII.
ex. 86 (1000 0110) => E in ASCII
I do like this now:
high = buffer.GetAt(i);
low = buffer.GetAt(i+1);
if(high >= 65)
high = high - 55;
else
high = high - 48;
if(low >= 65)
low = low - 55;
else
low = low - 48;
high = high << 4;
high = high + low;
i++;
summa += high;
I convert many times between hex to dec.
Thank you if you can help me
Martin
 
to convert intrinsic numbers to string representations in different base notation:

_itoa(), _uitoa(), _ltoa(), _ultoa()

to convert strings representing numbers in any base to a intrinsic number:

strtod(), strtol(), strtoul()

hope that helps
-pete
 
I don't think I understand what you are trying to accomplish. What variables are you converting from hex to dec, why and where? I also don't understand your checksum calculations.
Code:
summa+=atio(buffer[i]); //add byte to checksum
summa=summa && 0xff; //mask off any carry
I am probably completely missing what youare trying to. Please clarify.
 
Thinking some more maybe:

summa+=atio(buffer); //add byte to checksum

should be:

summa+=(int)buffer; //add byte to checksum

depending on what your data in 'buffer' represents.
 
Try messing with std::hex and std::dec with streams. E.G.

int myInt = 10;

cout << &quot;hex: &quot; << std::hex << myInt << ' '
<< &quot;dec: &quot; << std::dec << myInt << endl;


 
#define F binary # here
#define f binary # here
#include <iostream>
using namespace std;
int main()
{
int numOne;

cout<<&quot;Enter a number in hex: &quot;;
cin>>hex>>numOne;

if(numOne<=1)
cout<<&quot;Nope&quot;;
if(numOne>=f)
cout<<&quot;Nope&quot;;
else
cout<<&quot;Your DEC conversion is:&quot;<<numOne;
return 0;
}

This what I did, and it worked
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top