> -----Original Message----- > From: Graham North [SMTP:graham_from_malton@YAHOO.CO.UK] > Sent: Friday, September 26, 2003 1:46 PM > To: PICLIST@MITVMA.MIT.EDU > Subject: [PIC]: Hi Tech BCD to Bin routine? > > Hi everyone, > > Can someone help me by telling me how to convert BCD to Binary? I'm having > a bad day! :-( > > Cheers > > Graham > Is the BCD packed (i.e. two digits per byte) or unpacked (one digt per byte) and how many digits? The technique is very simple if you are not overly concerned about efficiency (speed in this case). unsigned int Bcd2Bin(unsigned int bcd) { // convert 4 digit packed BCD to binary unsigned int bin; bin= bcd & 0x000F; // get units bdc >>= 4; bin+= (bcd & 0x000F) * 10; // add tens bdc >>= 4; bin+= (bcd & 0x000F) * 100; // add hundreds bdc >>= 4; bin+= (bcd & 0x000F) * 1000; // add thousands return bin; } This is as basic as it gets, you should be able to significantly speed this up with a little thought. The method is the same for unpacked BCD, but obviously you only have to deal with the lower nibble in each byte. For some very fast methods check out the piclist web site. You will almost certainly need to use inline assembly to use them however. Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= Any questions about Bookham's E-Mail service should be directed to postmaster@bookham.com. -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.