Convert a binary coded decimal (BCD) string into bytes


/ Published in: C#
Save to your folder(s)

BCD is where the hex value of a byte represents two integer values between zero and nine; e.g., 0x98 -> "98" (whereas its integer value is actually 152). The method throws an exception if non-BCD data is input.

You can also check out the partner method, [Convert bytes into a binary coded decimal (BCD) string](http://snipplr.com/view/16802/convert-bytes-into-a-binary-coded-decimal-bcd-string/).

Update:
Unfortunately, there doesn't seem to be a built-in method for doing this (there is for the converse; see System.BitConverter.ToString). The System.BitConverter.GetBytes(char) overload returns the byte value of the character, rather than a BCD byte array (`BitConverter.GetBytes('A')` returns a byte array holding `[65, 0]`, not [0x0A] as we would want).

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.