0

I have a String like this in my Java code.

String my = "16 12 12 -119 102 105 -110 52 -89 80 -122 -68 114 20 -92 -28 -121 38 113 61"

the values separated by " " is integer ( you can see it ).

I can convert the integers to a int array, but I need to convert the int values to a byte array. The value represented by each integer is a byte value.

PS.

String aa[] = U.split(" ");
byte bb[] = new byte[aa.length];
for(int q=0;q<aa.length;q++){
 int v = Integer.parseInt(aa[q]);
 bb[q] = ???????????????????--the code I need to convert the int to a byte
}
asked Aug 28, 2014 at 5:55
4
  • 7
    Well how are you converting the values to an int array? Chances are you could skip that and convert them straight to bytes. Please show your code so far. (If you do need to convert the int[] to byte[], you can just create a new array and convert each value in turn, with a for loop. Again - what have you tried? Commented Aug 28, 2014 at 5:56
  • Please post the code that you have so far. Commented Aug 28, 2014 at 5:59
  • String string_as_int[] = U.split(" "); using a for loop and Integer.parseInt(); I converted them to a int array... Commented Aug 28, 2014 at 5:59
  • 1
    So when you can use the java.lang.Integer-class why not look at java.lang.Byte as well? Commented Aug 28, 2014 at 6:17

3 Answers 3

1

You should be able to do:

String[] parts = my.split(" ");
byte[] bytes = new byte[parts.length];
for(int i = 0; i < parts.length; i++) {
 bytes[i] = Byte.parseByte(parts[i]);
}
System.out.println(Arrays.toString(bytes));

[16, 12, 12, -119, 102, 105, -110, 52, -89, 80, -122, -68, 114, 20, -92, -28, -121, 38, 113, 61]

Be sure to look over the API for the Byte class, notably Byte.parseByte().

answered Aug 28, 2014 at 6:08
Sign up to request clarification or add additional context in comments.

1 Comment

No prob, homes. Remember each of the primitive types has an associated class with helpful utilities (Byte, Short, Character, etc.). Additionally, the Guava library provides several more useful primitive utilities.
1

Edit: You apparently can do most of my answer except for one line. You can just replace this line:

int v = Integer.parseInt(aa[q]);

with this (there's no need to make it an int first, so just skip it and go right to the byte):

bb[q] = Byte.parseByte(aa[q]);

Or you could just cast the int you created to a byte, like this:

int v = Integer.parseInt(aa[q]);
bb[q] = (byte)v;

The first thing you do is convert that single String into an array of Strings by using the String#split() method, like this:

String[] strArray = my.split(" "); // split by the spaces

Then, create a byte array, which will be the same length as the string array:

byte[] byteArray = new byte[strArray.length];

Then iterate over the String array and add each element of the String array to the byte array. Each time you add a number, you have to parse it to a byte from a String, using the Byte#parseByte(String s) method:

for (int i = 0; i < byteArray.length; i++) {
 byteArray[i] = Byte.parseByte(strArray[i]);
}

And then you should have your byte array.

answered Aug 28, 2014 at 6:12

Comments

0

You can split the string and parse each individual String token as Byte :-

String my = "16 12 12 -119 102 105 -110 52 -89 80 -122 -68 114 20 -92 -28 -121 38 113 61";
String [] ints = my.split (" ");
byte[] bArr=new byte[ints.length];
for(int i=0;i<ints.length;i++){
 bArr[i]=Byte.parseByte(ints[i]);
 System.out.println(bArr[i]);
}
answered Aug 28, 2014 at 6:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.