Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Hex number..

Posted on 2012-09-19
Medium Priority
535 Views
What I want to do is make a number which is "00458927" into "0x00458927" and store the number into an int.
0
Question by:RevJoker
• 7
• 6
• 4
• +2

LVL 15

Expert Comment

ID: 38416679
Do you want to convert hexa string to int?

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
0

LVL 84

Expert Comment

ID: 38416682
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.
0

LVL 45

Expert Comment

ID: 38416710
You have used C++.net and C++ as tags.
Which language do you want?  C++.net or C++
0

LVL 16

Expert Comment

ID: 38417837
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
}
0

LVL 1

Author Comment

ID: 38419125
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.
0

LVL 45

Expert Comment

ID: 38419153
>>>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)
0

LVL 1

Author Comment

ID: 38419158
C++.net
0

LVL 84

Expert Comment

ID: 38419215
"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.
0

LVL 45

Expert Comment

ID: 38419502
``````	Int64 l = 458927;
String s = String.Format("0x{0}", l);
``````
s is now "0x458927"
0

LVL 1

Author Comment

ID: 38419508
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.
0

LVL 84

Expert Comment

ID: 38419559
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.
0

LVL 1

Author Comment

ID: 38419632
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..
0

LVL 45

Accepted Solution

AndyAinscow earned 1050 total points
ID: 38419840
"45f5e0" is a string NOT a numerical value (but the code I posted will still work).

String^ l = "45f5e0";
String^ s = String.Format("0x{0}", l);

ps.  for some reason copying the ^ character didn't work correctly.  The earlier comment isn't quite correct in that it doesn't copy/paste correctly.  The above should be OK.
0

LVL 84

Expert Comment

ID: 38419940
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.
0

LVL 1

Author Comment

ID: 38420001
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.
0

LVL 84

Expert Comment

ID: 38420033
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.
0

LVL 1

Author Comment

ID: 38420058
ahh so how can I store it thats not a String?
0

LVL 84

Assisted Solution

Dave Baldwin earned 450 total points
ID: 38420105
You already have it stored so it is not a string.  The '0x' has nothing to do with the int value.  It only is used to show that the display is in HEX and not decimal.  Here is an extremely simple example. Decimal 16 is the same value as HEX 10 or 0x10.  This short program will show you that the actual values are the same because when you subtract them, you will get 0.

``````// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
int value1;
int value2;
int result;

// process:
value1 = 16;
value2 = 0x10;
result = value1 - value2;

// print out the result:
cout << result;

// terminate the program:
return 0;
}
``````

http://www.cplusplus.com/doc/hex/
http://www.cplusplus.com/doc/tutorial/variables/
0

LVL 1

Author Comment

ID: 38420180
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.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll