Question

I am really struggling to complete this piece, any thoughts or pointers will help me a lot. Here is my problem:

  1. I am working on ISO 8583 layer and data element 32 is defined as n ..11, which means the element is numeric, variable length with max size 11.

  2. and this need to be sent in binary format (not ASCII).

  3. We need to show LLNN, ex ISO binary raw should look like this : 11 06 01 04 00 00 00

Till now I was able to complete fill the byte[] array with (11/2)+(11%2) size of 6 with BCD(06 01 04 00 00 00). and I got the total length of the field calculated as 11, Now I need to insert this at byte[0] with the 11 not 0B i.e 0001 0001 packed to one byte byte[]as 11 , my logic as shown below when I give nBytes=1(first 255 chars in BCD ISO) it will insert the length field value as either 0b 01 04 00 00 00 or if i pass nBytes as 2, its giving 00 0b 04 00 00 00. Something going wrong with the right shift logic...

Can somebody help me and correct this below logic to bring the results as 11 06 01 04 00 00 00

int nBytes=1;
for (int i = nBytes - 1; i >= 0; i--) {
    buf[i] = (byte)(length & 0xFF);
    length >>= 8;
}
outstream.write(buf);
Was it helpful?

Solution 2

I have finally ended up using the two byte array objects. One holding the original request and calculating the length of the field in decimal and putting it in the first location i.e. 0 index of the secondary byte array and appending the original byte array to this.

OTHER TIPS

I think this function performs the BCD conversion you are looking for:

class T {
    public static byte toBCD(int n)
    {
        // a*10 + b -> a*16 + b;
        byte a = (byte)(n / 10);
        byte b = (byte)(n % 10);

        return (byte) (a * 0x10 + b);
    }

    public static void main(String[] args)
    {
        assert(toBCD(11) == 0x11);
        assert(toBCD(28) == 0x28);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top