0

I am working on converting and existing C# project over to Java/Android. I am looking for the Java equivalent to UTF8Encoding.GetBytes(String, Int32, Int32, Byte[], Int32). Take a look at the C# code below, how do I add the string packet into the data byte array? I have looked at the String.getBytes() method but it is not the same.

int length = packet.Length;

byte[] data = new byte[6 + length + 1];
data[0] = (byte)VAR1;
data[1] = 1;

**Encoding.UTF8.GetBytes(packet, 0, length, data, 6);**

data[6 + length] = (byte)VAR2;

data[5] = (byte)(length % 256);
length /= 256;
data[4] = (byte)(length % 256);
length /= 256;
data[3] = (byte)(length % 256);
length /= 256;
data[2] = (byte)(length % 256);
3
  • You seem to be assuming that you can get all of the UTF-8-encoded data into the same number of bytes as there are characters. That's simply not true, unless all the characters are ASCII. Commented May 1, 2014 at 12:56
  • @JonSkeet, they are all ASCII. All characters that can be found in the string "packet" are "predefined". Commented May 1, 2014 at 13:03
  • In that case, why specify UTF-8? Why not use Encoding.ASCII, which is a lot clearer in intent? Commented May 1, 2014 at 13:04

1 Answer 1

1

Okay, given that you mean ASCII rather than UTF-8, there are two immediate options:

Intermediate byte array

byte[] encodedText = text.getBytes(StandardCharsets.US_ASCII);
System.arraycopy(encodedText, 0, data, 6, encodedText.length);

This is inefficient, but simple.

Charset directly

CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(text);
ByteBuffer byteBuffer = ByteBuffer.wrap(data, 6, data.length - 6);
encoder.encode(charBuffer, byteBuffer, true);

This is possibly more efficient, but more complicated to understand.

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

6 Comments

Thanks Jon Skeet, that worked a charm. The System.arraycopy was what I needed. Honestly, for my proposes the encoding specification is irrelevant.
@user1017477: It's really not - because if you specify something like EBCDIC or UTF-16, you won't get the bytes you expect. Encodings are never irrelevant.
I understand what you're saying however, in this case all of the text that is to be converted is essentially "hard coded". I have run your suggestion through a handful of possible scenarios without specifying the encoding type of getBytes() and everything is functioning as it should.
@user1017477: That just means you haven't run it on a system where the default encoding is inhospitable to you. It doesn't mean such code isn't broken.
you are correct again. I guess by working on apps that only target a small subset of users within a controlled environment, one can overlook the ramifications of running the same app on a larger scale?
|

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.