Migrating BCD conversion code from Java to .Net

1

I am converting a Java class that converts BCD data to ASCII

I am converting it to .Net BCD Convertor

Following is the converted from java but it is giving wrong converted value e.g. for 123456789 it is giving 123456153153

 public static string GetStringFromBcd(int[] b)
 {
   StringBuilder buffer = new StringBuilder();
   foreach (var t in b)
   {
     if ((t & 0x0000000F) == 0x0000000F && ((t >> 4) & 0x0000000F) == 0x0000000F)
     {
         break;
     }
     buffer.Append((t & 0x0000000F) + "");
     if ((t & 0x000000F0) != 0x000000F0)
     {
         buffer.Append(((t >> 4) & 0x0000000F) + "");
     }
   }
}

What could be the problem?

EDIT: ANSWER:

I got the source program where the data has been BCD encoded. I found that nothing was wrong in that logic, then I discovered the source of the function where the data was converting from network stream to string and later converted to byte/int array. following is the code

 int bytesRead = tcpClient.Receive(message);//, 0, bytetoReadSize);
 if (bytesRead == 0)
 {
     break;
     //the client has disconnected from the server
 }

 //message has successfully been received
 data += new ASCIIEncoding().GetString(message, 0, bytesRead);

here is the problem ASCIIEncoding does not convert many encoded character and gives '?'63 instead of those character , when putting 63 in BCD conversion logic it gives 153.

To resolve this error, I Modified the last line and instead of decoding , I am simply casting the received byte to char.

 foreach (byte b in message)
 {
     data += ((char) b);
 }
c#
java
.net
asked on Stack Overflow Jul 26, 2012 by Imran Rizvi • edited Jul 31, 2012 by Imran Rizvi

2 Answers

0

Here's a Similar Question that comes about it a few different ways.

Here's a Site that has an excellent detailed description of what your facing.

It shouldn't be that hard, but processing them as int's is going to be much harder.

Zoned Decimal (BCD) is pretty straight forward to convert, but you have to be careful if your taking files from a mainframe that have been converted via an ASCII transfer. It can still be converted, but the byte values change due to the ebcdic to ascii conversion during the FTP.

If your processing binary files, it's much easier to deal with.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestZoned
{
  class Program
  {
    public static String zoneToString(byte[] zoneBytes)
    {
        Encoding ascii = Encoding.ASCII;
        Encoding ebcdic = Encoding.GetEncoding("IBM037");
        byte[] asciiBytes = null;
        String str = null;

        int zoneLen = zoneBytes.Length;
        int i = zoneLen - 1;
        int b1 = zoneBytes[i];
        b1 = (b1 & 0xf0) >> 4;

        switch (b1)
        {
            case 13:
            case 11:
                zoneBytes[i] = (byte)(zoneBytes[i] | 0xf0);
                asciiBytes = Encoding.Convert(ebcdic, ascii, zoneBytes);

                str = "-" + ASCIIEncoding.ASCII.GetString(asciiBytes);
                break;

            default:
                zoneBytes[i] = (byte)(zoneBytes[i] | 0xf0);
                asciiBytes = Encoding.Convert(ebcdic, ascii, zoneBytes);

                str = ASCIIEncoding.ASCII.GetString(asciiBytes);
                break;
        }

        return (str);
    }

    static void Main(string[] args)
    {
        byte[] array =  { 0xf0, 0xf0, 0xf1 }; // 001
        byte[] pos = { 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 }; // 123456789
        byte[] neg = { 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xd9 }; // -123456789

        Console.WriteLine("Converted: {0}", zoneToString(array));
        Console.WriteLine("Converted: {0}", zoneToString(pos));
        Console.WriteLine("Converted: {0}", zoneToString(neg));
    }
}

}

answered on Stack Overflow Jul 27, 2012 by Mike • edited May 23, 2017 by Community
0

If you wanted to stick to something similar

    public static String GetStringFromBcd(byte[] zoneBytes)
    {
        StringBuilder buffer = new StringBuilder();

        int b1 = (zoneBytes[zoneBytes.Length - 1] & 0xf0) >> 4;
        if ( (b1 == 13) || (b1 == 11) ) buffer.Append("-");

        for (int i = 0; i < zoneBytes.Length; i++)
        {
            buffer.Append((zoneBytes[i] & 0x0f));
        }

        return buffer.ToString();
    }
answered on Stack Overflow Jul 27, 2012 by Mike • edited Jul 27, 2012 by Mike

User contributions licensed under CC BY-SA 3.0