A conversion question (Integer to BCD-string to byte)

Hi all,

I'm currently stuck on a small problem....I know the solution should not be too hard to come by, but I just can't figure it out....

I'm working on an application that communicates with a piece of hardware through the serial port. The communication's up and running, no problems there. There is one problem though, I need to be able to set the time and date of the hardware, and the hardware expects that in bcd (binary coded decimal). The communication expects bytes.

I've written a conversion function, that takes an integer value, and returns a string, which contains a two-digit bcd code. What I want is to convert that string to a byte, and send that byte to the serialport, without any more calculation, to be sure I don't lose the bcd value.

If anyone can think of a better way, I'd love to hear it, this is getting silly!

In my opinion it should be possible to just create a byte out of the two sets of 4 bits the bcd code is made up of, but I'm creating my own blind spots or something...


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't understand yet what you want... So, you've done this:

LowBCD.Text := IntToHex(Display,4);

to get the BCD... Now you need which byte?
BinskyAuthor Commented:

Actually, what I've done is this :

function TSetupForm.IntToBCD(Value : integer) : string;
   Result := IntToHex(Value div 10, 1) +IntToHex(Value mod 10, 1);

Now I've got a string containing the BCD translation of any integer, but what I want to have is one byte containing the same info...

BinskyAuthor Commented:

I've answered the question myself...

The conversion is :

BCD := ((16*(DecimalValue div 10)) + (DecimalValue mod 10));
DEC := ((10*(BCDValue div 16))      + (BCDValue mod 16));


(Do I get the points myself?) ;-)
PAQed, with points refunded (125)

E-E Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.