Jump to content

Binary to hex


Go to solution Solved by Jos,

Recommended Posts

Hi,

So I was using this piece of code posted by Jos in 2007.

$BinIn = "11111110"
  BinaryToHex($BinIn)
 Func BinaryToHex($BinIn)
$Bits = StringSplit($BinIn,"")
$dec = 0
For $x = $Bits[0] To 1 Step -1
    $dec +=  (2^($Bits[0]-$x)) * $Bits[$x]
Next
$hex = Hex($dec)
ConsoleWrite("bin:" & $BinIn & "  Dec:" & $dec & "   Hex:" & $Hex & @CRLF)
ConsoleWrite("bin:" & $BinIn & "  Dec:" & $dec & " " &  Hex(254) & @CRLF)
EndFunc

The first consolewrite returns: bin:11111110  Dec:254   Hex:406FC00000000000
The second consolewrite returns: bin:11111110  Dec:254 000000FE

Obviously FE is the correct answer. Anyone know why the first console write got it wrong even though the decimal (254) used in the hex-conversion is correct?

Looks like a bug?

Link to comment
Share on other sites

  • Developers
  • Solution

Change the type of $dec to int and then it works:

$BinIn = "11111110"
BinaryToHex($BinIn)
Func BinaryToHex($BinIn)
    $Bits = StringSplit($BinIn, "")
    $dec = 0
    For $x = $Bits[0] To 1 Step -1
        $dec += (2 ^ ($Bits[0] - $x)) * $Bits[$x]
    Next
    $hex = Hex(int($dec))
    ConsoleWrite("bin:" & $BinIn & "  Dec:" & $dec & "   Hex:" & $hex & @CRLF)
    ConsoleWrite("bin:" & $BinIn & "  Dec:" & $dec & " " & Hex(254) & @CRLF)
EndFunc   ;==>BinaryToHex

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Another way:

$BinIn = "11111110"
ConsoleWrite(Hex(_StringToUint($BinIn, 2), 2) & @LF)

Func _StringToUint($s, $base)
    Return DllCall("msvcrt.dll", "uint64:cdecl", "_wcstoui64", "wstr", $s, "ptr*", 0, "int", $base)[0]
EndFunc   ;==>_StringToUint

Func _UintToString($i, $base)
    Return DllCall("msvcrt.dll", "wstr:cdecl", "_ui64tow", "uint64", $i, "wstr", "", "int", $base)[0]
EndFunc   ;==>_UintToString

You can specify any base in 2...36 and you get the reverse conversion for the same price.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Developers

It seems AutoiT3's behavior changed for Hex() as that piece seemed to work back in 2007: 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Do you mean my above sample code doesn't work for you?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...