taith Posted February 11, 2010 Share Posted February 11, 2010 does anyone know if there is a builtin ascii to binary converter? i know i can just $a=array('a'=>b'01100001','b'=>b'01100010'); but is there a more resource efficient way of doing this? Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/ Share on other sites More sharing options...
jl5501 Posted February 11, 2010 Share Posted February 11, 2010 is your string, a string representation of a binary number? If so, I have some code I wrote years ago, I can share with you Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010905 Share on other sites More sharing options...
Mchl Posted February 11, 2010 Share Posted February 11, 2010 echo decbin(ord('a')); Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010912 Share on other sites More sharing options...
taith Posted February 11, 2010 Author Share Posted February 11, 2010 no, its a string... like "test" becoming "01110100011001010111001101110100" and decbin(ord('a')) works, but it seems to remove preceding 0s but allinall, it'll do Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010940 Share on other sites More sharing options...
Mchl Posted February 11, 2010 Share Posted February 11, 2010 add sprintf to the mix to pad it with leading zeroes. Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010945 Share on other sites More sharing options...
taith Posted February 11, 2010 Author Share Posted February 11, 2010 thank ya kindly for those that have need of it... <? function exc($string){ $out=array(); for($i=0; $i<strlen($string); $i++) $out[]=$string{$i}; return $out; } function stb($s){ $s=exc($s); foreach($s as $k=>$v) $s[$k]=str_pad(decbin(ord($v)), 8, "0", STR_PAD_LEFT); return implode('',$s); } print_r(stb('test')); ?> Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010949 Share on other sites More sharing options...
RussellReal Posted February 11, 2010 Share Posted February 11, 2010 I would add spaces.. Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010951 Share on other sites More sharing options...
taith Posted February 11, 2010 Author Share Posted February 11, 2010 I would add spaces.. spaces are only for user viewing... processer doesnt care Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.