1

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.

4
  • Do you want "the quick brown fox jumps over the lazy dog42" to be the output? Commented Nov 16, 2015 at 17:43
  • 4
    You're not going to append the int for 42, you need to append the bytes corresponding to the appropriate encoding of the characters 4 and 2. Commented Nov 16, 2015 at 17:44
  • @resueman yes, I would like that exactly. Commented Nov 16, 2015 at 17:45
  • Each byte represents a char in the String, why should it read your int differently? You'll need to customize a parsing method to convert your mangled byte array back into the String you wish for it to be. Commented Nov 16, 2015 at 17:51

3 Answers 3

1

How about:

String myString = "the quick brown fox jumps over the lazy dog";
int myInt = 42;
String temp = myString + String.valueOf(myInt);
byte[] myByteArray = temp.getBytes();
Sign up to request clarification or add additional context in comments.

11 Comments

or even shorter: byte[] myByteArray = (myString + myInt).getBytes();
Yes, that would work, but I would like to store the integer, itself, in the byte array.
Ints are not strings in bytes... "42" vs int 42 are two different bytes, you didn't solve anything
@Brian the problem you face is that each digit of the int is a separate byte. First you would need to check the length of the int, then create your byte[] of appropriate size and THEN get the digits of the int one by one and properly convert them into their ASCII values and append them.
@Brian if you want to store the number in the byte array, when doing the output, the program have no way to distinguish which byte is for number, which byte is for ASCII character.
|
0

The simplest way would be to just append the int to the end of the string:

myString = myString + myInt;

Otherwise, you'll need to append the bytes for the characters '4' and '2' to an expanded byte array, as Sotirios Delimanolis suggested.

I would like to add that using myString.getBytes() is a very bad idea. The getBytes() method uses the default platform encoding which can differ from system to system, JVM to JVM, or even run to run. You should always prefer the version that takes a Charset as a parameter so that you know what you're getting, e.g.:

myString.getBytes(StandardCharsets.UTF_8);

Or as I like to say, Always assume the default platform encoding is EBCDIC and act accordingly.

Comments

0

Updated answer, so as to bypass charset encoding, and assuming it is required to get the characters:

            String numString = String.valueOf (newInt);
            String newString  = myString + newInt;
            int newLength = myString.length() + numString.length();
            char[] newCharArray = new char[newLength];

            newString.getChars(0, newLength, newCharArray, 0);

With David's comment:

     String newString  = myString + newInt;
     char[] newCharArray = newString.toCharArray();

7 Comments

You basically copied @Andics 's answer. Is is not necessary to have the same answer more than once.
I am not copying, I just post the answer and after that see another answer ahead of me. ; (
Only use getBytes() if you're equally happy with KOI8-R, Shift-JIS, EBCDIC, or CP-868 (Urdu).
@DavidConrad yes, I found out that. If he only wants to get each character, possiblly using getChars is better. getChars simply copies the characters out.
There is no getChars()? Do you mean chars(), or toCharArray()?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.