Jump to content

binary string > binary character array (solved, ty)


Nevin
 Share

Recommended Posts

Hi all. I was fooling around with regexp a bit because I'm going to be reading in a string of binary numbers, and I wanted to make sure that the user entered ONLY binary numbers. I thought regexp might be able to do that for me. Then, as I was looking at the documentation I noticed that you can return an array of matches. I'm a bit rusty on my regexp and was curious if anyone knew how I could do what I want.

Lets say the string entered is 111000. I want the array:

array[0] = 1

array[1] = 1

array[2] = 1

array[3] = 0

array[4] = 0

array[5] = 0

does anyone have a suggestion for what the best way this could be done is? I was thinking of regexp, but maybe someone else has a better idea. :) I'll keep researching. Thanks in advance.

Edited by Nevin
Link to comment
Share on other sites

Hi all. I was fooling around with regexp a bit because I'm going to be reading in a string of binary numbers, and I wanted to make sure that the user entered ONLY binary numbers. I thought regexp might be able to do that for me. Then, as I was looking at the documentation I noticed that you can return an array of matches. I'm a bit rusty on my regexp and was curious if anyone knew how I could do what I want.

Lets say the string entered is 111000. I want the array:

array[0] = 1

array[1] = 1

array[2] = 1

array[3] = 0

array[4] = 0

array[5] = 0

does anyone have a suggestion for what the best way this could be done is? I was thinking of regexp, but maybe someone else has a better idea. :) I'll keep researching. Thanks in advance.

This doesn't give the array quite as you asked for but you can sort that out.

$a = StringSplit("111000", '')
For $n = 1 To $a[0]
    ConsoleWrite($a[$n] & @LF)
Next
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks for the quick response. This will be a good enough starting point for me to get where I need to go. I just needed a little nudge :)

Thanks again.

For those curious, I'm trying to make a ghetto program to do DES encryption. School-related, I'm not hacking the pentagon or anything. ;)

Link to comment
Share on other sites

  • Moderators

You could take this a bit further if you have long strings with multiple values... just create a 2d array to split... and if you want to use RegExp this may give you an idea:

#include <array.au3>
$sString = "000111111000101010110011"
$aArray = _mySRE($sString)
_ArrayDisplay($aArray)

Func _mySRE($sString)
    Local $aSRE = StringRegExp($sString, "[01]{6}", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    Local $nUBound = UBound($aSRE)
    Local $avArray[$nUBound + 1][7], $aSep
    For $iCC = 0 To $nUBound - 1
        $aSep = StringRegExp($aSRE[$iCC], "[01]", 3)
        If IsArray($aSep) Then
            For $xCC = 0 To 5
                $avArray[$iCC + 1][$xCC + 1] = $aSep[$xCC]
            Next
        EndIf
    Next
    Return $avArray
EndFunc

Edit:

Added it so only 0's and 1's were detected.

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

ahhh thanks. [01] rings a bell now that I see it. That checks for a 0 or a 1, right? I am using the StringSplit but it has no data validation, so I will possibly add in some later. It depends how many hairs I've pulled out while trying to do the encryption. It's so tedious. I'd post some of it now but it's on my other PC.

edit:

Weee.

If StringregExp($Input,"[^01]") Then

MsgBox(48,"Exiting program","Non-binary input detected.")

Exit

EndIf

Edited by Nevin
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...