FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

from integer to binary

 


Antoine_935
I'm looking for a table of translation : how to translate interger from 0 to 255 into binary code ==> 2 = 10 etc...
I really need this table, please help me !
izcool
Might this help ? Not sure.

http://nickciske.com/tools/binary.php

- Mike.
Grimboy
Why do you need a table, it would be a very simple thing to do programaticaly and a function propebly already exists somewhere.
kv
use decbin() function. Simple. Isn't it?
Antoine_935
@izcool : not it doesn't help me at the moment, but maybe I'll need it on some day. thank you !

@Grimboy : at the moment I wrote this message, I did not know the algoritm. but now I've found it, so I'll do what you said Wink

@kv : I would have used this function if it was in PHP, but it is in Python.
kv
Hope this link helps

http://www.daniweb.com/code/snippet285.html
XeononyX
Hello fellow coders! I stumbled upon this post and thought I would respond.. Translating Integer to Binary really isn't that tough by hand especially from 0 to 255

0 to 255 will always be 8 "bits"
now just imagine these bits have values, and these values double at each new bit. (These numbers are so familiar to computer users because computers go by sets similar to these)

128 , 64 , 32 , 16 , 8 , 4 , 2 , 1

check digits left to right 1: a) is your number greater than or equal to it (b) if so add a 1 to that bit or a 0 if lower. (c) then subtract the value of the digit from your number if you put a one there.

then repeat till done.


Code:

example : 169

1 ) ( 169>=128 ) YES b ) 1??????? c ) 169-128=41
2 ) ( 41 >=64  ) NO  b ) 10?????? c ) 41
3 ) ( 41 >=32  ) YES b ) 101????? c ) 41-32=9
4 ) ( 9  >=16  ) NO  b ) 1010???? c ) 9
5 ) ( 9  >=8   ) YES b ) 10101??? c ) 9-8=1
6 ) ( 1  >=4   ) NO  b ) 101010?? c ) 1
7 ) ( 1  >=2   ) NO  b ) 1010100? c ) 1
8 ) ( 1  >=1   ) YES b ) 10101001 c ) 1-1=0


there you go! 169 = 10101001
dandelion
Please try the following code.
Code:
<?php
  functtion dec2bin($d) {
    $b = '';
    while ($d>0) {
      $r = $d % 2;
      $d = floor($d / 2);
      $b = $r.$b;
    }
    return $b;
  }

  for ($i=0; $i<256; $i++) {
    echo $i.'=>'.dec2bin($i).'<br />';
  }
?>
Defsanguje
sprintf(); could help? im not sure, but with sprintf you can translate integer value to hex value...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.