Jump to content

Split Binary To Environment Vairable


 Share

Recommended Posts

Hi ya.

What I am after is the ability to convert an 8 bit binary number to individual environment variables.

For example:

COnvert 11101011 to:

$a=1

$b=1

$c=1

$d=0

$e=1

$f=0

$g=1

$h=1

or if the binary number is 1101 then convert it to:

$a=0

$b=0

$c=0

$d=0

$e=1

$f=1

$g=0

$h=1

I don't know how to do this :) , but if you could show me that would be most welcome!

Cheers

Baz

Link to comment
Share on other sites

It doesn't do individual variables, but it could if you added to it. Instead, it creates an array and returns that. It accepts numbers or strings (converted to string either way), and doesn't allow anything other than 0's and 1's. You can also specify how long it should end up (default is 8).

$splitme = _SplitBinaryNumber (1011101)
If @error Then
    MsgBox (0, "Error", "Something's wrong")
Else
    _ArrayDisplay ($splitme, "Split")
EndIf

Func _SplitBinaryNumber ($BinaryNumber, $TotalLength = 8)
    Local $SplitBinary
    If IsString ($BinaryNumber) And Not StringRegExp ($BinaryNumber, "[^01]", 0) Then
        For $i = StringLen ($BinaryNumber) To $TotalLength - 1
            $BinaryNumber = "0" & $BinaryNumber
        Next
        $SplitBinary = StringSplit($BinaryNumber, "")
        Return $SplitBinary
    ElseIf IsNumber ($BinaryNumber) Then
        $BinaryNumber = String ($BinaryNumber)
        $SplitBinary = _SplitBinaryNumber($BinaryNumber, $TotalLength)
        If $SplitBinary <> -1 Then
            Return $SplitBinary
        EndIf
    EndIf
    SetError (1)
    Return -1
EndFunc
Edited by greenmachine
Link to comment
Share on other sites

Hi ya.

What I am after is the ability to convert an 8 bit binary number to individual environment variables.

For example:

COnvert 11101011 to:

$a=1

$b=1

$c=1

$d=0

$e=1

$f=0

$g=1

$h=1

Non-authoritative reply... :)

I haven't tried to code it yet, cause I'm not on a Windows box right now, but you can repeat a For loop eight times, each time your loop will:

1. Mask the low eight bits by $YourNum = BitAnd($YourNum, 0x000000FF)

2. Test bit 7 by If $YourNum >= 0x00000080

3. Left shift one place by $YourNum = BitShift($YourNum, -1)

This way, each bit get shifted through the bit 7 position for testing.

Hope this helps! :)

GreenMachine types too fast! Thhbbbbttt!!! :mellow:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Non-authoritative reply... :)

I haven't tried to code it yet, cause I'm not on a Windows box right now, but you can repeat a For loop eight times, each time your loop will:

1. Mask the low eight bits by $YourNum = BitAnd($YourNum, 0x000000FF)

2. Test bit 7 by If $YourNum >= 0x00000080

3. Left shift one place by $YourNum = BitShift($YourNum, -1)

This way, each bit get shifted through the bit 7 position for testing.

Hope this helps! :)

GreenMachine types too fast! Thhbbbbttt!!! :mellow:

Why all that bitshifting? Why not just use BitAND()?

$a = 0 <> BitAND($i, 128)
$b = 0 <> BitAND($i, 64)
$c = 0 <> BitAND($i, 32)
$d = 0 <> BitAND($i, 16)
$e = 0 <> BitAND($i, 8)
$f = 0 <> BitAND($i, 4)
$g = 0 <> BitAND($i, 2)
$h = 0 <> BitAND($i, 1)
Link to comment
Share on other sites

Thanks heaps for your help. :)

This is the final code. As you can see I am creating a small HTML file that displays different pic depending on the binary number. The code works but if you can see a way to improve on how I have written it, please tell.

Cheers

#include <File.au3>
dim $binary
$decimal=3
Call ("Dec2Bin", $decimal)
IF 0 <> BitAND($binary, 128) Then
    $a=1
Else
    $a=0
