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?
Hexadecimal To Base64
Re: Hexadecimal To Base64
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?
Re: Hexadecimal To Base64
Hello, this should work for you.
I have also posted this example snippet on stackoverflow for you.
Hope this helps
or as a single function
I have also posted this example snippet on stackoverflow for you.
Hope this helps
Code: Select all
#Include libcrypt.ahk
Hex := "48656c6c6f20576f726c642100"
len := LC_Hex2Bin(Bin, Hex)
LC_Base64_Encode(base64, Bin, len)
MsgBox % base64
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
}
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]
Re: Hexadecimal To Base64
@joedf ah, you beat me to it.
Anyways
I don't think it's possible to optimize any calls away.
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")
Re: Hexadecimal To Base64
joedf wrote: ↑08 Dec 2021, 23:12Hello, this should work for you.
I have also posted this example snippet on stackoverflow for you.
Hope this helps
or as a single functionCode: Select all
#Include libcrypt.ahk Hex := "48656c6c6f20576f726c642100" len := LC_Hex2Bin(Bin, Hex) LC_Base64_Encode(base64, Bin, len) MsgBox % base64
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 }
I am not sure why did not work the first time on StackOverflow, but it DOES WORK here Thanks
I really appreciate you
Re: Hexadecimal To Base64
To be honest, I was taking a short break at work and didn't want to write code to take more time...
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]