RevJoker
asked on
Hex number..
What I want to do is make a number which is "00458927" into "0x00458927" and store the number into an int.
Those two 'numbers' are not equal to each other, they are different values. And if you put them thru a numeric conversion, it will strip off the leading zeros because leading zeros are not significant and are effectively not part of a number. Storing them into an 'int' will do that also.
You have used C++.net and C++ as tags.
Which language do you want? C++.net or C++
Which language do you want? C++.net or C++
Based on the wording of your question, I can only assume you are starting with a string that represents a hexidecimal number, but just doesn't have the leading "0x" hex indicator.
If I am correct, you can convert the string to an integer using the C++ function strtol() (see http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/ for more info). If for some reason you didn't want to use library functions, you could write your own function that can convert a hex string to an integer like this:
CString str = "00458927"
int nLen = str.GetLength();
int nExp = 1;
int nResult = 0;
for(int i=nLen; i; nExp *= 16)
{
char ch = str[--i];
if( '0' <= ch && ch <= '9' )
nResult += (ch - '0') * nExp;
else if( 'A' <= ch && ch <= 'F' )
nResult += (ch - 'A' + 10) * nExp;
else if( 'a' <= ch && ch <= 'f')
nResult += (ch = 'a' + 10) * nExp;
else if( ch == 'x' || ch == 'X' )
nExp--; //Skip an 'X' assuming the string was formated 0x####
else
;//You don't have a valid number
}
If I am correct, you can convert the string to an integer using the C++ function strtol() (see http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/ for more info). If for some reason you didn't want to use library functions, you could write your own function that can convert a hex string to an integer like this:
CString str = "00458927"
int nLen = str.GetLength();
int nExp = 1;
int nResult = 0;
for(int i=nLen; i; nExp *= 16)
{
char ch = str[--i];
if( '0' <= ch && ch <= '9' )
nResult += (ch - '0') * nExp;
else if( 'A' <= ch && ch <= 'F' )
nResult += (ch - 'A' + 10) * nExp;
else if( 'a' <= ch && ch <= 'f')
nResult += (ch = 'a' + 10) * nExp;
else if( ch == 'x' || ch == 'X' )
nExp--; //Skip an 'X' assuming the string was formated 0x####
else
;//You don't have a valid number
}
ASKER
I'm sorry but I'm using "Microsoft Visual C++.Net".
Anyways the number is "00458927" or "458927" does not really matter. I just want to add "0x" infront of the number and store the number into an int. So it'll look like "0x00458927" or "0x458927".
Thank you for all your Comment's.
Anyways the number is "00458927" or "458927" does not really matter. I just want to add "0x" infront of the number and store the number into an int. So it'll look like "0x00458927" or "0x458927".
Thank you for all your Comment's.
>>>I'm sorry but I'm using "Microsoft Visual C++.Net".
OK, but which language are you using. classical C+ or C++.net ?
(Visual C++.net supports both)
OK, but which language are you using. classical C+ or C++.net ?
(Visual C++.net supports both)
ASKER
C++.net
"458927" != "0x458927" so I'm not sure what you're trying to do. Either one can be assigned to a 'long' or 32-bit 'int'. But they are not the same values.
Int64 l = 458927;
String s = String.Format("0x{0}", l);
s is now "0x458927"
ASKER
The number is a hex number already so it can be "45f5e0" but I want to make into "0x45f5e0" and store it to an int. The number i provided before was just a bad example.
Then the question is how or where are you getting the number (string?) "45f5e0" in the first place? What format? That would determine how to convert it to what you want. There is probably nothing magic about this but the details determine what needs to be done.
ASKER
the number is an "unsigned long" which is stored into an int, but I want to store it again into an int but with "0x" infront of it.
>> AndyAinscow: Still haven't checked your method yet..
>> AndyAinscow: Still haven't checked your method yet..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the number is an "unsigned long" which is stored into an int, but I want to store it again into an int but with "0x" in front of it.
The 'int' will be a binary int and with that value, it must be a 32-bit int. The '0x' is only used to indicate that it is HEX value when you assign it, it will Not be part of the 'int' which is binary. You must be getting the "45f5e0" from a printf() or sprintf() with a specifier for HEX output.
The 'int' will be a binary int and with that value, it must be a 32-bit int. The '0x' is only used to indicate that it is HEX value when you assign it, it will Not be part of the 'int' which is binary. You must be getting the "45f5e0" from a printf() or sprintf() with a specifier for HEX output.
ASKER
Actually what AndyAinscow posted is exactly what im looking for, but it stores the output into String, I need it to be stored into an int.
That's because the '0x' will only exist when it is displayed as a string. It does Not exist in the int because the int is binary.
ASKER
ahh so how can I store it thats not a String?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay I now understand. Thank you AndyAinscow and DaveBaldwin.
But now i ran into another question, not sure if i should make another thread but Im going to anyways.
But now i ran into another question, not sure if i should make another thread but Im going to anyways.
use std::stringstream
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer
http://stackoverflow.com/questions/3381614/c-convert-string-to-hexadecimal-and-vice-versa