Jump to content
Sign in to follow this  
layer

StringReplace help...

Recommended Posts

layer

im just making a function... and for some reason =/ its always returning 0... heres the code

Func Text2Binary($text)
    $letters= StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
    $split= StringSplit("01100001,01100010,01100011,01100100,01100101,01100110,01100111,01101000,01101001,01101010,011010


11,01101100,01101101,01101110,01101111,01110000,01110001,01110010,01110011,01110100,01110101,0111011


0,01110111,01111000,01111001,01111010", ",")
    For $i = 1 to $letters[0]
        $out= StringReplace($text, $letters[$i], $split[$i])
    Next
    Return $out
EndFunc
$text2bin= Text2Binary("testing")
    MsgBox(0, "Text2Binary", Text2Binary($text2bin))
    
Func Binary2Text($binary)
    $letters= StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
    $split= StringSplit("01100001,01100010,01100011,01100100,01100101,01100110,01100111,01101000,01101001,01101010,011010


11,01101100,01101101,01101110,01101111,01110000,01110001,01110010,01110011,01110100,01110101,0111011


0,01110111,01111000,01111001,01111010", ",")
    For $i = 1 to $split[0]
        $out= StringReplace($binary, $split, $letters)
    Next
    Return $out
EndFunc
$bin2text= Binary2Text("01100001,01100010")
MsgBox (0, "Binary2Text", Binary2Text($bin2text))

maybe it could be coded better, but to me, what ever gets the job done, works, so does anyone have any clue as to why this is always returning 0...? thanks! :)

Edited by layer

FootbaG

Share this post


Link to post
Share on other sites
layer

oops, i think i found the problem.. quite obvious actually, hang on and ill check :)!


FootbaG

Share this post


Link to post
Share on other sites
jpm

im just making a function... and for some reason =/ its always returning 0... heres the code

Func Text2Binary($text)
    $letters= StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
    $split= StringSplit("01100001,01100010,01100011,01100100,01100101,01100110,01100111,01101000,01101001,01101010,011010


11,01101100,01101101,01101110,01101111,01110000,01110001,01110010,01110011,01110100,01110101,0111011


0,01110111,01111000,01111001,01111010", ",")
    For $i = 1 to $letters[0]
        $out= StringReplace($text, $letters[$i], $split[$i])
    Next
    EndFunc
    MsgBox(0, "Text2Binary", Text2Binary("testing"))

maybe it could be coded better, but to me, what ever gets the job done, works, so does anyone have any clue as to why this is always returning 0...? thanks! :D

<{POST_SNAPBACK}>

your function as no explicit return

so the implicit return is 0

You have to have an return something :) if you want something different from 0

Share this post


Link to post
Share on other sites
layer

nevermind... :huh: doesnt work, heres the full code though..

Func Text2Binary($text)
    $letters= StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
    $split= StringSplit("01100001,01100010,01100011,01100100,01100101,01100110,01100111,01101000,01101001,01101010,011010


11,01101100,01101101,01101110,01101111,01110000,01110001,01110010,01110011,01110100,01110101,0111011


0,01110111,01111000,01111001,01111010", ",")
    For $i = 1 to $letters[0]
        $out= StringReplace($text, $letters[$i], $split[$i])
    Next
    Return $out
EndFunc
$text2bin= Text2Binary("testing")
    MsgBox(0, "Text2Binary", Text2Binary($text2bin))
    
Func Binary2Text($binary)
    $letters= StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
    $split= StringSplit("01100001,01100010,01100011,01100100,01100101,01100110,01100111,01101000,01101001,01101010,011010


11,01101100,01101101,01101110,01101111,01110000,01110001,01110010,01110011,01110100,01110101,0111011


0,01110111,01111000,01111001,01111010", ",")
    For $i = 1 to $split[0]
        $out= StringReplace($binary, $split, $letters)
    Next
    Return $out
EndFunc
$bin2text= Binary2Text("01100001,01100010")
MsgBox (0, "Binary2Text", Binary2Text($bin2text))

not sure in Binary2Text would work even if the code were right... so any suggestions? thanks!

EDIT: hey thanks jpm! ill give it a try! :D you responded as i was typing :(

EDIT2: :):D ok, Text2Binary works! thanks a million jpm, but Binary2Text doesnt :( i updated the code in this post... i dont think it may even be possible because theres only 0's and 1's where as the alphabet has all different letters, not just a and b... :(

Edited by layer

FootbaG

Share this post


Link to post
Share on other sites
layer

actually, it doesnt work... oh well... it returns testing, but it should return testing in binary..! geez, Text2Binary!


FootbaG

Share this post


Link to post
Share on other sites
SlimShady

I'm gonna fix it for you. But promise me that you'll study the code.

Edit:

I attached the working script.

Edited by SlimShady

Share this post


Link to post
Share on other sites
layer

thanks Slim! heres what i came out with, edited with your additions, but first, ill just say what i changed and why :)

1.) $out must = $text because that's what your returning, so $out = $text means your returning the text, that was replaced by binary...

2.) Same applies for $out = $binary :D

3.) I didn't have any [$i] arrays in $split and $letters in the Binary2Text...

4.) Changed:

$bin2text= Binary2Text("01100001,01100010")
MsgBox (0, "Binary2Text", Binary2Text($bin2text))

to

$bin2text = Binary2Text($text2bin)
MsgBox (0, "Binary2Text", $bin2text)

so that you could get the translated "testing" to binary, and then convert it back to text...

5.) and i think that pretty much wraps it up, don't you?

thanks a million Slim! :D


FootbaG

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.