Binary string convert to Hex Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
IWantItAll2
Posts: 8
Joined: 14 Jun 2023, 10:00

Binary string convert to Hex

25 Jul 2023, 06:31

I'm probably being very stupid but for the life of me i cant find what i want on the forum. I want to convert a string that is a binary number, and convert it to hex.

1010101111100111 = 0xABE7
1111100110111110 = 0xF9BE

Some forum posts say to use format but that doesn't seem to work for me

Code: Select all

hex := Format("0x{:x}","1111100110111110")
msgbox hex
outputs 0x3f28a275f8186
My guess would be something to do with ASCII formatting ?

Any help would be appreciated.
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Binary string convert to Hex

25 Jul 2023, 08:26

I just realized that you posted in the V2 section and the link I provided (by @SKAN ) appears to be V1. I checked @SKAN's list of functions in V2, but couldn't find the BinToHex() function. Perhaps @SKAN can help with the V2 version since I'm not well versed in DLL calls?

Russ
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Binary string convert to Hex

25 Jul 2023, 08:51

RussF wrote:
25 Jul 2023, 08:26
Perhaps @SKAN can help with the V2 version since I'm not well versed in DLL calls?
 
 
No need for DllCall()s.

Code: Select all

#Requires AutoHotkey v2.0

MsgBox Format("0x{:X}", unbin("1111100110111110")) ; 0xF9BE

unbin(b) ; By SKAN for Mcalc() @ autohotkey.com/r?p=437344
{
    local  l  :=  strlen(b)
         , f  :=  instr(b,1,1,-1)
         , n  :=  2**(l-f)

    while  f  :=  instr(b,1,1,f,-2)
           n  |=  2**(l-f)
    return n
}
Edit:
Binary digits need to be wrapped with quotes
MsgBox Format("0x{:X}", unbin(1111100110111110)) ; 0xF9BE
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Binary string convert to Hex  Topic is solved

25 Jul 2023, 08:51

More:

Code: Select all

; This script converts a binary number to a hexadecimal number
; https://www.autohotkey.com/board/topic/49990-binary-and-decimal-conversion/
#Requires AutoHotkey v2.0
bin2dec := (n) => (StrLen(n) > 1 ? bin2dec(SubStr(n, 1, -1)) << 1 : 0) | SubStr(n, -1)
MsgBox Format('0x{:X}', bin2dec('1111100110111110'))
IWantItAll2
Posts: 8
Joined: 14 Jun 2023, 10:00

Re: Binary string convert to Hex

25 Jul 2023, 09:54

Thanks to both @mikeyww and @SKAN
I like the 1 line approach from @mikeyww so i am going to list that as the response. Both seem to work with my code just as well as each other when i tested.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Binary string convert to Hex

25 Jul 2023, 14:28

For the complete collection:

Code: Select all

bin := '1111100110111110'
MsgBox Format('0x{:X}', DllCall('msvcrt\_wcstoui64', 'Str', bin, 'Ptr', 0, 'Int', 2, 'Cdecl Int64'))

DllCall('msvcrt\_i64tow', 'UInt64', 0xF9BE, 'Ptr', buf := Buffer(128), 'Int', 2, 'Cdecl')
MsgBox StrGet(buf)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Rohwedder, songdg, Soren and 42 guests