0

suppose I have two Strings like

txt1="something 1";
txt2="something 2";

now wanted to create byte[] str= new byte[]; from those Strings

byte[] t1,t2;
t1=txt1.getBytes();
t2=txt2.getBytes();
byte[] strings = { t1,t2};
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Laxman More
  • 307
  • 1
  • 5
  • 14

5 Answers5

5

Firstly, I would suggest not using getBytes() without specifying the encoding first. (I generally prefer UTF-8, but it depends on what you want.)

Secondly, if you just want the bytes of one string followed by the bytes of another, the simplest approach is to use string concatenation:

byte[] data = (txt1 + txt2).getBytes(StandardCharsets.UTF_8);

Alternatively you can fetch each byte array separately, and then combine them by creating a new byte array which is large enough to hold both, and copying the data:

byte[] t1 = txt1.getBytes(StandardCharsets.UTF_8);
byte[] t2 = txt2.getBytes(StandardCharsets.UTF_8);
byte[] both = new byte[t1.length + t2.length];
System.arraycopy(t1, 0, both, 0, t1.length);
System.arraycopy(t2, 0, both, t1.length, t2.length);

I'd use the string concatenation version myself though :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • My intuition is that the string concatenation version is slightly more expensive, since it allocates an additional copy of the string. Of course, it's highly unlikely that this will be a bottleneck. – Brian Sep 16 '13 at 14:00
  • @Brian: You'd have an additional copy of the string either way - you either get three strings and one byte array, or two strings and three byte arrays (one for each original string, and one for the combination). It may still be more expensive to perform the concatenation, but as you say it's probably not significant. I'd at least want to measure it before using more complicated code. – Jon Skeet Sep 16 '13 at 14:04
  • @JonSkeet: Thanks, I missed that. The next concern with deciding between the two variants is if there is a scenario where the two versions of the code don't yield identical results. Tony the pony says they're always identical. – Brian Sep 16 '13 at 14:18
  • @Brian: I can imagine *broken* scenarios where they're not identical - e.g. if `txt1` ended with the first code unit in a surrogate pair, and `txt2` started with the second code unit in a surrogate pair... asking for the UTF-8 bytes of each individual string makes no sense, but the combined string doesn't. For *sensible* strings, I don't think it would be a problem. – Jon Skeet Sep 16 '13 at 14:19
5

The easiest way would be:

byte[] strings = (t1+t2).getBytes();

Otherwise you'd have to manually allocate a large enough array and copy the individual bytes. I'm pretty sure there's no array-concatenation utility function in the standard API, though there might be one in Apache Commons or something...

Oh, and yes, usually yo want to specify the encoding when you use getBytes(), rather than relying on the platform default encoding.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

Because txt1.getBytes() returns a byte[]. You are trying to assign multiple byte[]'s in an array. Merge both the array and the assign into it.

byte[] combined = new byte[array1.length + array2.length];
newuser
  • 8,338
  • 2
  • 25
  • 33
1

You have to allocate the new byte array and copy the content of the two byte arrays into the new byte array:

byte[] t1 = txt1.getBytes();
byte[] t2 = txt2.getBytes();

byte[] result = new byte[t1.length + t2.length];

System.arraycopy(t1, 0, result, 0, t1.length);
System.arraycopy(t2, 0, result, t1.length, t2.length);
lpiepiora
  • 13,659
  • 1
  • 35
  • 47
1

Simplest approach is to use string concatenation and convert into bytes array.

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String txt1="something 1",txt2="something 2";
        //Use '+' string concatenation and convert into byte array.
        byte[] data = (txt1 + txt2).getBytes();
        System.out.println(data);
    }
}
Siddh
  • 712
  • 4
  • 21