Question about AutoHotkey storing hex and decimal numbers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Question about AutoHotkey storing hex and decimal numbers

18 Dec 2018, 22:59

Code: Select all

foo := 15
bar := 0xF
baz := 0xf
MsgBox, , Title, %foo%  ; (A) shows 15
MsgBox, , Title, %bar%  ; (B) shows 0xF
MsgBox, , Title, %baz%  ; (C) shows 0xf

foo += 0x0
bar += 0x0
baz += 0x0
MsgBox, , Title, %foo%  ; (D) shows 15
MsgBox, , Title, %bar%  ; (E) shows 15
MsgBox, , Title, %baz%  ; (F) shows 15
Hello. I am just wondering. When AutoHotkey stores a number value to a variable, does it also store information about whether it is a hex or decimal? Or maybe it just uses strings under the hood? Or else I can't understand why (A) (B) (C) are different.

Also after the calculation, all numbers become decimal, as shown by (D) (E) (F). Are there ways to keep a number hex after calculation?
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Question about AutoHotkey storing hex and decimal numbers

19 Dec 2018, 02:11

Hallo,
https://autohotkey.com/docs/commands/SetFormat.htm

Code: Select all

SetFormat, IntegerFast, Hex
foo := 15
bar := 0xF
baz := 0xf
MsgBox, , Title, %foo%  ; (A) shows 15
MsgBox, , Title, %bar%  ; (B) shows 0xF
MsgBox, , Title, %baz%  ; (C) shows 0xf

foo += 0x0
bar += 0x0
baz += 0x0
MsgBox, , Title, %foo%  ; (D) shows 15
MsgBox, , Title, %bar%  ; (E) shows 15
MsgBox, , Title, %baz%  ; (F) shows 15
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Question about AutoHotkey storing hex and decimal numbers

19 Dec 2018, 02:31

- I suppose that in AHK v1, the variables store numeric *and* string data.
- However, in AHK v2 the situation is more logical. The numbers are stored and presented as decimal only, and if you ever want to view the numbers as hex, you need to use the Format function. (Note: AHK v2 does not have SetFormat.)
- I write my scripts in a forwards compatible way, so I use the Format function whatever I want a number to be displayed as hex (e.g. MsgBox/ToolTip/txt/clipboard output). Cheers.

Code: Select all

foo := 15
bar := 0xF
baz := 0xf

MsgBox(foo) ;15
MsgBox(bar) ;15
MsgBox(baz) ;15

MsgBox(foo) ;15
MsgBox(Format("0x{:X}", bar)) ;0xF
MsgBox(Format("0x{:x}", baz)) ;0xf

MsgBox(Format("{:#x}", baz)) ;0xf
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Re: Question about AutoHotkey storing hex and decimal numbers

19 Dec 2018, 13:01

Thanks for the helpful replies!
Sometimes I just feel there are strange things in the AHK language. I haven't used AHK v2 yet, may try it in the future. By now I'll consider using jeeswg's way :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, peter_ahk, Spawnova and 338 guests