Tuesday, 2025-07-01, 6:25 AM
Welcome Guest

Blog

Main » 2011 » April » 21 » [Lib] Dec2Hex
3:12 PM
[Lib] Dec2Hex
Introduction:
Converting Decimal to Hexadecimal is easy with AutoHotkey, doing that again and again makes your script become longer. So I made a function to make it simpler.

The Code:

Code
; Dec2Hex.ahk

Dec2Hex(Var){
; Convert a decimal integer to hexadecimal:
SetFormat, IntegerFast, hex
Dec2Hex += Var ; Sets Dec2Hex (which previously contained 11) to be 0xb.
Dec2Hex .= "" ; Necessary due to the "fast" mode.
SetFormat, IntegerFast, d
StringTrimLeft, Dec2Hex, Dec2Hex, 2 ; removes 0x from the string
StringUpper, Dec2Hex, Dec2Hex ; makes it all caps
sDec2Hex .= Dec2Hex
return sDec2Hex
}

How to Use:
- Move this to your lib directory (i.e. C:\Program Files\AutoHotkey\lib)
- or Add this to your script
- Converting decimal to hex would be like this

Code
% Dec2Hex(Var)

Example:

Code
MsgBox, % Dec2Hex(1000)

Dec2Hex(Var){
; Convert a decimal integer to hexadecimal:
SetFormat, IntegerFast, hex
Dec2Hex += Var ; Sets Dec2Hex (which previously contained 11) to be 0xb.
Dec2Hex .= "" ; Necessary due to the "fast" mode.
SetFormat, IntegerFast, d
StringTrimLeft, Dec2Hex, Dec2Hex, 2
StringUpper, Dec2Hex, Dec2Hex
sDec2Hex .= Dec2Hex
return sDec2Hex
}

That will show a message box containing 3E8, the hex value of 1000.

For GUI Call:

Code
Gui, Add, Text, x6 y7 w50 h20 , Interger:
Gui, Add, Edit, x56 y7 w110 h20 +Center +number ,  
Gui, Add, Edit, x6 y57 w160 h20 +Center +ReadOnly ,  
Gui, Add, Button, x6 y27 w160 h30 , Convert
Gui, Show,, Dec2Hex
return

GuiClose:
ExitApp

ButtonConvert:
GuiControlGet, IntergerToConvert,, Edit1
GuiControl, Text, Edit2, % Dec2Hex(IntergerToConvert)
return

Dec2Hex(Var){
; Convert a decimal integer to hexadecimal:
SetFormat, IntegerFast, hex
Dec2Hex += Var ; Sets Dec2Hex (which previously contained 11) to be 0xb.
Dec2Hex .= "" ; Necessary due to the "fast" mode.
SetFormat, IntegerFast, d
StringTrimLeft, Dec2Hex, Dec2Hex, 2
StringUpper, Dec2Hex, Dec2Hex
sDec2Hex .= Dec2Hex
return sDec2Hex
}

Just experiment with the example scripts above.

Views: 13626 | Added by: GarenaZero | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *: