-3
public byte[] stringToEbcdic(String s, String encoding){

    String payload = null;
    try {
        payload = new String(s.getBytes("encoding"), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return payload.getBytes();
}

I should be able to call the method like

byte[] b = stringToEbcdic("abcd", "IBM01140");

but its not working.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
user1824107
  • 15
  • 1
  • 5

1 Answers1

0
payload = new String(s.getBytes("encoding"), "ISO-8859-1");

should be

// encoding is a variable, not a string literal
payload = new String(s.getBytes(encoding), "ISO-8859-1");

However, you're doing some strange further manipulation of the string. The whole method could be simplified to the working version;

public static byte[] stringToEbcdic(String s, String encoding) {
    try {
        return s.getBytes(encoding);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294