0

I just need to create a byte array out of the given String.

For example, if my String is String ss = "21331UA"; then the byte array elements should correspond to them as follows.

2 1 3 3 1 U A

I could have created it statically as this. byte[] arr = new byte[]{2,1,3,3,1,'U','A'} But have to create this byte array in the runtime dyanamically as this changes time to time. Thats the problem.

I just tried as follows and print them, it contains their corresponding ASCII values.This is not what I want.

byte[] arr = ss.getBytes();

for(int i=0; i<arr.length; i++)
{
    System.out.print(arr[i] + "  ");
}
Ans==> 50  49  51  51  49  85  65

Really appreciate any guidance.... Thanks in advance

5
  • 5
    byte is a numeric type in java, do you mean char '2', '1', '3' ...? Commented Jun 20, 2012 at 15:53
  • Hi Mana, I just need to dyanamically create this byte array to proceed to next step. Thats the problem. Otherwise I could have created it with this syntax... byte[] arr = new byte[]{2,1,3,3,1,'U','A'}. Thats why I am looking to create this array dyanamically in the run time and to use it in my application. Thanks... Commented Jun 20, 2012 at 16:10
  • @JibW If you were to print out the byte[] the way you create it, you'll get the exact same ASCII (which are the byte values) of that array. Calling getBytes() is the correct dynamic way of getting those values. Commented Jun 20, 2012 at 16:14
  • Hi Poindexter, My requirement is to create the byte array and to use it in the application. Since when I created with the getBytes() way it didn't work properly, I printed them and check what's going wrong there. My requirement is to create the byte array... Thanks Commented Jun 20, 2012 at 16:23
  • Ya, but like I said earlier, byte is numeric. It can only hold numbers from -128 to 127. It can not hold an 'U' but the byte representation for 'U' wich is 85. The ss.getBytes(); call is absolutely correct and does the right thing: Get the Byte representation of your String! Commented Jun 20, 2012 at 16:27

2 Answers 2

6

You could cast the values in your byte array like this:

System.out.print((char)arr[i] + "  ");

Or, alternatively, set your byte array to chars right off the bat:

char[] arr = ss.toCharArray();

for(int i=0; i<arr.length; i++)
{
    System.out.print(arr[i] + "  ");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Daniel, Yes When I print like this it prints what I want. I just need to create the byte array which exactly containing the characters of the String. Not ASCII Values...Because I just need a byte array that contains the exact characters I suggest by the string. Thanks
@JibW your code ss.getBytes() does exactly that, it is the byte[] representation of your String.
Hi cklab, Ya Thats the problem. I just need a way to create a byte array of the characters of the String...
Hi Daniel, I just need to dyanamically create this byte array to proceed to next step. Thats the problem. Otherwise I could have created it with this syntax... byte[] arr = new byte[]{2,1,3,3,1,'U','A'}. Thats why I am looking to create this array dyanamically in the run time and to use it in my application. A char array is not going to work for my scenario. Thanks.
@JibW, a byte array and a char array are essentially the same thing in that they are storing the exact same data. What you hope to pull back from these arrays is a matter of interpretation. If you want to store the string's individual characters in a byte array, you must cast the values you index as chars, otherwise you will see numeric data when you print to screen.
0

You need to CAST byte to char as type of array arr[i] is byte.

Simply say

System.out.print((char) arr[i] + "  ");

1 Comment

Hi Fahim, When printing of course I can cast it like this. But I am looking for a way to create a byte array with the exact characters that the string contains.

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.