EndIf
IF 0 <> BitAND($binary, 64) Then
    $b=1
Else
    $b=0
EndIf
IF 0 <> BitAND($binary, 32) Then
    $c=1
Else
    $c=0
EndIf
IF 0 <> BitAND($binary, 16) Then
    $d=1
Else
    $d=0
EndIf
IF 0 <> BitAND($binary, 8) Then
    $e=1
Else
    $e=0
EndIf
IF 0 <> BitAND($binary, 4) Then
    $f=1
Else
    $f=0
EndIf
IF 0 <> BitAND($binary, 2) Then
    $g=1
Else
    $g=0
EndIf
If 0 <> BitAND($binary, 1) Then
    $h=1
Else
    $h=0
EndIf
$file = FileOpen("g:\HTML\leds.html", 2)
FileWriteLine($file, "<html>" & @CRLF)
FileWriteLine($file, "<body bgcolor=" & "#000000" & ">" & @CRLF)
FileWriteLine($file, "<img src="& $a & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $b & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $c & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $d & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $e & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $f & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $g & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $h & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "</body>" & @CRLF)
FileWriteLine($file, "</html>" & @CRLF)
FileClose($file)
;run("notepad g:\HTML\leds.html")
Func Dec2Bin($decimal)
  $binary = ''
  While $decimal>0
    $binary = String(Mod($decimal, 2)) & $binary
    $decimal = Int($decimal/2)
  WEnd
  Return $binary
EndFunc
Link to comment
Share on other sites

Thanks heaps for your help. :)

This is the final code. As you can see I am creating a small HTML file that displays different pic depending on the binary number. The code works but if you can see a way to improve on how I have written it, please tell.

Cheers

#include <File.au3>
dim $binary
$decimal=3
Call ("Dec2Bin", $decimal)
IF 0 <> BitAND($binary, 128) Then
    $a=1
Else
    $a=0
EndIf
IF 0 <> BitAND($binary, 64) Then
    $b=1
Else
    $b=0
EndIf
IF 0 <> BitAND($binary, 32) Then
    $c=1
Else
    $c=0
EndIf
IF 0 <> BitAND($binary, 16) Then
    $d=1
Else
    $d=0
EndIf
IF 0 <> BitAND($binary, 8) Then
    $e=1
Else
    $e=0
EndIf
IF 0 <> BitAND($binary, 4) Then
    $f=1
Else
    $f=0
EndIf
IF 0 <> BitAND($binary, 2) Then
    $g=1
Else
    $g=0
EndIf
If 0 <> BitAND($binary, 1) Then
    $h=1
Else
    $h=0
EndIf
$file = FileOpen("g:\HTML\leds.html", 2)
FileWriteLine($file, "<html>" & @CRLF)
FileWriteLine($file, "<body bgcolor=" & "#000000" & ">" & @CRLF)
FileWriteLine($file, "<img src="& $a & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $b & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $c & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $d & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $e & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $f & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $g & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $h & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "</body>" & @CRLF)
FileWriteLine($file, "</html>" & @CRLF)
FileClose($file)
;run("notepad g:\HTML\leds.html")
Func Dec2Bin($decimal)
  $binary = ''
  While $decimal>0
    $binary = String(Mod($decimal, 2)) & $binary
    $decimal = Int($decimal/2)
  WEnd
  Return $binary
EndFunc
You don't want to convert the number to binary before bitAND'ing it - that defeats the purpose.

You can simplify variable defining by doing this:

#include <File.au3>
Global $binary = 3, $a = 0, $b = 0, $c = 0, $d = 0, $e = 0, $f = 0, $g = 0, $h = 0; changed binary to be decimal.. didn't want to change each one in the bitand.

