1

I need to convert String[] to Byte[] in Java. Essentially, I have a space delimited string returned from my database. I have successfully split this String into an array of string elements, and now I need to convert each element into a byte, and produce a byte[] at the end.

So far, the code below is what I have been able to put together but I need some help making this work please, as the getBytes() function returns a byte[] instead of a single byte. I only need a single byte for the string (example string is 0xd1 )

byte[] localbyte = null;
if(nbytes != null)
{
    String[] arr = (nbytes.split(" ")); 
    localbyte = new byte[arr.length];  
    for (int i=0;  i<localbyte.length;  i++) {
        localbyte[i] = arr[i].getBytes();  
    }
}
7
  • 4
    Can you tell us what you're attempting to accomplish? There might be a better way to do it. Commented Sep 16, 2012 at 2:35
  • 3
    I second @oldrinb's suggestion. Also after answering his question, you'll probably want to give more details about your problem and how your current solution is not working. But first let's make sure you're not barking up the wrong tree. Commented Sep 16, 2012 at 2:36
  • I am trying to take a string, split it into an array, remove empty entries into the array, and then convert the array of strings into an array of bytes. Commented Sep 16, 2012 at 2:42
  • 2
    The best, simplest and clearest thing is to show us sample input String and output byte[], and let us do the rest - can you post that? Commented Sep 16, 2012 at 2:45
  • 2
    But why the array of bytes? What are you going to do with this byte[]? What is its purpose? Commented Sep 16, 2012 at 2:53

3 Answers 3

1

I assume you'd like to split strings like this:

"Hello  world!"

Into "Hello", "world!" instead of "Hello", " ", "world!"

If that's the case, you can simply tweak on the split regex, using this instead:

String[] arr = (nbytes.split(" +"));
Sign up to request clarification or add additional context in comments.

Comments

1

You should be familiar with regular expression. Instead of removing empty string after splitting, you can split the string with one or more white space:

To split a string by space or tab, you can use:

String[] arr = (nbytes.split("\\p{Blank}+"));

E.g.

"Hello \tworld!" 

results in

"Hello","world!"

To split a string by any whitespace, you can use:

String[] arr = (nbytes.split("\\p{Space}+"));

E.g

"Hello \tworld!\nRegular expression" 

results in

"Hello","world!","Regular","expression"

3 Comments

I think I Have the part to split the string down. I need the conversion a string to byte.
A character is represented by a byte(at least), a string is represented by a series of characters, yield represented by a series of bytes. So you can't convert a string to a byte, except that you compute the string to return a byte.
How do I compute the string to return a byte, in Java?
0

What about Byte(String string) (Java documentation).
Also you might want to look up Byte.parseByte(string) (doc)

byte[] localbyte = null;
if(nbytes != null)
{
    String[] arr = (nbytes.split(" ")); 
    localbyte = new byte[arr.length];  
    for (int i=0;  i<localbyte.length;  i++) {
        localbyte[i] = new Byte(arr[i]);  
    }
}

Notice:

The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value.

So you might want to catch the NumberFormatException.
If this is not what your looking for maybe you can provide additional information about nbytes ?

Also Michael's answer could turn out helpful: https://stackoverflow.com/a/2758746/1063730

1 Comment

The conversion method Byte.parseByte(string) is what I was looking for. Thanks

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.