Sign in to follow this  
Followers 0
czardas

zero2one

8 posts in this topic

#1 ·  Posted (edited)

I would like someone to look at my script. I think that the code can be condensed. I will describe what is happening with the script. A three digit hexadecimal number represents a binary string. Each zero within the string is substituted for a one in turn. So a string with five zeros will produce 5 new strings each containing 4 zeros.

Eg The hex number 2A5 represents the binary string 001010100101 which contains seven zeros.

This means that there are seven places where '1' can replace a zero (ie positions 1, 2, 4, 6, 8, 9 and 11 in the string).

The seven return values for the input 2A5 are: 3A5, 6A5, AA5, 2B5, 2E5, 2A7 and 2AD.

;This script takes a hexadecimal number and treats it as a binary string. Each zero is turned to a 1 in turn, and the results recorded in add.log.
$input = InputBox("INPUT", "Input hex value 000 - FFF")

If StringLen($input) <> 3 Or Not StringIsXDigit($input) Then
    MsgBox(0,"ERROR" , "You did not enter a 3 digit hexadecimal number :-(")
    Exit
Else
EndIf

;Declare each of the characters in the string
$char1 = StringTrimRight($input, 2)
$char2 = StringMid($input, 2, 1)
$char3 = StringTrimLeft($input, 2)

FileOpen("add.log", 2)
If @error Then
    MsgBox(0, "Error", "Unable to open add.log")
    Exit
EndIf

;Aquire characters for substitution.
$charX = _zero2one($char1)
$Len = StringLen($charX)

;Checks whether $char1 is 'F', which can not be substituted.
If $charX <> "X" Then
    $count = 1
    $Len = StringLen($charX)
    $Len = $Len + 1
    Do
        $char2add = StringMid($charX, $count , 1)
        ;Log each new string entry.
        FileWriteLine("add.log", $char2add & $char2 & $char3)
        If @error Then
            MsgBox(0, "Error", "Unable to write to add.log")
            Exit
        EndIf
        $count = $count + 1
    Until $count = $Len
Else
EndIf

;Repeat for second digit.
$charX = _zero2one($char2)
$Len = StringLen($charX)
If $charX <> "X" Then
    $count = 1
    $Len = StringLen($charX)
    $Len = $Len + 1
    Do
        $char2add = StringMid($charX, $count , 1)
        FileWriteLine("add.log", $char1 & $char2add & $char3)
        If @error Then
            MsgBox(0, "Error", "Unable to write to add.log")
            Exit
        EndIf
        $count = $count + 1
    Until $count = $Len
Else
EndIf

;Repeat for third digit.
$charX = _zero2one($char3)
$Len = StringLen($charX)
If $charX <> "X" Then
    $count = 1
    $Len = StringLen($charX)
    $Len = $Len + 1
    Do
        $char2add = StringMid($charX, $count , 1)
        FileWriteLine("add.log", $char1 & $char2 & $char2add)
        If @error Then
            MsgBox(0, "Error", "Unable to write to add.log")
            Exit
        EndIf
        $count = $count + 1
    Until $count = $Len
Else
EndIf
FileClose("add.log")

;Returns a choice of substitution characters, Substituting each value results in changing a zero to 1 in binary.
Func _zero2one($char)
    If $char = "A" Then
        $char = "BE"
    ElseIf $char = "B" Then
        $char = "F"
    ElseIf $char = "C" Then
        $char = "DE"
    ElseIf $char = "D" Then
        $char = "F"
    ElseIf $char = "E" Then
        $char = "F"
    ElseIf $char = "F" Then
        $char = "X"
    ElseIf $char = 0 Then
        $char = "1248"
    ElseIf $char = 1 Then
        $char = "359"
    ElseIf $char = 2 Then
        $char = "36A"
    ElseIf $char = 3 Then
        $char = "7B"
    ElseIf $char = 4 Then
        $char = "56C"
    ElseIf $char = 5 Then
        $char = "7D"
    ElseIf $char = 6 Then
        $char = "7E"
    ElseIf $char = 7 Then
        $char = "F"
    ElseIf $char = 8 Then
        $char = "9AC"
    ElseIf $char = 9 Then
        $char = "BD"
    EndIf
    Return $char
EndFunc

I am know I can condense the function and I intend to do so, I have left it like this for the sake of clarity. What concerns me is that I don't like having to rewrite the function call for each of the digits in the input string. What I am after is a less long winded way of taking each of the characters in turn, and making the required substitutions. Does anyone have any suggestions. FYO: I'm sure this is obvious to nearly everybody here, but I'll mention it anyway - the script will not run without first creating the file add.log. :)

