Solved

Convert ascii to bcd(binary coded decimal)

Posted on 2004-04-12
5
4,268 Views
Last Modified: 2007-12-19
Hello everyone

I have a variable $number = 4012;

Is there a way with php to convert this ascii number to a bcd(binary coded decimal)

Thanks in advance,

jmsloan
0
Comment
Question by:jmsloan
5 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10805218

If you just want to print a binary number

printf("binary num = %b",$number);
0
 
LVL 3

Author Comment

by:jmsloan
ID: 10805621
But there is a difference between BCD and a binary number.
0
 
LVL 5

Accepted Solution

by:
TheClickMaster earned 50 total points
ID: 10807227


       $number = "1234";
      
       for($i = 0; $i < strlen($number); $i ++)
       {
            $output .= str_pad(decbin($number[$i]), 8, "0", STR_PAD_LEFT)." ";       
       }
      
       echo $output;



Sample output is:

00000001 00000010 00000011 00000100
0
 
LVL 14

Expert Comment

by:ThG
ID: 10812211
TheClickMaster, that's a poor example as it assumes $number is a STRING. Anyway it should work so you can fix it by adding this before "for":

settype($number, 'string');
0
 
LVL 14

Expert Comment

by:ThG
ID: 10812254
I HAD to write my own, how beautiful :-)

function dec2bcd($number) {
  $e = 0;
  $ret = 0;
  while ($number) {
    $d = $number % 10;
    $number = floor($number / 10);
    for ($i = 0; $i < $e; $i++) $d <<= 4;
    $ret |= $d;
    $e++;
  }
  return $ret;
}

$dec = 432;
printf("%x\n", dec2bcd($dec));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now