ID:195032
 
//Title: Binary to ASCII  (bin2ascii)
//Contributed By: Haywire
//Inspired By: http://www.theskull.com/javascript/ascii-binary.html

/*
This procedure turns binary (in string form) into ASCII.

For example:
01000001 = 65
65 = A (According to ASCII)

However, this procedure is not limited to one letter, you may also write words, sentences, paragraphs, and so on.

*/


// You may use this site to help gain a better understanding at what this procedure performs:
// http://www.theskull.com/javascript/ascii-binary.html

proc
bin2ascii(var/t)
.=""
t = StripWhitespace(t)
for(var/i=1;i<=length(t);i+=8)
.+=ascii2text(bin2dec(copytext(t,i,i+8)))

StripWhitespace(string)
var/last=1
for(var/find=findtext(string," ");find;find=findtext(string, " ", last))
. += copytext(string, last, find)
last = find+1
. += copytext(string,last)



// Courtesy of Popisfizzy for this procedure:
// http://www.byond.com/members/Jtgibson/forum?id=189&view=1
bin2dec(n)
var/len = length(n)
. = 0
for(var/a = len, a > 0, a --)
. += (text2ascii(n, a) == 49 && (1 << (len - a)))

mob
verb
Binary2Ascii()
world << bin2ascii("01001000 01100101 01101100 01101100 01101111 00100000 01000010 01011001 01001111 0100111001000100 00100001") // "Hello BYOND!" with spaces in binary.
world << bin2ascii("010010000110010101101100011011000110111100100000010000100101100101001111010011100100010000100001") // "Hello BYOND!" without spaces in binary.
// Both output the same string.

world << bin2ascii("01000001")// "A" (ascii: 65)
world << bin2ascii("01000010")// "B" (ascii: 66)