By the way, responses to my previous post, about the metronome are well appreciated, but I find it a bit tricky. I need time to absorb the information. I decided to post this in the mean time. I have not abandoned the metronome post.

Thanks,

Czardas

Edited by czardas

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't quite get what you want this to do.. Maybe I should read your other posts : \...

I was able to shorten it tho. Changed the If to Switch, and the way it called FileWriteLine. I had to make a new function to fit the code into a For loop because I wasn't really sure how to write it to the file the way you had before... But now you can have more or less than three characters :).

;This script takes a hexadecimal number and treats it as a binary string. Each zero is turned to a 1 in turn, and the results recorded in add.log.
$input = InputBox("INPUT", "Input hex value 000 - FFF")

If Not StringIsXDigit($input) Then
    Exit MsgBox(0,"ERROR" , "You did not enter a hexadecimal number :-(")
EndIf

;Declare each of the characters in the string
$char = StringSplit($input, "")

$hFile = FileOpen("add.log", 2)
If @error Then
    MsgBox(0, "Error", "Unable to open add.log")
    Exit
EndIf

For $i = 1 To $char[0]
    ;Aquire characters for substitution.
    $charX = _zero2one($char[$i])
    $Len = StringLen($charX)

    ;Checks whether $char1 is 'F', which can not be substituted.
    If $charX <> "X" Then
        $count = 1
        $Len = StringLen($charX)
        $Len = $Len + 1
        Do
            $char2add = StringMid($charX, $count , 1)
            ;Log each new string entry.
            FileWriteLine2($hFile, $char, $i, $char2add)
            If @error Then
                MsgBox(0, "Error", "Unable to write to add.log")
                Exit
            EndIf
            $count = $count + 1
        Until $count = $Len
    EndIf
Next

FileClose($hFile)

;Returns a choice of substitution characters, Substituting each value results in changing a zero to 1 in binary.
Func _zero2one($char)
    Switch $char
        Case 0
            $char = "1248"
        Case 1
            $char = "359"
        Case 2
            $char = "36A"
        Case 3
            $char = "7B"
        Case 4
            $char = "56C"
        Case 5
            $char = "7D"
        Case 6
            $char = "7E"
        Case 7
            $char = "F"
        Case 8
            $char = "9AC"
        Case 9
            $char = "BD"
        Case "A"
            $char = "BE"
        Case "B"
            $char = "F"
        Case "C"
            $char = "DE"
        Case "D"
            $char = "F"
        Case "E"
            $char = "F"
        Case "F"
            $char = "X"
    EndSwitch
    Return $char
EndFunc

Func FileWriteLine2($file, $chars, $i, $char2add)
    $msg = ""
    For $j = 1 To $chars[0]
        If $i == $j Then; replace
            $msg &= $char2add
            ContinueLoop
        EndIf
        $msg &= $chars[$j]
    Next
    FileWriteLine($file, $msg)
EndFunc
Edit: Also, when you use FileOpen, you have to use the handle it returns instead of the actual file name if you want it to use the opened file instead of opening it again. Edited by gamerman2360

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I don't quite get what you want this to do.. Maybe I should read your other posts : \...

What does it do? Hmm. It's part of a search engine. When no match is found within a database, then the string is altered and the search repeated. So if my input string is '5' [0101] and no match is found, then the engine will search for '7' [0111] followed by 'D' [1101]. Now suppose that 'D' is found but '7' is not. Then 'D' will be given as the most likely (closest) match. The results will reveal that the entry found in the database, namely 'D', differs from '5' by one binary digit. I hope you find this answer satisfactory for now. Sorry I can't be more specific. :) I will eventually post the source when the project is finished.

Thanks for taking the time. At a glance, I can see that studying your code will help me to code more efficiently.

Czardas

Update: Unfortunately the changes you made have broken it!

Edited by czardas

Share this post


Link to post
Share on other sites

This function makes NO sense to me at all:

;Returns a choice of substitution characters, Substituting each value results in changing a zero to 1 in binary.
Func _zero2one($char)
    Switch $char
        Case 0
            $char = "1248"
        Case 1
            $char = "359"
        Case 2
            $char = "36A"
        Case 3
            $char = "7B"
        Case 4
            $char = "56C"
        Case 5
            $char = "7D"
        Case 6
            $char = "7E"
        Case 7
            $char = "F"
        Case 8
            $char = "9AC"
        Case 9
            $char = "BD"
        Case "A"
            $char = "BE"
        Case "B"
            $char = "F"
        Case "C"
            $char = "DE"
        Case "D"
            $char = "F"
        Case "E"
            $char = "F"
        Case "F"
            $char = "X"
    EndSwitch
    Return $char
EndFunc

If you set the low bit on '0', you get '1'. If you set the high bit on '0', you get '8'. In no case whatsoever can you set a single bin on '0' and get 1248! Where did that bogus number (and the ones after it) come from???

:)


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This function makes NO sense to me at all:

If you set the low bit on '0', you get '1'. If you set the high bit on '0', you get '8'. In no case whatsoever can you set a single bin on '0' and get 1248! Where did that bogus number (and the ones after it) come from???

:)

1248 is just a string of digits returned by the function, and not a number. Each digit in the string can be used as a replacement. The actual substitutions do not occur within the function itself. The return value 1248 represents 4 replacements for the binary string 0000 viz:

replace 0 with 1 (result 0000 becomes 0001) OR

replace 0 with 2 (result 0000 becomes 0010) OR

replace 0 with 4 (result 0000 becomes 0100) OR

replace 0 with 8 (result 0000 becomes 1000)

Does that make any more sense? :P

Update: You are right, the function doesn't work. My original function at the start of this topic does work.

Edited by czardas

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Fixed!

;This script takes a hexadecimal number and treats it as a binary string. Each zero is turned to a 1 in turn, and the results recorded in add.log.
$input = InputBox("INPUT", "Input hex value 000 - FFF")

If Not StringIsXDigit($input) Then
    Exit MsgBox(0,"ERROR" , "You did not enter a hexadecimal number :-(")
EndIf

;Declare each of the characters in the string
$char = StringSplit($input, "")

$hFile = FileOpen("add.log", 2)
If @error Then
    MsgBox(0, "Error", "Unable to open add.log")
    Exit
EndIf

For $i = 1 To $char[0]
    ;Aquire characters for substitution.
    $charX = _zero2one($char[$i])
    $Len = StringLen($charX)

    ;Checks whether $char1 is 'F', which can not be substituted.
    If $charX <> "X" Then
        $count = 1
        $Len = StringLen($charX)
        $Len = $Len + 1
        Do
            $char2add = StringMid($charX, $count , 1)
            ;Log each new string entry.
            FileWriteLine2($hFile, $char, $i, $char2add)
            If @error Then
                MsgBox(0, "Error", "Unable to write to add.log")
                Exit
            EndIf
            $count = $count + 1
        Until $count = $Len
    EndIf
Next

FileClose($hFile)

;Returns a choice of substitution characters, Substituting each value results in changing a zero to 1 in binary.
Func _zero2one($char)
    Switch $char
        Case '0'
            $char = "1248"
        Case '1'
            $char = "359"
        Case '2'
            $char = "36A"
        Case '3'
            $char = "7B"
        Case '4'
            $char = "56C"
        Case '5'
            $char = "7D"
        Case '6'
            $char = "7E"
        Case '7'
            $char = "F"
        Case '8'
            $char = "9AC"
        Case '9'
            $char = "BD"
        Case "A"
            $char = "BE"
        Case "B"
            $char = "F"
        Case "C"
            $char = "DE"
        Case "D"
            $char = "F"
        Case "E"
            $char = "F"
        Case "F"
            $char = "X"
    EndSwitch
    Return $char
EndFunc

Func FileWriteLine2($file, $chars, $i, $char2add)
    $msg = ""
    For $j = 1 To $chars[0]
        If $i == $j Then; replace
            $msg &= $char2add
            ContinueLoop
        EndIf
        $msg &= $chars[$j]
    Next
    FileWriteLine($file, $msg)
EndFunc

I believe the problem was something to do with numerical characters in each case were being read as numbers. Actually they are replacement characters. I was getting the wrong results. Putting the numbers back between quotation marks has fixed the problem. Perhaps there is a more elegant way to do this, but now the script works again. It appears that the letters in the input were being interpreted as having a value of zero.

Thanks a lot, hopefully I will now be able to incorporate some of these techniques. I like the addition of the second function. I figured that something like that was necessary, but I wasn't sure where to begin. Very Grateful. :)

Edited by czardas

Share this post


Link to post
Share on other sites

1248 is just a string of digits returned by the function, and not a number. Each digit in the string can be used as a replacement. The actual substitutions do not occur within the function itself. The return value 1248 represents 4 replacements for the binary string 0000 viz:

Does that make any more sense? :)

Yeah, I missed that completely. I guess partially from not getting what you wanted it to do.

Glad you got it working.

:P


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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Glad you got it working.

:P

Yeah me too! Though I'm still trying to figure out why it's working? :)

I have edited my first post. I guess I could have explained it better in the first place.

Edited by czardas

Share this post


Link to post
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
Sign in to follow this  
Followers 0