0

hi guys i should convert this code to C# in Java. Could you give me a hand?

private static String ConvertStringToHexStringByteArray(String input) {
        Encoding ebcdic = Encoding.GetEncoding("IBM037");
        Encoding utf8 = Encoding.UTF8;
        byte[] utfBytes = utf8.GetBytes(input);
        byte[] isoBytes = Encoding.Convert(utf8, ebcdic, utfBytes);
        StringBuilder hex = new StringBuilder(isoBytes.length * 2);
        foreach( byte b in isoBytes)
        hex.AppendFormat("{0:x2}", b);
        return hex.ToString();

    }

I tried to convert it to java like this. But the result is different:

private static String ConvertStringToHexStringByteArray(String input) throws UnsupportedEncodingException {

        byte[] isoBytes = input.getBytes("IBM037");
        StringBuilder hex = new StringBuilder(isoBytes.length * 2);

        for (byte b : isoBytes) {
            hex.append(String.format("%02x", b));
        }
        return hex.toString();

    }

input = "X1GRUPPO 00000000726272772"
expected = "e7f1c7d9e4d7d7d64040404040f0f0f0f0f0f0f0f0f1f6f7f3f5f3f5f5f2"
result = "e7f1c7d9e4d7d7d640f0f0f0f0f0f0f0f0f7f2f6f2f7f2f7f7f2"

what am I doing wrong?

4
  • 1
    Step one open a java IDE, step 2 write java code, step 3 when you have a problem paste your java code, and your actual issue Commented Nov 4, 2021 at 9:56
  • Do not "convert" code. You write down what it does and then you do not look at the original code any more and you implement these requirements in the other language. Commented Nov 4, 2021 at 9:58
  • I entered my Java conversion Commented Nov 4, 2021 at 10:34
  • What is the point of using EBCDIC? Why not just input.getBytes("UTF-8") or input.getBytes(StandardCharsets.UTF_8)? Commented Nov 4, 2021 at 14:23

1 Answer 1

1

Your code works but you are comparing the output for two different input strings.

When you write expected and result side by side:

e7f1c7d9e4d7d7d64040404040f0f0f0f0f0f0f0f0f1f6f7f3f5f3f5f5f2
e7f1c7d9e4d7d7d640f0f0f0f0f0f0f0f0f7f2f6f2f7f2f7f7f2

you will notice that both start with the same sequence (e7f1c7d9e4d7d7d6) which seems to come from a common beginning X1GRUPPO

But then the two outputs differ:

4040404040f0f0f0f0f0f0f0f0f1f6f7f3f5f3f5f5f2
40f0f0f0f0f0f0f0f0f7f2f6f2f7f2f7f7f2

Reasoning from the input that you provided, the remainder of first input string starts with 5 spaces followed by "00000000167353552"

This means the complete input for the C# code was "X1GRUPPO 00000000167353552", which is not the same input that you provided to the Java code and then clearly the output cannot match.

Sign up to request clarification or add additional context in comments.

1 Comment

you're right, I got the input wrong. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.