Converting a int to a BCD byte array
Asked Answered
G

9

5

I want to convert an int to a byte[2] array using BCD.

The int in question will come from DateTime representing the Year and must be converted to two bytes.

Is there any pre-made function that does this or can you give me a simple way of doing this?

example:

int year = 2010

would output:

byte[2]{0x20, 0x10};
Genevieve answered 15/3, 2010 at 15:32 Comment(0)
C
11
    static byte[] Year2Bcd(int year) {
        if (year < 0 || year > 9999) throw new ArgumentException();
        int bcd = 0;
        for (int digit = 0; digit < 4; ++digit) {
            int nibble = year % 10;
            bcd |= nibble << (digit * 4);
            year /= 10;
        }
        return new byte[] { (byte)((bcd >> 8) & 0xff), (byte)(bcd & 0xff) };
    }

Beware that you asked for a big-endian result, that's a bit unusual.

Chenopod answered 15/3, 2010 at 16:2 Comment(2)
Big-endian may not very unusual if he is working with embedded hardware.Ideate
Works great thanks! In this case i dont think Endian matters, I am careful with it but in this case it ok.Genevieve
S
8

Use this method.

    public static byte[] ToBcd(int value){
        if(value<0 || value>99999999)
            throw new ArgumentOutOfRangeException("value");
        byte[] ret=new byte[4];
        for(int i=0;i<4;i++){
            ret[i]=(byte)(value%10);
            value/=10;
            ret[i]|=(byte)((value%10)<<4);
            value/=10;
        }
        return ret;
    }

This is essentially how it works.

  • If the value is less than 0 or greater than 99999999, the value won't fit in four bytes. More formally, if the value is less than 0 or is 10^(n*2) or greater, where n is the number of bytes, the value won't fit in n bytes.
  • For each byte:
    • Set that byte to the remainder of the value-divided-by-10 to the byte. (This will place the last digit in the low nibble [half-byte] of the current byte.)
    • Divide the value by 10.
    • Add 16 times the remainder of the value-divided-by-10 to the byte. (This will place the now-last digit in the high nibble of the current byte.)
    • Divide the value by 10.

(One optimization is to set every byte to 0 beforehand -- which is implicitly done by .NET when it allocates a new array -- and to stop iterating when the value reaches 0. This latter optimization is not done in the code above, for simplicity. Also, if available, some compilers or assemblers offer a divide/remainder routine that allows retrieving the quotient and remainder in one division step, an optimization which is not usually necessary though.)

Supposed answered 18/8, 2011 at 12:33 Comment(3)
Are you able to tell me what's happening here step by step? Thanks.Ruthenic
@Anonymous: I've added an explanation.Supposed
Awesome function - I've used it for a long time - I changed mine to cover all int values by increasing the array to 5 bytes. Also made ones for other datatypes and made them into extension methods. Thanks for getting me going.Mariko
P
3

Here's a terrible brute-force version. I'm sure there's a better way than this, but it ought to work anyway.

int digitOne = year / 1000;
int digitTwo = (year - digitOne * 1000) / 100;
int digitThree = (year - digitOne * 1000 - digitTwo * 100) / 10;
int digitFour = year - digitOne * 1000 - digitTwo * 100 - digitThree * 10;

byte[] bcdYear = new byte[] { digitOne << 4 | digitTwo, digitThree << 4 | digitFour };

The sad part about it is that fast binary to BCD conversions are built into the x86 microprocessor architecture, if you could get at them!

Pokey answered 15/3, 2010 at 15:52 Comment(0)
I
3

Here is a slightly cleaner version then Jeffrey's

static byte[] IntToBCD(int input)
{
    if (input > 9999 || input < 0)
        throw new ArgumentOutOfRangeException("input");

    int thousands = input / 1000;
    int hundreds = (input -= thousands * 1000) / 100;
    int tens = (input -= hundreds * 100) / 10;
    int ones = (input -= tens * 10);

    byte[] bcd = new byte[] {
        (byte)(thousands << 4 | hundreds),
        (byte)(tens << 4 | ones)
    };

    return bcd;
}
Ideate answered 15/3, 2010 at 15:58 Comment(1)
You can probably come up with some crazy way that uses Boolean logic and shifting... but this will be much nicer to maintain.Ideate
H
2

maybe a simple parse function containing this loop

i=0;
while (id>0)
{
    twodigits=id%100; //need 2 digits per byte
    arr[i]=twodigits%10 + twodigits/10*16;  //first digit on first 4 bits second digit shifted with 4 bits
    id/=100;

    i++;
}
Horvitz answered 18/8, 2011 at 12:16 Comment(0)
G
1

More common solution

    private IEnumerable<Byte> GetBytes(Decimal value)
    {
        Byte currentByte = 0;
        Boolean odd = true;
        while (value > 0)
        {
            if (odd)
                currentByte = 0;

            Decimal rest = value % 10;
            value = (value-rest)/10;

            currentByte |= (Byte)(odd ? (Byte)rest : (Byte)((Byte)rest << 4));

            if(!odd)
                yield return currentByte;

            odd = !odd;
        }
        if(!odd)
            yield return currentByte;
    }
Gurnard answered 22/2, 2012 at 13:30 Comment(1)
You shouldn't use a decimal as input parameter if all you can compute is an integer. If you need a decimal for your internal computation it should stay internal or how do you compute GetBytes(3.55549322);?Clothbound
A
1

Same version as Peter O. but in VB.NET

Public Shared Function ToBcd(ByVal pValue As Integer) As Byte()
    If pValue < 0 OrElse pValue > 99999999 Then Throw New ArgumentOutOfRangeException("value")

    Dim ret As Byte() = New Byte(3) {} 'All bytes are init with 0's

    For i As Integer = 0 To 3
      ret(i) = CByte(pValue Mod 10)
      pValue = Math.Floor(pValue / 10.0)
      ret(i) = ret(i) Or CByte((pValue Mod 10) << 4)
      pValue = Math.Floor(pValue / 10.0)
      If pValue = 0 Then Exit For
    Next

    Return ret
End Function

The trick here is to be aware that simply using pValue /= 10 will round the value so if for instance the argument is "16", the first part of the byte will be correct, but the result of the division will be 2 (as 1.6 will be rounded up). Therefore I use the Math.Floor method.

Asgard answered 2/9, 2016 at 7:9 Comment(0)
S
0

I made a generic routine posted at IntToByteArray that you could use like:

var yearInBytes = ConvertBigIntToBcd(2010, 2);

Selfexpression answered 4/1, 2013 at 16:1 Comment(0)
S
-3
static byte[] IntToBCD(int input) { 
    byte[] bcd = new byte[] { 
        (byte)(input>> 8), 
        (byte)(input& 0x00FF) 
    };
    return bcd;
}
Swacked answered 11/8, 2011 at 1:58 Comment(1)
This is not BCD. you are just converting the int into to a two byte array,Hydrozoan

© 2022 - 2024 — McMap. All rights reserved.