Mar 15, 2014 The problem with your code is that the data in the buff is ASCII characters, not the individual binary digits. You need to subtract '0' from each digit before performing a conversion to BCD. Uint8t HourTens = (buff0 - '0'); // && 0xF0, will be 1 Note that each element takes its own byte, so you don't need to mask anything. Sep 14, 2011 Because the result to BCD should be 0000 0000 0100, rather than just 0100. If someone can help me with some C algorithm or snippet of code with some explanation, I would really appreciate it. Sep 13, 2011 at 2:22am UTC.
I know you can make use of this table to convert decimal to BCD:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Is usually there a formula for this transformation or you have got to simply use the table? Im trying to write some code for this transformation but Im not really certain how to perform the mathematics for it. Suggestions?
user1899861
BrambleBramble
10 Solutions
You understand the Binary numeral program, don'capital t you?
Especially have a appearance at this section.
EDIT: Also note KFro'beds comment that the lower nibble (= 4 parts) of the binary ASCII portrayal of numerals can be in BCD. This can make conversions BCD lt;-gt; ASCII really easy as you simply possess to include/remove the top 4 pieces:
schnaaderschnaader
Simon PeverettSimon Peverett
This is from the tiny controller world. Notice that ideals are curved in the department. For instance 91 to BCD would become 91/10. 16 = 144 + 91%10 = 145. Transformed to Binary will be 10010001.
nmushovnmushov
Usually when someone says they desire to convert from decimal to BCD, they're talking about even more than one decimal digit.
BCD can be often packed into two decimal numbers per byte (because 0.9 suit in 4 bits, as you've shown), but I think it's more normal to make use of an number of bytes, one per decimal digit.
An n-bit unsigned binary amount will match into ceil(n.log2(10)) = ceil(n/log10(2)) decimal digits. It will also fit in ceil(n/3) = floor((n+2)/3)) decimal numbers, since 2^3=8 can be much less than 10.
With that in brain, here's how I'd get the decimal numbers of an unsigned int:
Of training course, if you know the width of your int type, you may prefer fixed size arrays. There's i9000 also no reason to reverse at all if you can keep in mind the reality that the 0th digit is usually the very least significant and invert only on input/output. Maintaining the least substantial digit as the 1st simplifies digit-wise math ops in the case that you don't use a set number of digits.
If you want to symbolize '0' as the one '0' decimal digit instead than the clear digit-string (either is valid), then you'd check specifically for a0.
Jonathan GraehlJonathan Graehl
If you wish two decimal digits per byte, and 'unsigned' can be half the dimension of 'unsigned long' (use uint32 and uint64 typedefs if you would like):
This results in you with the least significant (unit) decimal number in the minimum significant half-byte. You can furthermore implement the loop a set amount (10 for uint32) of moments, not ending early when only 0 bits are still left, which would permit the optimizer to unroll it, but that's not so quick if your figures are usually sluggish.
Jonathan GraehlJonathan Graehl
Would something like this work for your conversion?
CTTCTT
This code encodes and decodes. Standards are usually as follows.
- 45 clocks for the round-trip
- 11 clocks for unpacking BCD to uint32t
- 34 clocks for packaging uint32t into BCD
I utilized an uint64t to store the BCD here. Very convenient and set thickness, but not very area efficient for large tables. Group the BCD numbers, 2 to char for that.
NOTE:It shows up that it's impossible, also with 64-bit ints, to change left more than 32 pieces, but luckily, it's entirely probable to increase by some aspect of 16 - which enjoyably offers the preferred effect. It't also very much faster. Go physique.
user1899861
I understand this offers been formerly responded but I've prolonged this for unsigned ints of various sizes using a template to develop the particular program code.
Toby DemandingAndrew Demanding
Here is a macro for uint16t, so that it gets examined at compile-time (supplied that u is definitely a pre-defined constant). This wants with december2bcd from above upward to 9999.
Beam FoulkBeam Foulk
randzrandz
Not the reply you're also searching for? Browse other questions marked cbcd or question your personal issue.
BCDConvert.java
/.Several BCD Transformation util. methods./ |
open publicclassBCD |
/. |
. lengthy quantity to bcd byte number e.h. 123 -gt; (0000) 0001 0010 0011 |
. elizabeth.h. 12 -gt; 0001 0010 |
./ |
publicstationarybyteDecToBCDArray(lengthynum) |
intnumbers=0; |
lengthytemp=num; |
while(temp!=0) |
numbers; |
temp/=10; |
intbyteLen=numbers%20?digits/2:(digits+1)/2; |
booleanisOdd=digits%2!=0; |
bytebcd=freshbytebyteLen; |
for(inti=0; ilt;digits; i) |
bytetmp=(byte) (num%10); |
if(i digits-1amp;amp;isOdd) |
bcdi/2=tmp; |
elseif(i%20) |
bcdi/2=tmp; |
else |
bytefoo=(byte) (tmplt;lt;4); |
bcdi/2=foo; |
num/=10; |
for(inti=0; ilt;byteLen/2; i) |
bytetmp=bcdi; |
bcdi=bcdbyteLen-i actually-1; |
bcdbyteLen-i-1=tmp; |
returnbcd; |
open publicstationaryLineBCDtoString(bytebcd) |
StringBuffersb=newStringBuffer; |
bytehigh=(byte) (bcdamp;0xy0); |
highgt;gt;gt;=(byte)4; |
high=(byte) (highamp;0x0f); |
bytereduced=(byte) (bcdamp;0x0f); |
sb.append(higher); |
sb.append(low); |
returnsb.toString; |
general publicstationaryChainBCDtoString(bytebcd) |
StringBuffersb=newStringBuffer; |
for(inti=0; ilt;bcd.duration; i) |
sb.append(BCDtoString(bcdi)); |
returnsb.toString; |
commentedSep 26, 2015
this is certainly not functioning properly, if you wear't use the built in function to transform to string making use of the returned variety. if you pass 255, 123 or any three digit to DecToBCDArray it gives a byte selection of just two digits which should end up being three however if you move the returned byte variety to BDCtoString it regenerates the quantity successfully. Therefore basically its worthless if you wish to use it with something else(which is certainly generally the case). |
commentedApril 15, 2015
Hi, any three or four digit amount such as 123 or1234 will be a2 bytevalue because each BCD digit is only achew (4-parts). If you're functioning with embedded devices like as a Point of sale device (POS), for which I acquired written this electricity class, the device demands a continous stream of bytes in this file format for BCD ideals. i.age. you'd even more likely need to get rid of byte at your UART or whatever serial port than convert to Line.I incorporated a primary mehtod to testDecToBCDArray, and it outputs: In the wikipedia article for BCD there's an illustration as such: The check technique I integrated asserts equally with that of the Wikipedia instance. You're best in that on occasion it might become more convenient to output a byte (8-little bit value) for each digit in the given number rather of concatenating each BCD worth into a byte, therefore that the harasser can do this at may. I'll consist of such a technique in the class document. Thanks a lot. |
commentedApril 18, 2017
When I make use of this code my finish result starts with a top zero. Is it proper? Does it need to begin with a top zero? |
Indication up for freeto sign up for this conversation on GitHub. Currently have got an accounts? Sign in to comment