Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active April 15, 2022 03:20
Show Gist options
  • Save tmplinshi/faf391f98d3b80d18618 to your computer and use it in GitHub Desktop.
Save tmplinshi/faf391f98d3b80d18618 to your computer and use it in GitHub Desktop.
HEX, RGB, INT, STR | Convert Base. (by jNizM, http://ahkscript.org/boards/viewtopic.php?f=6&t=3925)
; ========================================================
; Int to Hex
; ========================================================
int2hex(int)
{
HEX_INT := 8
while (HEX_INT--)
{
n := (int >> (HEX_INT * 4)) & 0xf
ret .= n > 9 ? chr(55 + n) : n
if (HEX_INT == 0 && HEX_INT//2 == 0)
ret .= " "
}
return "0x" ret
}
MsgBox % int2hex(119) ; 0x00000077
; ========================================================
; Convert Base
; ========================================================
ConvertBase(InputBase, OutputBase, nptr) ; Base 2 - 36
{
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
static v := A_IsUnicode ? "_i64tow" : "_i64toa"
VarSetCapacity(s, 66, 0)
value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InputBase, "CDECL Int64")
DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
return s
}
MsgBox % ConvertBase(10, 16, 119) ; Dec to Hex: 77
MsgBox % ConvertBase(16, 10, 77) ; Hex to Dec: 119
; ========================================================
; Hex to Str
; ========================================================
hex2str(hex)
{
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
loop, parse, hex, " "
{
char .= Chr(DllCall("msvcrt.dll\" u, "Str", A_LoopField, "Uint", 0, "UInt", 16, "CDECL Int64"))
}
return char
}
MsgBox % hex2str("0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79") ; AutoHotkey
; ========================================================
; Str to Hex
; ========================================================
str2hex(str)
{
static v := A_IsUnicode ? "_i64tow" : "_i64toa"
loop, parse, str
{
VarSetCapacity(s, 65, 0)
DllCall("msvcrt.dll\" v, "Int64", Asc(A_LoopField), "Str", s, "UInt", 16, "CDECL")
hex .= "0x" s " "
}
return SubStr(hex, 1, (StrLen(hex) - 1))
}
MsgBox % str2hex("AutHotkey") ; 0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79
hex2rgb(CR)
{
H := InStr(CR, "0x") ? CR : (InStr(CR, "#") ? "0x" SubStr(CR, 2) : "0x" CR)
return (H & 0xFF0000) >> 16 "," (H & 0xFF00) >> 8 "," (H & 0xFF)
}
MsgBox % hex2rgb("0x77c8d2") ; 119,200,210
MsgBox % hex2rgb("#77c8d2") ; 119,200,210
MsgBox % hex2rgb("77c8d2") ; 119,200,210
; ==================================================================================================
hex2rgb(CR)
{
H := InStr(CR, "0x") ? CR : (InStr(CR, "#") ? "0x" SubStr(CR, 2) : "0x" CR)
return (H / 65536) & 255 "," (H / 256) & 255 "," H & 255
}
MsgBox % hex2rgb("0x77c8d2") ; 119,200,210
MsgBox % hex2rgb("#77c8d2") ; 119,200,210
MsgBox % hex2rgb("77c8d2") ; 119,200,210
; ==================================================================================================
hex2rgb(CR)
{
NumPut((InStr(CR, "#") ? "0x" SubStr(CR, 2) : "0x") SubStr(CR, -5), (V := "000000"))
return NumGet(V, 2, "UChar") "," NumGet(V, 1, "UChar") "," NumGet(V, 0, "UChar")
}
MsgBox % hex2rgb("0x77c8d2") ; 119,200,210
MsgBox % hex2rgb("#77c8d2") ; 119,200,210
MsgBox % hex2rgb("77c8d2") ; 119,200,210
rgb2hex(R, G, B, H := 1) ; just me
{
H := ((H = 1) ? "#" : ((H = 2) ? "0x" : ""))
VarSetCapacity(Hex, 17 << !!A_IsUnicode, 0)
DllCall("Shlwapi.dll\wnsprintf", "Str", Hex, "Int", 17, "Str", "%016I64X", "UInt64", (R << 16) + (G << 8) + B, "Int")
return H SubStr(Hex, StrLen(Hex) - 6 + 1)
}
MsgBox % rgb2hex("119", "200", "210", 0) ; 77C8D2
MsgBox % rgb2hex("119", "200", "210", 1) ; #77C8D2
MsgBox % rgb2hex("119", "200", "210", 2) ; 0x77C8D2
; ==================================================================================================
rgb2hex(R, G, B, H := 1)
{
static U := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
static V := A_IsUnicode ? "_i64tow" : "_i64toa"
rgb := ((R << 16) + (G << 8) + B)
H := ((H = 1) ? "#" : ((H = 2) ? "0x" : ""))
VarSetCapacity(S, 66, 0)
value := DllCall("msvcrt.dll\" U, "Str", rgb , "UInt", 0, "UInt", 10, "CDECL Int64")
DllCall("msvcrt.dll\" V, "Int64", value, "Str", S, "UInt", 16, "CDECL")
return H S
}
MsgBox % rgb2hex("119", "200", "210", 0) ; 77C8D2
MsgBox % rgb2hex("119", "200", "210", 1) ; #77C8D2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment