Jump to content
Sign in to follow this  
erifash

Why doesn't this work!

Recommended Posts

I have spent about two hours trying to figure out why this doesn't work. It's very frusterating. Can someone please help? Here is the code:

MsgBox(0, "", "Z = " & str2bin("Z") & @CRLF & str2bin("Z") & " = " & bin2str(str2bin("Z")))

Func str2bin($str)
  If $str <> String($str) Then Return 0
  For $i = StringLen($str) - 1 to 0 step -1
    $bin = $bin & _ToBase(2, Asc(StringTrimRight(StringTrimLeft($str, StringLen($bin)), $i)))
  Next
  Return $bin
EndFunc

Func bin2str($bin)
  If $bin <> String($bin) or StringLen($bin) / 7 <> Int(StringLen($bin) / 7) Then Return 0
  For $i = StringLen($bin) - 7 to 0 step -7
    $str = $str & Chr(_ToBase(10, StringTrimRight(StringTrimLeft($bin, StringLen($str) * 7), $i)))
  Next
  Return $str
EndFunc

Func _ToBase($pBase, $pNumber)
  $sX = ""
  Do
    $nNext = Int($pNumber / $pBase)
    $nRem = Mod($pNumber, $pBase)
    If $nRem > 9 then
      $sX = Chr(Asc("A") - 10 + $nRem) & $sX
    Else
      $sX = String($nRem) & $sX
    Endif
    $pNumber = $nNext
  Until $pNumber = 0
  Return $sX
EndFunc

If you try it, it will encrypt "Z" and decrypt it as "B". How can I fix this? I've spent a lot of time trying to get this to work.

Btw, I'm trying to encrypt a string into binary so I can use Paint to represent it with black and white pixels ( black = 1 & white = 0 ).

Thanks!

Share this post


Link to post
Share on other sites

Func bin2str($bin)
  If $bin <> String($bin) or StringLen($bin) / 7 <> Int(StringLen($bin) / 7) Then Return 0
  For $i = StringLen($bin) - 7 to 0 step -7
    $str = $str & Chr(_ToBase(10, StringTrimRight(StringTrimLeft($bin, StringLen($str) * 7), $i)))
  Next
  Return $str
EndFunc
Two comments upon quick inspection

StringLen($bin) / 7 <> Int(StringLen($bin) / 7) ????

Why don't you just say StringLen($bin) <> 7

Also, are you sure the following is correct?:

For $i = StringLen($bin) - 7 to 0 step -7


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Two comments upon quick inspection

StringLen($bin) / 7 <> Int(StringLen($bin) / 7) ????

Why don't you just say StringLen($bin) <> 7

Also, are you sure the following is correct?:

  For $i = StringLen($bin) - 7 to 0 step -7

<{POST_SNAPBACK}>

#1: To make sure that there is not an odd amount of characters to be decrypted because one character encrypts into seven binary digits.

#2: Yes, AutoIt interprets it as For $i = ( StringLen($bin) - 7 ) to 0 step -7 ;(negative seven)

Share this post


Link to post
Share on other sites

Shouldn't that be 8 instead of 7? In order to store any ASCII character, it needs to be 8 bits (2^8=256=sizeof ASCII character set).

Edit: Also, I would think doing this would ensure that a string is properly encoded in binary:

If Mod(StringLen($string), 8) Then ...

A properly encoded string will always be in multiples of 8.

Another suggestion: Instead of doing all that trimming crap, why not just set up a While loop like this:

; Assume string has valid binary data
While StringLen($string)
    Local $chunk = StringLeft($string, 8)
    $string = StringTrimLeft($string, 8)
  ; Decode $chunk
WEnd

Once all characters in $string are consumed, the loop is exited.

Edit 2: I think _ToBase() is screwed up. It converts to binary fine, but it doesn't convert back properly.

By the way, here is a new version of str2bin/bin2str which is much cleaner, IMO:

Func str2bin($string)
    Local $res = ""
    While StringLen($string)
        Local $chunk = StringLeft($string, 1)
        $string = StringTrimLeft($string, 1)
        $res = $res & _ToBase(2, Asc($chunk))
    WEnd
    Return $res
EndFunc

Func bin2str($string)
    Local $res = ""
    If Not Mod(StringLen($string), 8) Then
        Return $res
    EndIf
    
    While StringLen($string)
        Local $chunk = StringLeft($string, 8)
        $string = StringTrimLeft($string, 8)
        $res = $res & Chr(_ToBase(10, $chunk))
    WEnd
  Return $res
EndFunc
Edited by Valik

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...