Jump to content
  • 0

Converting decimal to hex


n3kitOzz

Question

Im trying to convert dec to signed hex with

string.format("%X", tostring(number))

But, tested it in lua interpreter, and have this:

8EkfaEf.png

But, i need to convert it to signed hex, like that:
RaMnXH4.png

How to do that?

string.format("%X", tostring(number))

returning a FFFFFFFFFFFFFFBF

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

FFFFFFFFFFFFFFFFFBF is actually -65 but in 64 bits. 

Two's Complement

Let's simplify everything and workout with -1. I will explain this in binary with 2's complement in 4 bits signed integer.

We know that 1 in binary is 0001 (Binary formed). Now we would like to negate 1, to negate 1 into 2's complement, we have to perform two steps.

1. Invert every single bit

2. Add 1

0001 --> 1

1110 --> Inverted all bit

1111 --> +1 And now we have -1 in 2's complement.

 

Let's do more example but now in 64 bits. 

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 -- > This is 64 bits long integer. With 2's complement, we will have upper limit 9.223372e+18 and lower limit -9.223372e+18.

But we are human, we do not like this kind long messy numbering system. Hence, we use Hexadecimal for a better view.

0000 (Binary) = 0 (Hexadecimal)

So the long 64 bits binary is actually 0000000000000000 in hexadecimal.

65 is actually 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 0001. Which is 0000000000003639 in hexadecimal.

By doing the first step I mentioned just now, we have 

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1101 1110

now we add a 1

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1101 1111

By converting this into hexadecimal, we will get

FFFFFFFFFFFFFFFFFBF

 

In conclusion, FFFFFFFFFFFFFFFFFBF is -65 in 64bit and BF is -65 in 8 bits

Link to comment
Share on other sites

34 minutes ago, ItsSC said:

FFFFFFFFFFFFFFFFFBF is actually -65 but in 64 bits. 

Two's Complement

Let's simplify everything and workout with -1. I will explain this in binary with 2's complement in 4 bits signed integer.

We know that 1 in binary is 0001 (Binary formed). Now we would like to negate 1, to negate 1 into 2's complement, we have to perform two steps.

1. Invert every single bit

2. Add 1

0001 --> 1

1110 --> Inverted all bit

1111 --> +1 And now we have -1 in 2's complement.

 

Let's do more example but now in 64 bits. 

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 -- > This is 64 bits long integer. With 2's complement, we will have upper limit 9.223372e+18 and lower limit -9.223372e+18.

But we are human, we do not like this kind long messy numbering system. Hence, we use Hexadecimal for a better view.

0000 (Binary) = 0 (Hexadecimal)

So the long 64 bits binary is actually 0000000000000000 in hexadecimal.

65 is actually 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 0001. Which is 0000000000003639 in hexadecimal.

By doing the first step I mentioned just now, we have 

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1101 1110

now we add a 1

1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1101 1111

By converting this into hexadecimal, we will get

FFFFFFFFFFFFFFFFFBF

 

In conclusion, FFFFFFFFFFFFFFFFFBF is -65 in 64bit and BF is -65 in 8 bits

Thanks for your reply!

Im writing memory editor script, and this script show old and new data.
If number < 0, we have FFFFFFFFFFFFFFFFFBF, but in hex editors and in gg memory editor its simply show "BF".
I need to do it same way.

Link to comment
Share on other sites

1 hour ago, n3kitOzz said:

Thanks for your reply!

Im writing memory editor script, and this script show old and new data.
If number < 0, we have FFFFFFFFFFFFFFFFFBF, but in hex editors and in gg memory editor its simply show "BF".
I need to do it same way.

function dec2Hex(val)
	return val>=0 and string.format("%X", tonumber(val)) or string.format("%X", (val~ 0xffffffffffffffff <<((math.floor(math.log(math.abs(val))/math.log(10)) + 1) *4)))
end

Well... It took me a while to come with this solution. Hope it helps 🙂

 

In case anyone want to use and modify this function. Here is the explanation of how it works.

Explanation

Refer to

Converting decimal to hex (#31c26b3w)

for a better understanding.

 

Now we want to convert ffffffffffffffbf into bf. Obviously what we need to do here is eliminate the leading F. The easier approach here is using XOR to toggle the f back to 0. 

XOR operation

    FFFFFFFFFFFFFFBF

⊕ FFFFFFFFFFFFFF00

==================

                                  BF

We can look deep into each F, F = 1111 in binary form, 1111 ~ 1111 = 0000, because 1 ⊕ 1 = 0 , 0 ⊕0 = 0 and 1 ⊕ 0 = 1.

 

Now we have the concept, we can implement this into our programme. To make a function, obviously we do not know what value the user will enter, so we leave "val" as parameter. 

val ~ 0xFFFFFFFFFFFFFFFF is to eliminate the leading F. Noticed that the calculation I used on top is FFFFFFFFFFFFFF00, which means that we need to left shifting the FFFFFFFFFFFFFFFF according to the number of digits of val.

For example: -165 must XOR with FFFFFFFFFFFFF000

-99999 must XOR with FFFFFFFFFFF00000

math.floor(math.log(math.abs(val))/math.log(10)) + 1)

The code here calculate the numbers of digits of a value. For example val = 100, you will get 3, val = 5000, you will get 4.

