crackdonalds Posted November 23, 2021 Posted November 23, 2021 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?
Developers Solution Jos Posted November 23, 2021 Developers Solution Posted November 23, 2021 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 crackdonalds 1 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.
crackdonalds Posted November 23, 2021 Author Posted November 23, 2021 $hex = Hex(int($dec)) that fixed it indeed. thx
jchd Posted November 23, 2021 Posted November 23, 2021 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. Jos 1 Reveal hidden contents 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 hereRegExp tutorial: enough to get startedPCRE 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)
Developers Jos Posted November 23, 2021 Developers Posted November 23, 2021 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.
crackdonalds Posted November 23, 2021 Author Posted November 23, 2021 On 11/23/2021 at 12:17 PM, Jos said: It seems AutoiT3's behavior changed for Hex() as that piece seemed to work back in 2007: Expand Yea that's what I was thinking. I had found more examples and they all didn't work (anymore).
jchd Posted November 23, 2021 Posted November 23, 2021 Do you mean my above sample code doesn't work for you? Reveal hidden contents 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 hereRegExp tutorial: enough to get startedPCRE 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)
crackdonalds Posted November 23, 2021 Author Posted November 23, 2021 On 11/23/2021 at 2:26 PM, jchd said: Do you mean my above sample code doesn't work for you? Expand I didn't try your solution because the suggestion made by Jos already worked for me. Thanks for you contribution. Maybe they can implement your solution as UDF.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now