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? Quote 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 Quote 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')); Quote 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 Quote 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. Quote 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')); ?> Quote 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.. Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/191803-string-to-binary-converter/#findComment-1010958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.