I'm making a game that gets some information from an API that returns in hex format. The only problem is, it's really long strings. using Godot's builtin .hex_to_int() only works for shorter strings. But if I put something like "0x2b5bbe05641850c00" it will always return the same number. Anyone know a better way?

What do you need these numbers for? It looks like that for some strange reason hex_to_int()'s output is limited to unsigned 32 bit integers, while Godot's ints are signed 64 bits (63 bits + a sign bit). The number you posted is also 64 bit. You could make a custom conversion function but you'll need to decide what to do with the sign bit.

That hex value is 17 digits (68 bits). Are you sure that it's supposed to be a number? Do you have to perform calculations with it? If not, you could keep it as a string, or convert it to an Array or PoolByteArray.

@xyz said: What do you need these numbers for? It looks like that for some strange reason hex_to_int()'s output is limited to unsigned 32 bit integers, while Godot's ints are signed 64 bits (63 bits + a sign bit). The number you posted is also 64 bit. You could make a custom conversion function but you'll need to decide what to do with the sign bit.

I'm trying to make a game through the Ethereum blockchain. The basis is essentially you'll connect your account and then the API I use gets the account balance but it only sends it in hex format. So it's kinda essential that I figure it out to the game.

I could try to find another API for it. But I also kinda just wanna be able to figure this out.

@DaveTheCoder said: That hex value is 17 digits (68 bits). Are you sure that it's supposed to be a number? Do you have to perform calculations with it? If not, you could keep it as a string, or convert it to an Array or PoolByteArray.

Yes. The API call is the balance of an address, the API returns the whole amount in hex. Here's the documentation for it with another long sample hex number.

You won't be able to fit such large numbers into Godot's int type to perform calculations.

@xyz said: You won't be able to fit such large numbers into Godot's int type to perform calculations.

oh, what is Godot's maximum size? how would I be able to approach this then?

@Schleckenmiester said: Yes. The API call is the balance of an address, the API returns the whole amount in hex. Here's the documentation for it with another long sample hex number.

That page calls that value an address, and it's used in an HTTP request. I don't see why you can't keep it as a string.

As I said already, GDScript's int is signed 64 bit - 63 bits for value and 1 bit for the sign.

It really depends on what you want to do and what's the size limit of numbers you expect to encounter. If you only need to do basic addition/subtraction, you could write a custom class that does that digit by digit. If you need extensive calculation I don't know an easy way to do it in GDScript. However you can use C# which has a class for handling integers of indefinite size. Third option is to go with native scripting in C or C++ and use boost::multiprecision which can handle large integers, up to 1024 bits I think. There are probably other libraries that can do it as well.

@DaveTheCoder said:

@Schleckenmiester said: Yes. The API call is the balance of an address, the API returns the whole amount in hex. Here's the documentation for it with another long sample hex number.

That page calls that value an address, and it's used in an HTTP request. I don't see why you can't keep it as a string.

So, I need the game and the user to know their balance in a number format. People don't operate in hex, so if they see their balance is "0xdeadbeef" they're not gonna know how much they have.

I've been thinking about it, and it appears Godot can interact with JS. I'm planning this to be a browser game too. So, I can just offload the hex numbers into the JS, then I've tested it already and JS doesn't seem to have a limit. After it's converted I can get a shortened integer from the JS side of things.

@xyz said: As I said it already, GDScript's int is signed 64 bit - 63 bits for value and 1 bit for the sign.

It really depends on what you want to do and what's the size limit of numbers you expect to encounter. If you only need to do basic addition/subtraction, you could write a custom class that does that digit by digit. If you need extensive calculation I don't know an easy way to do it in GDScript. However you can use C# which has a class for handling integers of indefinite size. Third option is to go with native scripting in C or C++ and use boost::multiprecision which can handle large integers, up to 1024 bits I think. There are probably other libraries that can do it as well.

That's alright, I don't need it exclusively done in GDScript, it's just easier. But I'm up for a challenge. I'll look into those. I know some C# so I'll start with that.

a year later