0

I want to convert a string value to the same byte array in C# and Java with following codes:

C#:

string key="EA1302AFBCCF791CB0065BFAD948B092";
byte[] keyByte = Encoding.UTF8.GetBytes(plainKey);

Java:

String key="EA1302AFBCCF791CB0065BFAD948B092";
byte[] keyByte = (key).getBytes("UTF-8");

But the length of the generated array is 32 in C# and 343 in Java. I have to create a byte array in C# same as Java, so please don't suggest changes for my Java code.

8
  • 2
    How did you check the lengths? Commented Oct 14, 2019 at 11:12
  • @ThomasTimbul I check it by test unit (break points) on netbeans 8.2 and Visual Studio 2019 Commented Oct 14, 2019 at 11:14
  • 1
    (key) doesn't need to be in parenthesis :) Commented Oct 14, 2019 at 11:16
  • I highly doubt your reading is correct. Code seems ok. Regardless of the parentheses, it should work as expected. Couldn't reproduce at least. Commented Oct 14, 2019 at 11:19
  • 1
    @MahyarEsteki then I suggest you print keyByte.length to System.out instead, and you'll probably find that they match. Commented Oct 14, 2019 at 11:19

1 Answer 1

1

I tried:

 public static void main(String args[]) throws UnsupportedEncodingException {
        String key="EA1302AFBCCF791CB0065BFAD948B092";
        byte[] keyByte = key.getBytes("UTF-8");
        System.out.println("Length: " + keyByte.length);

    }

output is:

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

Comments

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.