Hexadecimal to Decimal

Post your Gambas programming questions here.
Post Reply
User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Hexadecimal to Decimal

Post by cogier » Monday 15th February 2021 4:52pm

Print &HFFF Returns 4095
Print &HFFFF Returns -1 not 65535
Print &HFFFFF Returns 1048575

Am I missing something here?

User avatar
stevedee
Posts: 367
Joined: Monday 20th March 2017 6:06pm

Re: Hexadecimal to Decimal

Post by stevedee » Monday 15th February 2021 5:03pm

cogier wrote:
Monday 15th February 2021 4:52pm
Print &HFFF Returns 4095
Print &HFFFF Returns -1 not 65535
Print &HFFFFF Returns 1048575

Am I missing something here?
Try:-
Print &H00FFFF (Returns 65535)

User avatar
PJBlack
Posts: 96
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Hexadecimal to Decimal

Post by PJBlack » Monday 15th February 2021 5:05pm

Byte 0 … 255 1 Byte
Short -32.768 … +32.767 2 Bytes
Integer -2.147.483.648 … +2.147.483.647 4 Bytes
Long -9.223.372.036.854.775.808 … +9.223.372.036.854.775.807 8 Bytes

Print &HFFF nearest fitting data type = short -> Returns 4095
Print &HFFFF nearest fitting data type = short -> Returns -1 (which is correct)
Print &HFFFFF nearest fitting data type = integer -> Returns 1048575

User avatar
stevedee
Posts: 367
Joined: Monday 20th March 2017 6:06pm

Re: Hexadecimal to Decimal

Post by stevedee » Monday 15th February 2021 5:14pm

Yes, I think its a casting problem. Adding a trailing "&" seems to sort it out:-
&hffff&

User avatar
cogier
Site Admin
Posts: 637
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Hexadecimal to Decimal

Post by cogier » Monday 15th February 2021 5:20pm

Thanks, that extra '00' works but why?

Post Reply