Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QB64 Phoenix Edition v3.11.0 Released!
#35
So hey, here's to qb64 doing some true second guessing. Going from decimal to hex:

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
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.
Reply


Messages In This Thread
QB64 Phoenix Edition v3.11.0 Released! - by a740g - 01-03-2024, 05:45 PM
RE: QB64 Phoenix Edition v3.11.0 Released! - by bert22306 - 01-13-2024, 04:10 AM



Users browsing this thread: 1 Guest(s)