Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to make a converter convert ACSii to and from Binary.
Old 01-16-2008, 05:24 PM How to make a converter convert ACSii to and from Binary.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay, i learn binary how each 8 digit section worked out blah blah added up and then with the total u can use it to get a ACSii character form the table...

But how can i do all this with a converter?

and where would i get a FULL acsll table from?

i think i could understand that i would take the inputted binary string split it into 8 digit segments and then split that into 8 and if the first one is one add 128 to the total and so on to get the total for that char

and then compare this to a table...

But firstly how do i split a string every X chars so i can create a array with each 8 digit binary part so i can then make my loop to go through de-coding it...
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 01-16-2008, 06:37 PM Re: How to make a converter convert ACSii to and from Binary.
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
So you want to convert a binary representation of a string to text.

So in ASCII table 010100000100100001010000 represents the string 'PHP' .

PHP Code:
 // convert a binary representation of a string back into it's original form.
function bin2asc($binaryInput$byteLength=8)
{
    if (
strlen($binaryInput) % $byteLength)
    {
        return 
false;
    }
    
    
// why run strlen() so many times in a loop? Use of constants = speed increase.
    
$strSize strlen($binaryInput);
    
$origStr '';

    
// jump between bytes.
    
for($x=0$x<$strSize$x += $byteLength)
    {
        
// extract character's binary code
        
$charBinary substr($binaryInput$x$byteLength);
        
$origStr .= chr(bindec($charBinary)); // conversion to ASCII.
    
}
    return 
$origStr;
}



$bin "010100000100100001010000"// binary of 'PHP'
$ascOut bin2asc($bin);
echo 
$ascOut// echos word 'PHP' 

http://www.phpfreaks.com/quickcode/code/244.php
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 01-17-2008, 12:17 PM Re: How to make a converter convert ACSii to and from Binary.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
can you explain what this bits doing?

ive never had to use the for() loop before dont really get it :s

Thanks, (TP on way )
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-17-2008, 01:11 PM Re: How to make a converter convert ACSii to and from Binary.
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
First statement checks the length of binary string. If not divisible by 8 then string will be invalid and terminates.

First iteration of for loop evaluates first 8 characters and converts binary string into decimal equivalent (bindec).Chr then returns the ASCII of this decimal and adds to $origStr.

The for loop then repeats until end of binary string is reached. $origStr is returned and echoed as ASCII character string.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 01-17-2008, 02:03 PM Re: How to make a converter convert ACSii to and from Binary.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay what i ment in particular was this:
PHP Code:
for($x=0$x<$strSize$x += $byteLength
</SPAN>

what i ment was what makes that bit work?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-17-2008, 02:52 PM Re: How to make a converter convert ACSii to and from Binary.
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
For loop will execute nested statement providing the second condition $x<$strSize evaluates to true.

Basically sets a counter , tests the value of counter and then adds a value to counter.

So in above example string length is 24
First iteration: $x is initialised and set at 0; $x is tested (0 is less than 24) and evaluates to true. It then adds 8 to counter $x and executes nested statement once.

Second iteration : $x is now set to 8; is tested and evaluates to true and executes. And so on

Result is that loop executes 3 times.

http://uk.php.net/for
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 01-17-2008, 02:54 PM Re: How to make a converter convert ACSii to and from Binary.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
o i see so basically thats the bit which makes it convert the binary for each 8 digits

Just a thing, how could i have it so IF the string isnt / by 8 (and is there-fore corrupt) to try and do it by removing whatever extra digits are on the end until it is / by 8?


Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 01-17-2008 at 02:55 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to How to make a converter convert ACSii to and from Binary.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 2.77914 seconds with 12 queries