Jump to content
  • 0

bad argument #2 to 'string.format' (string: long expected, got string) (field 'format')


Platonic
 Share

Question

Got error: bad argument #2 to 'string.format' (string: long expected, got string) (field 'format')

image.thumb.png.9e2ccce07ecd25206d627127d5cf1e17.png

I can't understand the cause of error from the surface. Because the same code that i am using works on 32 bit. Quite convinced that in deeper level something is happening with the length of the values. Here is the code that causes the error:

    a = gg.getValues(a)
    teser = {}
    for i, v in ipairs(a) do
  --[[
      supper = 
      {
        address = assembly[1].address, 
        addressConvertToDecimal = string.format(tonumber(assembly[1].address)), 
        valueToCompare = v.value, 
      }
      print(supper)
  ]]
      v.value = string.format("%x", "0x".. v.value + 100000000), -1
      if (v.value == assembly[1].address) then
        teser[#teser + 1] = v
      end
    end

Normally on 32 bit i need to convert the values of a sub table to hex. And then everything works fine with no error.
But here on 64 bit it doesn't work as seen in the error above. But script works fine when this part of the code is removed:

      v.value = string.format("%x", "0x".. v.value + hexConvert), -1

Tried like printing out some info but i can't really see anything wrong from the surface level. Everything seems to print out fine. Here is example from the 64bit with no issues after removing that part of the code:

Script ended:
{ -- table(80b13c5)
	['address'] = 0x7f1bc4ba1a90, -- address of a specifc sub table of result list
	['addressConvertToDecimal'] = '139757241375376', -- specifc address converted
	['valueToCompare'] = 139757049444224, -- value from a sub table of a result list, must be compared with converted address
}
{ -- table(8d4f67d)
	['address'] = 0x7f1bc4ba1a90,
	['addressConvertToDecimal'] = '139757241375376',
	['valueToCompare'] = 139757241375376,
}
{ -- table(7753972)
	['address'] = 0x7f1bc4ba1a90,
	['addressConvertToDecimal'] = '139757241375376',
	['valueToCompare'] = 0,
}

On 32 bit i need to convert these signed values otherwise comparison of an address and a value does not work because the bits of the value are signed while the value that represent address is shown unsigned.
what i find strange to is that 64 bit does not need that conversion and the values are shown automatically as signed long.., which is a bit confusing for me. Are values of data type long not allowed to be unsigned? some math explanation would be great.

 

Edited by Platonic
fixing some mistakes in the writhing
Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
1 hour ago, Platonic said:
v.value = string.format("%x", "0x".. v.value + hexConvert), -1

Here you are doing concatenation of a number with string "0x" this will result a string because that what concatenation do, and since the function string.format are expecting an integer to be passed as argument this will result an error.

Actually you don't need to convert hex value to integer since Lua do it automatically you can try this 

local x = 0xA
print (x) --> 10

Except if your value is a string no conversion will be done 

local x = "0xA"
print (x) --> "0xA"

If you have a hex value as a string you will need to use the function tonumber instead and he doesn't require the "0x" before 

local x = "A"

print (tonumber(x, 16)) --> 10

 

Link to comment
Share on other sites

  • 0

Uh Never mind. Error makes sense. Java site is so friendly for p rovides some basic information about data types:

image.thumb.png.e7ff4e1d4ec3d16f0212c723efd81992.png

 

139757241375376 is still in the range of signed long. Value did not need to be converted to begin with. string.format expected data type long or . But based on conversion method used it did not receive a long at all. Not sure why it instead says that it receives a string. The value must have come out as a normal integer resulting in it to be a string i think.

printing this gives me the type string as well:

value = string.format("%x", 154775807)
print(value)
print(type(value))
print(string.len(value))


-- result
Script ended:
939b0ff
string
7

perhaps if it does not recognize it as a number it will read it as a string by default. Not sure.
 

Just now, MAARS said:

Here you are doing concatenation of a number with string "0x" this will result a string because that what concatenation do, and since the function string.format are expecting an integer to be passed as argument this will result an error.

Actually you don't need to convert hex value to integer since Lua do it automatically you can try this 

local x = 0xA
print (x) --> 10

Except if your value is a string no conversion will be done 

local x = "0xA"
print (x) --> "0xA"

If you have a hex value as a string you will need to use the function tonumber instead and he doesn't require the "0x" before 

local x = "A"

print (tonumber(x, 16)) --> 10

 

I guess we sended around same time, so i missed on the information

Link to comment
Share on other sites

  • 0

string.format always return a string, if you have formated your integer as hexadecimal If you want to retrieve is value as Integer then use the function tonumber and second argument as 16

Edited by MAARS
Link to comment
Share on other sites

  • 0
4 minutes ago, MAARS said:

Actually you don't need to convert hex value to integer since Lua do it automatically you can try this 

Problem with Lua is that it converts it to signed integer by default. while i actually need unsigned integer, from there the conversion to hex. Because it does read Hex as unsigned integer.

or at least that is what is happening on GG.

Link to comment
Share on other sites

  • 0
1 hour ago, MAARS said:

But signed integer allow you to represent both, negative and positive number ? Why would you want only positive number ?

Yes but only to the ranges based on their data types. I am not sure if this is a gameguardian or a Lua concept. But when you print out an address it will be printed as an unsigned integer. And when you print a value it will be printed as a signed integer.
As you can see in the script. It has to check if the values of "address" and "value" are the same. If so there tables must be added to the empty table named "teser".

But because of address being unsigned integer and value being signed integer this comparison is can not work. Here is the example (without conversion):

Script ended:
value: 385877511 	address: 2202972824
value: 0 	address: 2202972824
value: -2091994472 	address: 2202972824 -- you can see that "value" is in signed integer and address is unsigned integer. causing comparison to fail.
value: -2091994472 	address: 2202972824
value: 0 	address: 2202972824
value: 0 	address: 2202972824

You can see that i am only using 1 address so i don't do changes to it. Instead i convert the value to hex so that Lua or gameguardian(not sure which one) reads it instead as a unsigned integer, then comparison works:

Script ended:
value: 4680844807 	address: 2202972824
value: 4294967296 	address: 2202972824
value: 2202972824 	address: 2202972824 -- both value and address are now unsigned integers and comparison will work.
value: 2202972824 	address: 2202972824
value: 4294967296 	address: 2202972824
value: 4294967296 	address: 2202972824

The script interrupted. [1]

 

Edited by Platonic
Link to comment
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
 Share

×
×
  • 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.