erifash 1 Posted January 16, 2005 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! My UDFs:_FilePrint() | _ProcessGetName() | _Degree() and _Radian()My Scripts:Drive Lock - Computer Lock Using a Flash DriveAU3Chat - Simple Multiuser TCP ChatroomStringChunk - Split a String Into Equal PartsAutoProxy - Custom Webserver Share this post Link to post Share on other sites
CyberSlug 6 Posted January 16, 2005 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 EndFuncTwo comments upon quick inspectionStringLen($bin) / 7 <> Int(StringLen($bin) / 7) ????Why don't you just say StringLen($bin) <> 7Also, 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
erifash 1 Posted January 16, 2005 Two comments upon quick inspectionStringLen($bin) / 7 <> Int(StringLen($bin) / 7) ????Why don't you just say StringLen($bin) <> 7Also, 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) My UDFs:_FilePrint() | _ProcessGetName() | _Degree() and _Radian()My Scripts:Drive Lock - Computer Lock Using a Flash DriveAU3Chat - Simple Multiuser TCP ChatroomStringChunk - Split a String Into Equal PartsAutoProxy - Custom Webserver Share this post Link to post Share on other sites
erifash 1 Posted January 16, 2005 ... I can't figure this out!!! ...help... My UDFs:_FilePrint() | _ProcessGetName() | _Degree() and _Radian()My Scripts:Drive Lock - Computer Lock Using a Flash DriveAU3Chat - Simple Multiuser TCP ChatroomStringChunk - Split a String Into Equal PartsAutoProxy - Custom Webserver Share this post Link to post Share on other sites
Valik 478 Posted January 16, 2005 (edited) 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 WEndOnce 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 January 16, 2005 by Valik Share this post Link to post Share on other sites