# Decimal from signed 2's complement

I am having a really difficult time figuring this out.

I am pulling data from a device that produces values both positive and negative, the data is output in hex.

I use a function like this to convert the hex into decimal:

return msg;

this works just fine when the numbers are positive for example 0x00000f = 15, but when the numbers are negative values - the hex to decimal changes to something like 0xfffffff1 = 4294967281.

Now I know this is because of a decimal from signed 2's complement (because the hex converter I found online said so), and shows the value -15.

I tried looking how to calculate this value correctly to give me a negative number - I saw an answer online that said to write the function like this:

return msg;

However that just returns the value 1

I am in way too deep to figure this out on my own....so I'm hoping someone can help me out.

thanks.

I just figured it out. My function was wrong.

return msg;

now injecting 0xfffffff1 yields a value of -15...

A couple of points. Firstly just making sure that you realise that the //==-3 is just a comment, presumably related to the example you took this from. In javascript anything on a line after // is ignored. So your code is identical to (and slightly less confusing) written as
It seems initially odd that this does anything at all as the ~ operator is the bitwise NOT operator which flips all 1s to 0 and all 0s to 1. So ~~ flips them and then flips them back again, the net result of which is nothing when you look at the bit pattern. However javascript defines the result of a bitwise logic operation as being a signed 32 bit integer, so you are starting off with a bit pattern that is interpreted as a positive integer, but after flipping the bits and flipping them back again javascript interprets it as signed, giving the result you want.
It is worth noting that this will only work with 32 bit values, so for a negative numbers the value you start with must be 8 hex characters long.
I am interested in knowing what you actually start with in your calculations as it is possible there is a better way. Does your device give you a string of the forrm "0xfffffff1" or is it "fffffff1" or is it a buffer or what?

The output from the device is a string 0xffffffff starting at negative one, then counting backwards 0xfffffffe -2 etc. Positive numbers are normal 0x00000001 and start counting up.

That first example I found didn't really explain itself as to what exact it was doing - thank you for explaining the tildes function, I did understand the comment marks but wasn't quite sure what the reference to "-3" was (hoped someone could explain that).

I'm really really new to programming and I'm just learning as I go. I still am lost with the 2's compliment and the math to make 4294967295 in to -1.

OK, if you are starting with a string then you do need the parseInt(). In fact you can use