Say I have a string
String myString = "the quick brown fox jumps over the lazy dog";
and I convert it to a byte array
byte[] myByteArray = myString.getBytes();
then, say I have an int
int myInt = 42;
and I convert it to a byte
byte myByte = (byte) myInt;
now, say I would like to add the int to the byte array so that when I print the array, it prints out
the quick brown fox jumps over the lazy dog42
I tried this, but it didn't work
byte[] newByteArray = new byte[myByteArray.length + 1];
System.arraycopy(myByteArray, 0, newByteArray, 0, myByteArray.length);
newByteArray[newByteArray.length-1] = myByte;
String finalString = new String(newByteArray);
System.out.println(finalString);
All that gets printed out is
the quick brown fox jumps over the lazy dog*
'*' being the ASCII character 42.
So, what is the simplest way to do this?
EDIT:
Yes, I know I can append the String "42" to my other string, but I want the integer value stored in the byte array. I was under the assumption that a byte was a byte, and it did not matter if it was a String or an int, because bytes were 1s and 0s under the hood.
intfor 42, you need to append the bytes corresponding to the appropriate encoding of the characters 4 and 2.