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

try looking up xor to switch the 0's to 1's and the 1's to 0's.
to rearrange the elements look up shift rigth shift left and rotate rigth and rotate left

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

thanks for your tips. I can get the user to input these numbers, they are values that have been searched for in a file. I think the way to progress would be an xOr statement, ive never herd of these are they hard to use - does anyone know of any good resources to research/see em in action?

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:

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.

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?

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;
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;

What you are doing here is declaring 4 characters and sticking a capital letter F in each of them. Characters can't store more than one character so the second F drops off. I'm not familiar with the "unsigned char" datatype. My compiler simply treated them as char types. 'F' in ASCII is 70, so it added 70 + 70 + 70 + 70 and got 280 and put that in long5. If you wanted all 1's in long5 to do an XOR with, there's your problem because 280 is not all 1's in memory. If you want to put all 1's into long5, perhaps this?

unsigned int long5 = 0xFFFFFFFF;

ive tried as you say and this time i get -16547835 instead of -232838. Im a bit confused, im sure im doing everything right i just dnt understand why im recieving such values!

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.

#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.

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

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??

Gack!!!!

Hex 38 55 FD FF already is -174792! Just reverse the bytes because of it's endian-ness. You don't need XORs, bits, anything like that.

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.

Because you performed a bitwise XOR. Just reverse the bytes and you have it -- IOW stop when you get to FF FC 72 7A!

I've fixed it!! my bad, i had a code typo :( Thanks VernonDozier your help has been much appreciated!!!!!!!

This article has been dead for over six months. Start a new discussion instead.