Search code examples
javabinaryhexasciiebcdic

How to convert EBCDIC hex to binary


//PROBLEM SOLVED

I wrote a program to convert EBCDIC string to hex. I have a problem with some signs.

So, I read string to bytes, then change them to hex(every two signs)

Problem is that, JAVA converts Decimal ASCII 136 sign according to https://shop.alterlinks.com/ascii-table/ascii-ebcdic-us.php to Decimal ASCII 63.

Which is problematic and wrong, becouse it's the only wrong converted character.

EBCDIC 88 in UltraEdit

//edit -added code

int[] bytes = toByte(0, 8);
String bitmap = hexToBin(toHex(bytes));

//ebytes[] - ebcdic string
public int[] toByte(int from, int to){
    int[] newBytes = new int[to - from + 1];
    int k = 0;
    for(int i = from; i <= to; i++){
        newBytes[k] = ebytes[i] & 0xff;
        k++;
    }
    return newBytes;
}

public String toHex(int[] hex){
    StringBuilder sb = new StringBuilder();
    for (int b : hex) {
        if(Integer.toHexString(b).length() == 1){
            sb.append("0" + Integer.toHexString(b));
        }else{
            sb.append(Integer.toHexString(b));
        }
    }
    return sb.toString();
}

public String hexToBin(String hex){
    String toReturn = new BigInteger(hex, 16).toString(2);
    return String.format("%" + (hex.length() * 4) + "s", toReturn).replace(' ', '0');
}

//edit2

Changing encoding in Eclipse to ISO-8859-1 helped, but I lose some signs while reading text from a file.

//edit3

Problem solved by changing the way of reading file.

Now, I read it byte by byte and parse it to char. Before, it was line by line.


Solution

  • Solution.

    • Change encoding in Eclipse to more proper (in my example ISO-8859-1).
    • Change the way of reading file.

    Now, I read it byte by byte and parse it to char.

    Before, it was line by line and this is how I lost some chars.