Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

10 Jul 2014, 01:30

HEX to RGB

Code: Select all

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

Code: Select all

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

Code: Select all

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
Last edited by jNizM on 16 Jul 2014, 02:57, edited 4 times in total.
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: HEX to RGB && RGB to HEX

10 Jul 2014, 01:31

RGB to HEX

Code: Select all

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

Code: Select all

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
MsgBox % rgb2hex("119", "200", "210", 2)        ; 0x77C8D2
Last edited by jNizM on 10 Jul 2014, 03:31, edited 1 time in total.
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: HEX to RGB && RGB to HEX

10 Jul 2014, 01:37

EXTRAS

Int to Hex

Code: Select all

int2hex(int)
{
    HEX_INT := 8
    while (HEX_INT--)
    {
        n := (int >> (HEX_INT * 4)) & 0xf
        h .= n > 9 ? chr(0x37 + n) : n
        if (HEX_INT == 0 && HEX_INT//2 == 0)
            h .= " "
    }
    return "0x" h
}

MsgBox % int2hex(119)        ; 0x00000077

Convert Base

Code: Select all

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

Code: Select all

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

Code: Select all

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
Last edited by jNizM on 17 Jul 2014, 08:58, edited 3 times in total.
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: HEX, RGB, INT, STR | Convert Base

10 Jul 2014, 04:03

Nice, thanks. I've copied all above functions to my gist.
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: HEX, RGB, INT, STR | Convert Base

15 Jul 2014, 08:55

Added scripts collection in top post
BaseConversion.html
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: HEX, RGB, INT, STR | Convert Base

15 Jul 2014, 09:06

For the Int2Hex couldn't you have used the SetFormat command?
ciao
toralf
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: HEX, RGB, INT, STR | Convert Base

15 Jul 2014, 11:37

@toralf.. I dont think you test it yet!? - So no! No need for SetFormat!

My goal was not to use SetFormat for these base convert functions.
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
joedf
Posts: 7796
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: HEX, RGB, INT, STR | Convert Base

15 Jul 2014, 16:56

Awesome webpage :D
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

29 Jul 2014, 08:16

thx..
added some more examples and fix something
BaseConversion
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
nnnik
Posts: 4479
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

29 Jul 2014, 14:49

Why don't you use the CryptBinaryToString function?

Code: Select all

str2hex(str)
{
	StrLength := (Strlen(str)+1)*(2)
	DllCall("crypt32\CryptBinaryToString","strw",Str,"uint",StrLength,"uint",4,"ptr",0,"uint*",size)
	VarSetCapacity(OutStr,A_IsUnicode?size*2:size,0)
	DllCall("crypt32\CryptBinaryToString","strw",Str,"uint",StrLength,"uint",4,"ptr",&OutStr,"uint*",size)
    return RegexReplace(Strget(&outstr),"s)\s")
}

Msgbox % str2hex("Test")
I know that it isnt actually the same as your function.
However I consider this the better option of the 2.
Recommends AHK Studio
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

30 Jul 2014, 03:28

Hex for Test:
T = 0x54
e = 0x65
s = 0x73
t = 0x74
Test ==> 0x54 0x65 0x73 0x74 (54 65 73 74)

your function gives:
54 00 65 00 73 00 74 00 00 00
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
nnnik
Posts: 4479
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

30 Jul 2014, 04:11

Yeah thats due to the fact that this function gives an exact binary to hex conversion.
your function gives:
54006500730074000000
Recommends AHK Studio
guest3456
Posts: 3150
Joined: 09 Oct 2013, 10:31

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

30 Jul 2014, 09:53

its probably giving a unicode string: two bytes per character, plus a null terminator

User avatar
joedf
Posts: 7796
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

30 Jul 2014, 13:49

Possibly... But then it cant be UTF-8... UTF-16?
robodesign
Posts: 714
Joined: 30 Sep 2017, 03:59
Facebook: marius.sucan
GitHub: mariussucan
Location: Romania
Contact:

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

09 Oct 2019, 08:31

jNizM wrote:
29 Jul 2014, 08:16
thx..
added some more examples and fix something
BaseConversion
The link is dead. Is there a backup somewhere else?

Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

09 Oct 2019, 08:33

Sorry got no backup from my old dropbox account.
It was just page to visual the functions with examples.
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
bourdin07
Posts: 33
Joined: 23 Jun 2019, 12:57

Re: Convert Base | HEX, DEC, OCT, BIN, INT, RGB, STR

05 Feb 2020, 14:10

Nice methods, I like ConvertBase()

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 20 guests