| | |
converting numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 78
Reputation:
Solved Threads: 0
Hi I have the following string of hex '38 55 FD FF'. Somehow i need to get the value -174792 from it. I think it involves reversing the bits to 'FF FD 55 38' then showing its binary representation and reversing all the bits so that 1's become 0's and 0's become 1's. then +1 to the value. I think this is the process but if im wrong please correct me, but i doo need to get that value from that hex string. Is it called a 32 bit int? anyway, I hope someone knows what i mean and hopefully someone can show me how to do this in C++?
thank you
thank you
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Flipping the bits and adding 1 has to do with the 2's complement representation of integers.
+174792 is this in 32 bit hexadecimal:
00 02 AA C8
You want negative -17492 so flipping the bits yields this:
FF FD 55 37
In 2's complement, after you flip the bits you add 1, so
FF FD 55 37
+ 00 00 00 01
----------------
FF FD 55 38
Reversing the byte order yields this:
38 55 FD FF
The above two numbers are the same, just the first one is "big endian" and the second one is in "little endian" (I THINK I don't have them backwards).
So it looks like you need to ask the user to input a 32 bit hexadecimal number in little endian format. Your job is to convert that hexadecimal number into an integer. Below are links to wikipedia for big endian/little endian and for 2's complement, which is how integers are stored in computers:
http://en.wikipedia.org/wiki/Endianness
http://en.wikipedia.org/wiki/2%27s_complement
+174792 is this in 32 bit hexadecimal:
00 02 AA C8
You want negative -17492 so flipping the bits yields this:
FF FD 55 37
In 2's complement, after you flip the bits you add 1, so
FF FD 55 37
+ 00 00 00 01
----------------
FF FD 55 38
Reversing the byte order yields this:
38 55 FD FF
The above two numbers are the same, just the first one is "big endian" and the second one is in "little endian" (I THINK I don't have them backwards).
So it looks like you need to ask the user to input a 32 bit hexadecimal number in little endian format. Your job is to convert that hexadecimal number into an integer. Below are links to wikipedia for big endian/little endian and for 2's complement, which is how integers are stored in computers:
http://en.wikipedia.org/wiki/Endianness
http://en.wikipedia.org/wiki/2%27s_complement
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Here's a link to XOR (exclusive or). Take two bits. If one of the bits, but not both of the bits are equal to 1, then the XOR is true (1). Otherwise, if both bits are 0 or both are 1, the XOR of the two bits is false (0). So the XOR of 0101 and 1100 would be 1001.
http://en.wikipedia.org/wiki/XOR
The "bitwise XOR" operator in C++ is denoted by the carrot symbol: ^
I think all XOR is bitwise. At any rate, I'm referring to the bitwise XOR in this post.
So if you have two integers (or two of anything) and you wanted to take the XOR of them and store them in integer 3, you could do something like this:
Doing a bitwise XOR of the original number and 0xFFFFFFFF is a way to flip the bits.
http://en.wikipedia.org/wiki/XOR
The "bitwise XOR" operator in C++ is denoted by the carrot symbol: ^
I think all XOR is bitwise. At any rate, I'm referring to the bitwise XOR in this post.
So if you have two integers (or two of anything) and you wanted to take the XOR of them and store them in integer 3, you could do something like this:
C++ Syntax (Toggle Plain Text)
int int1, int2, int3; // miscellaneous code int3 = int1^int2; // assign int3 to be bitwise XOR of num1 and num2
Doing a bitwise XOR of the original number and 0xFFFFFFFF is a way to flip the bits.
•
•
Join Date: Nov 2007
Posts: 78
Reputation:
Solved Threads: 0
hi i have tested what you advised me. I used the string 7A 72 FC FF. I tranformed them to little endian (FF FC 72 7A). then preformed a bitwise xOR on them and got the value 14465896. However i should have got the value -232838. Im not sure how im getting that value. I've attached my code below , Can anyone see what im doing wrong?
C++ Syntax (Toggle Plain Text)
unsigned char long1 = '0xFF'; unsigned char long2 = '0xFF'; unsigned char long3 = '0xFF'; unsigned char long4 = '0xFF'; int long5 = long1 + long2 + long3 + long4; int proper_longitude; proper_longitude = longitude^long5;
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
C++ Syntax (Toggle Plain Text)
unsigned char long1 = '0xFF'; unsigned char long2 = '0xFF'; unsigned char long3 = '0xFF'; unsigned char long4 = '0xFF'; int long5 = long1 + long2 + long3 + long4; int proper_longitude; proper_longitude = longitude^long5;
C++ Syntax (Toggle Plain Text)
unsigned int long5 = 0xFFFFFFFF;
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
It takes some getting used to if this is brand new to you. Before trying to code much I would recommend checking out the links I gave you and learning how integers versus unsigned integers are stored in memory. Lots of resources on integer representation, 2's complement, and XOR on google. Below is a little program I wrote to sort of compare how different numbers are displayed in memory. Using a debugger that shows thing in Hex base or a calculator that can go back and forth from base 10 to hex helps a lot. Play around with it, put in different hex numbers, reverse the bits, etc. Work a few out on paper, then verify on the computer. The thing to know about integers is that they span the range from -2^31 through 2^31 - 1. Unsigned integers span from 0 to 2^32 -1. They are represented the same way from 0 through 2^31 - 1. In an integer, if the most significant bit is 1, the number is negative. If it's 0, the number is 0 or positive.
Hope this helps.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int num1 = 0xFFFD5538; cout << "0xFFFD5538 stored as an int is " << num1 << endl; unsigned int num2 = 0xFFFD5538; cout << "0xFFFD5538 stored as an unsigned int is " << num2 << endl; int num3 = 0x7FFFFFFF; cout << "0x7FFFFFFF stored as an int is " << num3 << endl; unsigned int num4 = 0x7FFFFFFF; cout << "0x7FFFFFFF stored as an int is " << num4 << endl; int num5 = 0x12345678; cout << "0x12345678 stored as an int is " << num5 << endl; int num6 = 0x12345678^0xFFFFFFFF; cout << "0x12345678 with its bits flipped is " << num6 << endl; cout << "Compare the last two numbers. The second is the negative of " << "the first, but off by 1. That's why you add 1 after flipping bits." << endl; int num8 = 0xEDCBA987; cout << "0xEDCBA987 stored as an int is " << num8 << endl; cout << "the last two numbers should be equal." << endl; return 0; }
Hope this helps.
•
•
Join Date: Nov 2007
Posts: 78
Reputation:
Solved Threads: 0
i think ive narrowed it down to something that is going wrong in my code earlier on to do with the string '7A 72 FC FF'. In little endian, when transformed to decimal on a calculator is gives 4294734458 which is what i would expect. However when i tested my prog it stores it as 16547834 when converted to decimal. I have used the same method before and it works fine which lead me to believe i think something is going wrong when it comes to storing 'FF'. I have attached my code below for storing the string as an integer and i should work as it has done in the past but i just dont understand why its not giving me the right variable
On a test print of longitude it gives 16547834 but its true value should be 4294734458. If the value were 4294734458 then all the thing you told me work perfectly!!(...thanks by the way.) So i believe my problem lies in this part. Am i right in thinking this??
C++ Syntax (Toggle Plain Text)
infile.read (&bytes1,sizeof (bytes1)) ; // read in byte 0x7A infile.read (&bytes2,sizeof (bytes2)) ; //read in byte 0x72 infile.read (&bytes3,sizeof (bytes3)) ; //read in byte 0xFC infile.read (&bytes4,sizeof (bytes4)) ; //read in byte 0xFF int longitude; longitude = (bytes4 << 4) | (bytes3 << 16) | (bytes2 << 8) | bytes1 ;
On a test print of longitude it gives 16547834 but its true value should be 4294734458. If the value were 4294734458 then all the thing you told me work perfectly!!(...thanks by the way.) So i believe my problem lies in this part. Am i right in thinking this??
![]() |
Similar Threads
- Converting numbers to text equivalent (C#)
- Numbers in Turbo C++ (C++)
- need help converting numbers to words (VB.NET)
- Converting numbers to words (C++)
- How many different numbers are there? (C++)
- Converting a char array into numbers (C++)
- Sorting Video titles using a ID numbers (Java)
- Converting Hexadecimal characters to integers. (C++)
- text to numbers, numbers to texrt (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: classes and inheritance problems
- Next Thread: e-mail class
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






