Jump to content
Sign in to follow this  
n3kitOzz

Converting decimal to hex

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Edited by ItsSC
Add on explanation

Share this post


Link to post
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!

Edited by n3kitOzz

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
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"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • By Crystal_Mods100x
      Hey guys long time i haven't posted a guide in a long time but here it is!
      today ill teach you how to create a game guardian script with multiple protections and make it look awesome!
      we are going to add in the script:
      *A password (could be cracked with a password cracker so multi passwords could be usefull instead)
      *Rename blocker (this was made by: @TopGEOYT) this gets the file so we can check if  they changed the file name! 
      *Encryption Tool (use any encryption tool you would like but i reccomend you not use the simple XOR Encrypt program)
      *Adding Emojis (this makes it more awesome and gives it some art! we don't want it to look boring)
      You may add more to it im not stopping ya 🙂
      --main script --Version: 1.0 --Coder: yourCoderName --Game: Gangstar City --Description: Gangster City script, contains fire rate, unlimited ammo, super jump and more hack options :) script_file_name = gg.getFile():match('^/]+$') file_name = "main.lua" if script_file_name == file_name then gg.alert("Verifcation: Passed") else gg.alert("Verication: Failed") os.exit() end ------------------------------------- function LAUNCH() menu = gg.choice({ "Super Jump v1.0", "Aimbot", "Weapon Fire rate" "Exit" },nil,"put any text here") if menu == 1 then end if menu == 2 then end if menu == 3 then end if menu == 4 then end end myPassword = "2021" input = gg.prompt({"ENTER PASSWORD"}, {[1] = '0'}, {[1] = 'number'}) if input[1] == nil or not myPassword then gg.alert("Wrong Password") os.exit() else LAUNCH() end first this will check the file then prompt the user to the password then the menu starts!
      All ya need is your values and then your encryption tool then your emojis then your finished! 🙂
      if you have any questions reply down below and ill be there soon!
    • By leonkappo
      View File Sky Children of Light
      Teleport, infinite energy, invisible cape Home party, blue color
      telegram group: https://t.me/joinchat/W4KLRx6ntl04ODJh
       
      Submitter leonkappo Submitted 04/10/2021 Category LUA scripts  
    • By leonkappo
      Teleport, infinite energy, invisible cape Home party, blue color
      telegram group: https://t.me/joinchat/W4KLRx6ntl04ODJh
       
    • By BLaCpO
      View File 🛡️PUBG BYPASS🛡️.lua
      HI USER ,
      THIS IS A BYPASS FOR PUBG MOBILE LITE , PUBG MOBILE AND FOR PUBG KR ALSO .
      PLEASE DON'T CHANGE THE CREDITS 😔😔
      THANKS 😊
      Submitter BLaCpO Submitted 03/31/2021 Category PUBG  
    • By xmagisk00
      View File Sky Childern of the light v0.13.2 (164898)
      Enjoy
      Sky Childern new season.lua
      Submitter xmagisk00 Submitted 04/06/2021 Category LUA scripts  
×
×
  • Create New...