IF 0 <> BitAND($binary, 128) Then $a=1
IF 0 <> BitAND($binary, 64) Then $b=1
IF 0 <> BitAND($binary, 32) Then $c=1
IF 0 <> BitAND($binary, 16) Then $d=1
IF 0 <> BitAND($binary, 8) Then $e=1
IF 0 <> BitAND($binary, 4) Then $f=1
IF 0 <> BitAND($binary, 2) Then $g=1
If 0 <> BitAND($binary, 1) Then $h=1
$file = FileOpen("g:\HTML\leds.html", 2)
FileWriteLine($file, "<html>" & @CRLF)
FileWriteLine($file, "<body bgcolor=" & "#000000" & ">" & @CRLF)
FileWriteLine($file, "<img src="& $a & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $b & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $c & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $d & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $e & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $f & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $g & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "<img src="& $h & ".gif"&"><br>" & @CRLF)
FileWriteLine($file, "</body>" & @CRLF)
FileWriteLine($file, "</html>" & @CRLF)
FileClose($file)
;run("notepad g:\HTML\leds.html")
Link to comment
Share on other sites

  • Moderators

I really don't understand what ya'll are talking about... but this is Baz's cleaned up a tad:

dim $binary, $ah_Write[9], $Bit = 128
$decimal = 3
Dec2Bin($decimal)

Func Dec2Bin($decimal)
    $binary = ''
    While $decimal > 0
        $binary = String(Mod($decimal, 2)) & $binary
        $decimal = Int($decimal / 2)
    WEnd
    For $i = 1 To 8
        If BitAND($binary, $Bit) <> 0 Then
            $ah_Write[$i] = 1
        Else
            $ah_Write[$i] = 0
        EndIf
        $Bit = $Bit / 2
    Next
    $Bit = 128
    $file = FileOpen("g:\HTML\leds.html", 2)
    FileWriteLine($file, "<html>" & @CRLF)
    FileWriteLine($file, "<body bgcolor=" & "#000000" & ">" & @CRLF)
    For $x = 1 To 8
        FileWriteLine($file, "<img src=" & $ah_Write[$x] & ".gif" & "><br>" & @CRLF)
    Next
    FileWriteLine($file, "</body>" & @CRLF)
    FileWriteLine($file, "</html>" & @CRLF)
    FileClose($file)
EndFunc

Edit:

And green's made into a function:

Func Dec2Bin()
    Local $binary = 3
    Local $ah_Write[9] = ['', 0,0,0,0,0,0,0,0]
    Local $Bit = 128
    Local $file = FileOpen("g:\HTML\leds.html", 2)
    FileWriteLine($file, "<html>" & @CRLF)
    FileWriteLine($file, "<body bgcolor=" & "#000000" & ">" & @CRLF)
    For $i_Count = 1 To 8
        If BitAND($binary, $Bit) <> 0 Then $ah_Write[$i_Count] = 1
        FileWriteLine($file, "<img src=" & $ah_Write[$i_Count] & ".gif" & "><br>" & @CRLF)
        $Bit = $Bit / 2
    Next
    FileWriteLine($file, "</body>" & @CRLF)
    FileWriteLine($file, "</html>" & @CRLF)
    FileClose($file)
EndFunc
Edit2:

You'll need Beta for greens that I did, because of how the array is set up.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks heaps for your help. :)

This is the final code. As you can see I am creating a small HTML file that displays different pic depending on the binary number. The code works but if you can see a way to improve on how I have written it, please tell.

Cheers

A few questions:

Why are you including file.au3 when you aren't calling any functions?

Why are you using all these variables and not an array?

Why do you need all these variables anyway? In the code you posted, you're not doiong anything with them other than using them to tell you which file to source in the HTML. Just seems kind of redundant.

Why are you appending the EOL markers when using FileWriteLine? HTML ignores whitespace anyway, so what is the point of double-spacing it?

Would this do the same thing?

$decimal=3
$file = FileOpen("leds.html", 2)
FileWriteLine($file, "<html>" & @CRLF & "<body bgcolor=#000000>")
For $i_Count = 7 to 0 Step -1
    If BitAND($decimal, 2^$i_Count) Then
        FileWriteLine($file, "<img src=1.gif"&"><br>")
    Else
        FileWriteLine($file, "<img src=0.gif"&"><br>")
    EndIf
Next
FileWriteLine($file, "</body>" & @CRLF & "</html>")
FileClose($file)
run("notepad leds.html")
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...