5

I have a byte array I want to assign as follows:

  • First byte specifies the length of the string: (byte)string.length()
  • 2nd - Last bytes contain string data from string.getBytes()

Other than using a for loop, is there a quick way to initialize a byte array using bytes from two different variables?

9
  • 2
    Can you guarantee that your strings will always be < 256 characters long (128 if you don't deal with sign bit)? Commented Jan 6, 2011 at 19:56
  • Yes I can, and can you elaborate on dealing with the sign bit? Commented Jan 6, 2011 at 19:57
  • @Anon: byte is an unsigned type in Java. Commented Jan 6, 2011 at 19:58
  • 2
    Bytes in Java are signed values. When converted to an int, they'll be sign-extended. So you have to mask the sign-extended value to get the range 0..255: b & 0xFF Commented Jan 6, 2011 at 19:59
  • 2
    @R Bernrose - really? so the JLS is wrong? java.sun.com/docs/books/jls/third_edition/html/… Commented Jan 6, 2011 at 20:00

4 Answers 4

6

You can use System.arrayCopy() to copy your bytes:

String x = "xx";
byte[] out = new byte[x.getBytes().length()+1];
out[0] = (byte) (0xFF & x.getBytes().length());
System.arraycopy(x.getBytes(), 0, out, 1, x.length());

Though using something like a ByteArrayOutputStream or a ByteBuffer like other people suggested is probably a cleaner approach and will be better for your in the long run :-)

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

2 Comments

This is probably simpler than using a library class for an operation this straightforward. One comment is that the (0xFF & ...) operation is completely redundant when going TO a byte. The (byte) cast truncates in the same way. It's only when going back to an integer that you care.
Ok, granted. I was thinking, better safe then sorry, right? ;-)
2

How about ByteBuffer ?

Example :

    ByteBuffer bb = ByteBuffer.allocate(string.getBytes().length +1 );
    bb.put((byte) string.length());
    bb.put(string.getBytes());

3 Comments

This is the right approach, but requires some comments. First, it needs to check the string's length before blindly casting to byte. Second, it should use the variant of String.getBytes() that takes a character set.
@Anon agree with your comments +1 :) @fredley if you are following please take care of this
It would be nice to include the code to get to what the OP asked for: a byte array.
2

While ByteBuffer is generally the best way to build up byte arrays, given the OP's goals I think the following will be more robust:

public static void main(String[] argv)
throws Exception
{
   String s = "any string up to 64k long";

   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   DataOutputStream out = new DataOutputStream(bos);
   out.writeUTF(s);
   out.close();

   byte[] bytes = bos.toByteArray();

   ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
   DataInputStream in = new DataInputStream(bis);

   String s2 = in.readUTF();
}

Comments

0

How about ByteArrayOutputStream?

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.