Created
August 18, 2012 15:12
-
-
Save flesch/3387431 to your computer and use it in GitHub Desktop.
Convert to and from a binary sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function str2seq(str:String):String { | |
var seq:String = "", byte:String, len:Number = str.length, i:Number = 0; | |
for (; i<len; i++) { | |
byte = parseInt(ord(str.charAt(i)), 10).toString(2); | |
seq += "00000000".substr(0, 8-byte.length) + byte; | |
} | |
return seq; | |
} | |
function seq2str(seq:String):String { | |
var str:String = "", len:Number = seq.length, i:Number = 0; | |
for (0; i<len; i+=8) { | |
str += chr(parseInt(seq.substr(i, 8), 2)); | |
} | |
return str; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function str2seq(str:String):String { | |
var seq:String = "", byte:String, len:Number = str.length, i:Number = 0; | |
for (; i<len; i++) { | |
byte = (str.charCodeAt(i)).toString(2); | |
seq += "00000000".substr(0, 8-byte.length) + byte; | |
} | |
return seq; | |
} | |
function seq2str(seq:String):String { | |
var str:String = "", len:Number = seq.length, i:Number = 0; | |
for (0; i<len; i+=8) { | |
str += String.fromCharCode(parseInt(seq.substr(i, 8), 2)); | |
} | |
return str; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function str2seq(str) { | |
var seq = '', byte, len = str.length, i = 0; | |
for (; i<len; i++) { | |
byte = (str.charCodeAt(i)).toString(2); | |
seq += '00000000'.substr(0, 8-byte.length) + byte; | |
} | |
return seq; | |
} | |
function seq2str(seq) { | |
var str = '', len = seq.length, i = 0; | |
for (0; i<len; i+=8) { | |
str += String.fromCharCode(parseInt(seq.substr(i, 8), 2)); | |
} | |
return str; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function str2seq($str) { | |
$seq = ''; | |
for($i=0; $i<strlen($str); $i++){ | |
$byte = decbin(ord($str[$i])); | |
$seq .= substr('00000000', 0, 8-strlen($byte)) . $byte; | |
} | |
return $seq; | |
} | |
function seq2str($seq) { | |
if (strlen($seq) % 8 != 0) { | |
return NULL; | |
} | |
$str = ''; | |
for($i=0; $i<strlen($seq); $i+=8) { | |
$str .= chr(bindec(substr($seq, $i, 8))); | |
} | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment