Jump to content

Binary as InputBox to decimal and hex?


Recommended Posts

#include <MsgBoxConstants.au3>
Local $input = InputBox("Message", "bin", "10101010")
Local $bin = BinaryToString(Binary($input))
MsgBox($MB_SYSTEMMODAL,  Hex($bin), Dec($bin))

Clearly binary to string is taking the value of the input as string and not as binary. 
I don't see any standard solution to this? Maybe, I am missing(overlooking) something here.


results values Hex( 009A2112) Dec(269488144)
When what I want to get Hex(aa) Dec(170)

Link to comment
Share on other sites

28 minutes ago, major_lee said:

When what I want to get Hex(aa) Dec(170)

Try this :

#include <MsgBoxConstants.au3>
Local $input = InputBox("Message", "bin", "10101010")
Local $iInt  = _BitsToInt($input)
MsgBox($MB_SYSTEMMODAL, Hex($iInt), $iInt)

Func _BitsToInt($bin)
    Local $aArr = StringSplit($bin, "", 2)
    Local $dec = 0
    For $i = UBound($aArr) - 1 To 0 Step -1
        If $aArr[$i] = "1" Then
            $dec = BitXOR($dec, BitShift(1, -(UBound($aArr) - 1 - $i)))
        EndIf
    Next
    Return $dec
EndFunc   ;==>_BitsToInt

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Also:

Local $input = InputBox("Message", "bin", "10101010")
Local $bin = _StringToInt($input, 2)
MsgBox($MB_SYSTEMMODAL,  "Result", Hex($bin) & @LF & $bin)

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

Works for $base in [2,36]

Edited by jchd

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

Just in case someone needs similar companion functions for base conversion:

Func _IntToString($i, $base)
    Return DllCall("msvcrt.dll", "wstr:cdecl", "_i64tow", "int64", $i, "wstr", "", "int", $base)[0]
    Return $aRes[0]
EndFunc   ;==>_IntToString

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

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

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

 

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

It's extremely unlikely that a given Windows install doesn't have some (any) version of msvcrt.dll lying around in system folders. I don't know if that is even possible.

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

 

Considering speed as a factor, Do you think it will be more optimal to use dllcall?  

 

4 minutes ago, jchd said:

It's extremely unlikely that a given Windows install doesn't have some (any) version of msvcrt.dll lying around in system folders. I don't know if that is even possible.

I understand.

Link to comment
Share on other sites

One single call to a probably already loaded DLL will be faster than a loop in interpreted language IMHO. Test by yourself and report.

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

3 hours ago, major_lee said:

Considering speed as a factor, Do you think it will be more optimal to use dllcall?  

There is not that much of a time difference between these two functions. Test : Calling the function in a loop "For $i = 1 To 100000" results in :

- with DLLCall = 2380 Milliseconds (==> approx. 2.4 Sec)

- 'my' variant = 3095 Milliseconds (==> approx. 3.1 Sec)

(my PC is 6 years old and therefore not the fastest :lol:)

The only little benefit (if any) is, that 'my variant' does not require the "msvcrt.dll". However, I agree with @jchd when he writes :

3 hours ago, jchd said:

It's extremely unlikely that a given Windows install doesn't have some (any) version of msvcrt.dll lying around in system folders. I don't know if that is even possible.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Correct but another benefit of the DLL is that it supports any base (2 ≤ base ≤ 36) without adjustment.

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

12 minutes ago, jchd said:

Correct but another benefit of the DLL is that it supports any base (2 ≤ base ≤ 36) without adjustment.

Your solution undoubtedly covers a wider scope. I would therefore recommend @major_lee to use it.

'My solution" may perhaps be interesting for people, who want to follow this process step by step. However, it is never a drawback to be able to choose from several alternatives :).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Here a loop approach with base from [2..16] :

Local $input, $bin
$input = InputBox("Message", "bin", "DEADBEEF")
$bin = _StringToIntEx($input, 16)
MsgBox($MB_SYSTEMMODAL,  "", Hex($bin) & @LF & $bin)
$input = InputBox("Message", "bin", "77777")
$bin = _StringToIntEx($input, 8)
MsgBox($MB_SYSTEMMODAL,  "", Hex($bin) & @LF & $bin)
$input = InputBox("Message", "bin", "10101010")
$bin = _StringToIntEx($input, 2)
MsgBox($MB_SYSTEMMODAL,  "", Hex($bin) & @LF & $bin)

Func _StringToIntEx($s, $base)
  Local $iVal = 0, $iLen = StringLen($s)
  For $i = 0 to $iLen - 1
    $iVal += Dec(StringMid($s, $iLen - $i, 1)) * ($base^$i)
  Next
  Return Int($iVal)
EndFunc

 

Link to comment
Share on other sites

Simple. Fast.* Short. No dlls and no loops:

Local $sbyte="10101010", $arr[1112]

$arr[0]="0"
$arr[1]="1"
$arr[10]="2"
$arr[11]="3"
$arr[100]="4"
$arr[101]="5"
$arr[110]="6"
$arr[111]="7"
$arr[1000]="8"
$arr[1001]="9"
$arr[1010]="A"
$arr[1011]="B"
$arr[1100]="C"
$arr[1101]="D"
$arr[1110]="E"
$arr[1111]="F"

