So hey, here's to qb64 doing some true second guessing. Going from decimal to hex:
The program:
And the results:
-2 to 8-bit hex: FE
-2 to 16-bit hex: FFFE
-2 to 24-bit hex: FFFFFE
254 to 8-bit hex: FE
65534 to 16-bit hex: FFFE
16777214 to 24-bit hex: FFFFFE
Moral of the story: It's clever enough to figure out if I'm trying to use signed hex or unsigned hex. Weird, man!
PS. Thinking about it, without having to explicitly state "signed" or "unsigned," this is probably the best default. All you have to do here is limit the input + values, when using signed.
The program:
Code: (Select All)
_Title "Decimal to hex with negative and positive numbers"
Screen _NewImage(120, 43, 0)
Color 1, 7
Cls
decimal1 = -2
hex1$ = Right$("00" + Hex$(decimal1), 2)
Print "-2 to 8-bit hex: "; hex1$
Print
hex2$ = Right$("0000" + Hex$(decimal1), 4)
Print "-2 to 16-bit hex: "; hex2$
Print
hex3$ = Right$("000000" + Hex$(decimal1), 6)
Print "-2 to 24-bit hex: "; hex3$
Print
decimal1 = 254
hex1$ = Right$("00" + Hex$(decimal1), 2)
Print "254 to 8-bit hex: "; hex1$
Print
decimal1 = 65534
hex2$ = Right$("0000" + Hex$(decimal1), 4)
Print "65534 to 16-bit hex: "; hex2$
Print
decimal1 = 16777214
hex3$ = Right$("000000" + Hex$(decimal1), 6)
Print "16777215 to 24-bit hex: "; hex3$
End
-2 to 8-bit hex: FE
-2 to 16-bit hex: FFFE
-2 to 24-bit hex: FFFFFE
254 to 8-bit hex: FE
65534 to 16-bit hex: FFFE
16777214 to 24-bit hex: FFFFFE
Moral of the story: It's clever enough to figure out if I'm trying to use signed hex or unsigned hex. Weird, man!
PS. Thinking about it, without having to explicitly state "signed" or "unsigned," this is probably the best default. All you have to do here is limit the input + values, when using signed.