0

I have a GUI where i try to convert a EBCDIC String into Hexadecimal, but it will not work :/

ebcdichex.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            output6.setText("");
            String hexadecimal3 = input4.getText().replace("\n", "");
            byte[] chars2;

            try {
                chars2 = hexadecimal3.getBytes("IBM-273");
                StringBuilder hexa2 = new StringBuilder();
                for(int i = 0;i<chars2.length;i++){
                    byte[] b = {(byte) Integer.parseInt(chars2.toString(),16)};     
                    hexa2.append(b);
                }
                output6.append(hexa2.toString());
            }
         catch (UnsupportedEncodingException e1) {

            e1.printStackTrace();
        }
        }
    });

Example input:

f1f2f3f4

translated:

1234

It just throws this Error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "[B@147c5fc"

What is wrong with this code?

Starlight
  • 5
  • 1
  • 6

2 Answers2

1

The following line looks suspicious:

byte[] b = {(byte) Integer.parseInt(chars2.toString(),16)}; 

Assuming that you have your EBCDIC in byte[] array, you can use method to present it as HEX -> for instance from this answer: How to convert a byte array to a hex string in Java?

EDIT:

Are you sure you are using the correct encoding name for EBCDIC? https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html states Cp273

EDIT#2: Ok, now I got what you want. Here's a sample code:

import java.util.*;
import java.lang.*;
import java.io.*;

class Conversion
{
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        String hexadecimal3 = "f1f2f3f4";
        byte[] ebcdic = hexStringToByteArray(hexadecimal3);
        System.out.println(  new String(ebcdic, "Cp273") ); // prints 1234
    }
}
Lukasz
  • 7,572
  • 4
  • 41
  • 50
0

From a hex-digits string you want to get the readable characters, interpreting the hex string as EBCDIC-encoded.

In your code, with chars2 = hexadecimal3.getBytes("IBM-273"); you create a byte sequence that encodes the hex digits from hexadecimal3 one by one to EBCDIC. Later you try to convert that EBCDIC byte sequence back to a String chars2.toString(), not telling Java that it's EBCDIC, and try to parse that as hexadecimal.

You should do:

  • Parse all the 2-character substrings from the input individually as hex numbers and store the results as byte array bytes.

  • Create the result as new String(bytes, "IBM-273")

And a style recommendation:

extract the logic into a method like String stringFromEbcdicHex(String ebcdicHexDigits).

EDIT:

To parse the 2-character substrings, you can do something like:

byte[] bytes = new byte[ebcdicHexDigits.length/2];
for (int i=0; i<bytes.length; i++) {
    bytes[i] = (byte) Integer.parseInt(ebcdicHexDigits.substring(2*i, 2*i+2));
}
Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7