Hexadecimal To Base64

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Mr MG
Posts: 14
Joined: 07 Dec 2021, 04:14

Hexadecimal To Base64

08 Dec 2021, 19:10

Hello,

I really did search hard almost anywhere to find an example script that can convert hexadecimal to base64 but no point! all of them are going to convert text to base64.

Anyone has a working example, please?
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Hexadecimal To Base64

08 Dec 2021, 19:30

Do you actually have a string of data represented as 0-9 and A-F characters? Or do you have a binary file that you are looking at with a viewer that displays it as hex?
Mr MG
Posts: 14
Joined: 07 Dec 2021, 04:14

Re: Hexadecimal To Base64

08 Dec 2021, 22:10

boiler wrote:
08 Dec 2021, 19:30
Do you actually have a string of data represented as 0-9 and A-F characters? Or do you have a binary file that you are looking at with a viewer that displays it as hex?
Yes, I do have a string data that contains 0-9 and A-F characters.
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Hexadecimal To Base64

08 Dec 2021, 23:12

Hello, this should work for you.
I have also posted this example snippet on stackoverflow for you.
Hope this helps :+1:

Code: Select all

#Include libcrypt.ahk

Hex := "48656c6c6f20576f726c642100"
len := LC_Hex2Bin(Bin, Hex)

LC_Base64_Encode(base64, Bin, len)

MsgBox % base64
or as a single function

Code: Select all

#Include libcrypt.ahk

MsgBox % hexstring2base64("48656c6c6f20576f726c642100")

hexstring2base64(hex_string) {
    len := LC_Hex2Bin(Bin, hex_string)
    LC_Base64_Encode(base64, Bin, len)
    return base64
}
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]
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Hexadecimal To Base64

08 Dec 2021, 23:23

@joedf ah, you beat me to it.

Anyways

Code: Select all

str := "abcdef"

      DllCall("crypt32\CryptStringToBinary"
               , "ptr", &str, "uint", 0, "uint", CRYPT_STRING_HEXRAW := 0xC, "ptr", 0, "uint*", size:=0, "ptr", 0, "ptr", 0)

      VarSetCapacity(bin, size)

      DllCall("crypt32\CryptStringToBinary"
               , "ptr", &str, "uint", 0, "uint", CRYPT_STRING_HEXRAW := 0xC, "ptr", &bin, "uint*", size, "ptr", 0, "ptr", 0)

      ; CRYPT_STRING_NOCRLF | CRYPT_STRING_BASE64 = 0x40000001

      ; Using CryptBinaryToStringA saves about 2MB in memory.
      DllCall("crypt32\CryptBinaryToStringA", "ptr", &bin, "uint", size, "uint", 0x40000001, "ptr", 0, "uint*", length:=0)
      VarSetCapacity(str, length)
      DllCall("crypt32\CryptBinaryToStringA", "ptr", &bin, "uint", size, "uint", 0x40000001, "ptr", &str, "uint*", length)

      MsgBox % StrGet(&str, length, "CP0")

I don't think it's possible to optimize any calls away.
Mr MG
Posts: 14
Joined: 07 Dec 2021, 04:14

Re: Hexadecimal To Base64

09 Dec 2021, 03:11

joedf wrote:
08 Dec 2021, 23:12
Hello, this should work for you.
I have also posted this example snippet on stackoverflow for you.
Hope this helps :+1:

Code: Select all

#Include libcrypt.ahk

Hex := "48656c6c6f20576f726c642100"
len := LC_Hex2Bin(Bin, Hex)

LC_Base64_Encode(base64, Bin, len)

MsgBox % base64
or as a single function

Code: Select all

#Include libcrypt.ahk

MsgBox % hexstring2base64("48656c6c6f20576f726c642100")

hexstring2base64(hex_string) {
    len := LC_Hex2Bin(Bin, hex_string)
    LC_Base64_Encode(base64, Bin, len)
    return base64
}
:D
I am not sure why did not work the first time on StackOverflow, but it DOES WORK here :lol: Thanks
I really appreciate you
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Hexadecimal To Base64

09 Dec 2021, 09:48

To be honest, I was taking a short break at work and didn't want to write code to take more time... :P
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]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, mmflume, OrangeCat, roysubs, ShatterCoder, TAC109 and 97 guests