Hex to Decimal Converter

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Hex to Decimal Converter

24 May 2014, 09:40

Code: Select all

/*
hexToDecimal(str)
Converts hexadecimal strings to decimal numbers.
Parameters:
    - str: The hex value to convert to decimal. Optionally can have the 0x prefix. Any characters that do not fit the range 0-9, a-z are removed beforehand.

Examples:
    MsgBox % hexToDecimal("0xfffff") ; 1048575
    ; Notice that the case doesn't matter.
    MsgBox % hexToDecimal("DEADBEEF") ; 3735928559
*/  

hexToDecimal(str){
    local newStr := ""
    static comp := {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, "a":10, "b":11, "c":12, "d":13, "e":14, "f":15}
    StringLower, str, str
    str := RegExReplace(str, "^0x|[^a-f0-9]+", "")
    Loop, % StrLen(str)
        newStr .= SubStr(str, (StrLen(str)-A_Index)+1, 1)
    newStr := StrSplit(newStr, "")
    local ret := 0
    for i,char in newStr
        ret += comp[char]*(16**(i-1))
    return ret
}
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Hex to Decimal Converter

24 May 2014, 12:42

I don't think we need to use RegEx here, ltrim should be enough.
Here is one that runs twice as fast ;)

Code: Select all

hexToDecimal(str){
    static _0:=0,_1:=1,_2:=2,_3:=3,_4:=4,_5:=5,_6:=6,_7:=7,_8:=8,_9:=9,_a:=10,_b:=11,_c:=12,_d:=13,_e:=14,_f:=15
    str:=ltrim(str,"0x `t`n`r"),   len := StrLen(str),  ret:=0
    Loop,Parse,str
      ret += _%A_LoopField%*(16**(len-A_Index))
    return ret
}
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hex to Decimal Converter

24 May 2014, 16:30

HotKeyIt wrote:Here is one that runs twice as fast ;)
Not written in pure AHK, but the fastest conversion I know of is this DllCall:

Code: Select all

;http://www.autohotkey.com/board/topic/18670-converting-numbers-to-any-base-radix-using-msvcrtdll/
BaseToDec(n, Base) {
	static U := A_IsUnicode ? "wcstoui64_l" : "strtoui64"
	return, DllCall("msvcrt\_" U, "Str",n, "Uint",0, "Int",Base, "CDECL Int64")
}

DecToBase(n, Base) {
	static U := A_IsUnicode ? "w" : "a"
	VarSetCapacity(S,65,0)
	DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
	return, S
}
It is actually pretty interesting to do a search for this kind of script, there are quite a few of them out there.
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Hex to Decimal Converter

26 May 2014, 21:42

@Kon good point.
But, anyways... Pure math solution, no string functions or dllcall (but DLL is faster :P ) anyways, have you all forgotten about laszlo's ToBase() function ?
http://www.autohotkey.com/board/topic/1 ... ntry103624

Code: Select all

	;from Laszlo : http://www.autohotkey.com/board/topic/15951-base-10-to-base-36-conversion/#entry103624
	ToBase(n,b) { ; n >= 0, 1 < b <= 36
		Loop {
			d := mod(n,b), n //= b
			m := (d < 10 ? d : Chr(d+55)) . m
			IfLess n,1, Break
		}
		Return m
	}
Cheers ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Hex to Decimal Converter

27 May 2014, 02:00

^might as well write that in MCode :P

User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Hex to Decimal Converter

27 May 2014, 08:38

Haha true! ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hex to Decimal Converter

30 May 2014, 08:25

@kon
or in 1 function to convert base from 2 to 36

Code: Select all

MsgBox, % "Decimal:`t`t42`n"
        . "to Binary:`t`t"      ConvertBase(10, 2, 42)       "`n"
        . "to Octal:`t`t"       ConvertBase(10, 8, 42)       "`n"
        . "to Hexadecimal:`t"   ConvertBase(10, 16, 42)      "`n`n"
        . "Hexadecimal:`t2A`n"
        . "to Decimal:`t"       ConvertBase(16, 10, "2A")    "`n"
        . "to Octal:`t`t"       ConvertBase(16, 8, "2A")     "`n"
        . "to Binary:`t`t"      ConvertBase(16, 2, "2A")     "`n`n"
ExitApp


ConvertBase(InputBase, OutputBase, number)
{
    static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
    static v := A_IsUnicode ? "_i64tow"    : "_i64toa"
    VarSetCapacity(s, 65, 0)
    value := DllCall("msvcrt.dll\" u, "Str", number, "UInt", 0, "UInt", InputBase, "CDECL Int64")
    DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
    return s
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
kidingwithlaura

Re: Hex to Decimal Converter

01 Mar 2016, 11:07

I am not a programmer but I landed here while looking for number converter tools and I have got one resource as well, I want to thank all the hard working programmers for making such useful tools.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 202 guests