Next, getting the number of digits is not enough, it will cause a logical error if we use this to left shifting the 0xFFFFFFFFFFFFFFFF. Because F = 1111, if we need to left shifting to remove one F, we need left shifting 4 times to clear the four 1. 

Here is the example, 

1111<< 1 = 1110 which indicate E, and you will end up with

0xFFFFFFFFFFFFFFFE but not 0xFFFFFFFFFFFFFFFF. 

So I multiply 4 at the end. Combine with all these concepts, we will get the final result above.

Link to comment
Share on other sites

4 hours ago, ItsSC said:

function dec2Hex(val)
	return val>=0 and string.format("%X", tonumber(val)) or string.format("%X", (val~ 0xffffffffffffffff <<((math.floor(math.log(math.abs(val))/math.log(10)) + 1) *4)))
end

Well... It took me a while to come with this solution. Hope it helps 🙂

 

In case anyone want to use and modify this function. Here is the explanation of how it works.

Explanation

Refer to

Converting decimal to hex (#31c26b3w)

for a better understanding.

 

Now we want to convert ffffffffffffffbf into bf. Obviously what we need to do here is eliminate the leading F. The easier approach here is using XOR to toggle the f back to 0. 

XOR operation

    FFFFFFFFFFFFFFBF

⊕ FFFFFFFFFFFFFF00

==================

                                  BF

We can look deep into each F, F = 1111 in binary form, 1111 ~ 1111 = 0000, because 1 ⊕ 1 = 0 , 0 ⊕0 = 0 and 1 ⊕ 0 = 1.

 

Now we have the concept, we can implement this into our programme. To make a function, obviously we do not know what value the user will enter, so we leave "val" as parameter. 

val ~ 0xFFFFFFFFFFFFFFFF is to eliminate the leading F. Noticed that the calculation I used on top is FFFFFFFFFFFFFF00, which means that we need to left shifting the FFFFFFFFFFFFFFFF according to the number of digits of val.

For example: -165 must XOR with FFFFFFFFFFFFF000

-99999 must XOR with FFFFFFFFFFF00000


math.floor(math.log(math.abs(val))/math.log(10)) + 1)

The code here calculate the numbers of digits of a value. For example val = 100, you will get 3, val = 5000, you will get 4.

Next, getting the number of digits is not enough, it will cause a logical error if we use this to left shifting the 0xFFFFFFFFFFFFFFFF. Because F = 1111, if we need to left shifting to remove one F, we need left shifting 4 times to clear the four 1. 

Here is the example, 

1111<< 1 = 1110 which indicate E, and you will end up with

0xFFFFFFFFFFFFFFFE but not 0xFFFFFFFFFFFFFFFF. 

So I multiply 4 at the end. Combine with all these concepts, we will get the final result above.

Thanks, man, great job!

Link to comment
Share on other sites

13 minutes ago, n3kitOzz said:

Thanks, man, great job!

function dec2Hex(val)
  return val>=0 and string.format("%X", tonumber(val)) or string.format("%X", (val ~ 0xffffffffffffffff <<( string.len(string.format("%X",math.abs(val))) *4)))
end

I realize the first one not 100% working all the time. Here I improve the code, also to tell you something terrible. 

The use of Leading F is to indicate the sign of value. 

 

Try dec2Hex(1) and dec2Hex(-255). You will find both output = 1 because you had removed the leading sign bits. That's why it is better to keep the F there.

Link to comment
Share on other sites

5 hours ago, ItsSC said:

function dec2Hex(val)
  return val>=0 and string.format("%X", tonumber(val)) or string.format("%X", (val ~ 0xffffffffffffffff <<( string.len(string.format("%X",math.abs(val))) *4)))
end

I realize the first one not 100% working all the time. Here I improve the code, also to tell you something terrible. 

The use of Leading F is to indicate the sign of value. 

 

Try dec2Hex(1) and dec2Hex(-255). You will find both output = 1 because you had removed the leading sign bits. That's why it is better to keep the F there.

Some simplified for range -127 .. 127.

function dec2Hex(val)
	if val >= 0 and val < 16 then
		return string.format("0%X", tonumber(val))
	elseif val > 15 and val < 128 then
		return string.format("%X", tonumber(val))
	elseif val == 0 then
		return "00"
	elseif val < 0 and val > -128 then
	    return string.sub(string.format("%X", tostring(val)), 15)
	end
end

Thanks for help for all!

Link to comment
Share on other sites

When converting number to hex string, in most cases one would want to get a result with particular number of hexadecimal digits depending on the data type of the input. The following solution can be used to convert a number to hex string with specified count of digits: 

function convertToHexString(number, digits)
  if digits < 1 or digits > 16 then
    error("Number of hexadecimal digits in the resulting string has to be in range [1; 16]")
  end
  
  local mask = (1 << (digits * 4)) - 1
  local format = "%0" .. digits .. "X"
  return string.format(format, number & mask)
end

Some examples of using the function along with returned strings: 

convertToHexString(94, 2) -- "5E"
convertToHexString(-41, 2) -- "D7"
convertToHexString(3345, 4) -- "0D11"
convertToHexString(256787, 8) -- "0003EB13"
convertToHexString(-62268, 8) -- "FFFF0CC4"
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.