$sHex=$arr[StringLeft($sByte,4)] & $arr[StringRight($sByte,4)]

ConsoleWrite($sHex &" "& Dec($sHex))

*Assuming multiple invocations

Edited by JockoDundee

Code hard, but don’t hard code...

Link to comment
Share on other sites

Timing Results for 100000 results, all algorithims, in order of solution posting:

Based on code from: Musashi
 Check: 000000AA 170
 100000 Iterations in 1.4478746 Seconds

Based on code from: jhcd
 Check: 00000000000000AA 170
 100000 Iterations in 0.5701819 Seconds

Based on code from: nine
 Check: 000000AA 170
 100000 Iterations in 1.1597322 Seconds
 
Based on code from: JockoDundee
 Check: AA 170
 100000 Iterations in 0.2289266 Seconds

Code was modified only to normalize testing across all.  If there are further optimizations or if I screwed anything up, let me know and I will rerun.

The modified sources are below, alpha by author:

 

JD.au3 JH.au3 M.au3 N.au3

Code hard, but don’t hard code...

Link to comment
Share on other sites

19 hours ago, JockoDundee said:

Simple. Fast.* Short. No dlls and no loops:

$arr[1112]
 

*Assuming multiple invocations

 

an array of 1113 elements to then use only 16?

... can't look at that absurd array declaration ... :P

here is an equally "bizarre" solution but with less memory waste .... I think it works at roughly the same speed

Global $sBasedOn = "Chimp", $iIter = 100000, $sOut, $t = TimerInit()
Global $sbyte = "10101010"

Global Enum $0000, $0001, $0010, $0011, $0100, $0101, $0110, $0111, $1000, $1001
Global $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"

For $n = 1 To $iIter
    $sOut = Eval(StringLeft($sbyte, 4)) & Eval(StringRight($sbyte, 4))
    $sOut = $sOut & " " & Dec($sOut)
Next

$t = TimerDiff($t)

ConsoleWrite("Based on code from: " & $sBasedOn & @CRLF & " Check: " & $sOut & " " & @CRLF & " " & $iIter & " Iterations in " & $t / 1000 & " Seconds" & @CRLF & @CRLF)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

17 minutes ago, Chimp said:

 

an array of 1113 elements to then use only 16?

... can't look at that absurd array declaration ... :P

here is an equally "bizarre" solution but with less memory waste .... I think it works at roughly the same speed

Global $sBasedOn = "Chimp", $iIter = 100000, $sOut, $t = TimerInit()
Global $sbyte = "10101010"

Global Enum $0000, $0001, $0010, $0011, $0100, $0101, $0110, $0111, $1000, $1001
Global $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"

For $n = 1 To $iIter
    $sOut = Eval(StringLeft($sbyte, 4)) & Eval(StringRight($sbyte, 4))
    $sOut = $sOut & " " & Dec($sOut)
Next

$t = TimerDiff($t)

ConsoleWrite("Based on code from: " & $sBasedOn & @CRLF & " Check: " & $sOut & " " & @CRLF & " " & $iIter & " Iterations in " & $t / 1000 & " Seconds" & @CRLF & @CRLF)

 

I'm interested in how this works.  By defining the Enum it defines it as it's binary through Eval?

Link to comment
Share on other sites

16 minutes ago, major_lee said:

I'm interested in how this works.  By defining the Enum it defines it as it's binary through Eval?

No, Enum assigns the values in sequence starting by default at 0 and incrementing by 1 to the following variables
is a shorter way than it equates to this:

Global $0000 = 0, $0001 = 1, $0010 = 2, $0011 = 3, $0100 = 4, $0101 = 5, $0110 = 6, $0111 = 7, $1000 = 8, $1001 = 9

here the "trick" is to use variable names that correspond to the binary value (base 2) of the value they contain,
In this way using the Eval () function we can convert the binary Nibble to the corresponding decimal value.
This is just a fun, but unscientific exercise ....

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Here, just for comparison, are the runtime results on my stone-age PC :) :

Quote

Based on code from: Chimp
 Check: AA 170
 100000 Iterations in 0.551558805355629 Seconds

Based on code from: JockoDundee
 Check: AA 170
 100000 Iterations in 0.999790610787806 Seconds

Based on code from: jhcd
 Check: 00000000000000AA 170
 100000 Iterations in 1.31044055121973 Seconds

Based on code from: nine
 Check: 000000AA 170
 100000 Iterations in 2.48655608145451 Seconds

Based on code from: Musashi
 Check: 000000AA 170
 100000 Iterations in 3.03238456696285 Seconds

@major_lee :

I'm sure you've already realized this yourself, but here an additional note. The solutions from @JockoDundee and @Chimp only work if you specify exactly 8 bits. This is not meant as a critique, because that was the specification !!.

But if you also want to convert e.g. "101" or "11111111000000", then you should use the solution from @jchd , @Nine (or my one).

Edited by Musashi
Moved @Nine's solution to the right section :)

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

21 minutes ago, major_lee said:

if the $input was not always 4,and,4 it wouldn't work,

I see that you have already realized this issue :) . 100000 iterations were intended as a 'hardcore' scenario. You will probably need only a few conversions, especially when using InputBox. The conversion time is therefore negligible.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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...