-1

//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.

Shaq
  • 377
  • 5
  • 16
  • Can you explain what you are trying to do and supply your code. If you have an 'EBCDIC' string and want to convert it to hex, why do you need to convert it to ascii ?? – Bruce Martin Dec 28 '15 at 21:30
  • I have EBCDIC hex which i want to convert to binary. My description above can be misleading, I'm sorry. In UltraEdit I open EBCDIC text and change view to HEX Mode. This HEX I need to get in JAVA too. – Shaq Dec 29 '15 at 08:08
  • @BruceMartin thanks for help – Shaq Dec 30 '15 at 10:55

2 Answers2

0

There is no ASCII-value of 136 since ASII is only 7 bit - everything beyond 127 is some custom extended codepage (the linked table seems to use some sort of windows-codepage, e.g. Cp1252). Since it is printing a ? it seems you are using a codepage that doesn't have a character assigned to the value 136 - e.g. some flavour of ISO-8859.

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • So what can I do with that? – Shaq Dec 28 '15 at 10:40
  • By the way, as you see I wrote "Decimal ASCII 136 sign according to". Where "according to" is a key word. – Shaq Dec 28 '15 at 11:27
  • @Shaq What you can do depends on what you are doing now - which I don't know. – piet.t Dec 28 '15 at 13:05
  • First, I read a string from file. Then read bytes from it an convert to integer(since I have negative values). Next, I take every two bytes and convert them to hex. Finally, I convert hex to binary. – Shaq Dec 29 '15 at 08:12
0

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.

Shaq
  • 377
  • 5
  • 16