File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Convert String Array to Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert String Array to Byte Array" Watch "Convert String Array to Byte Array" New topic
Author

Convert String Array to Byte Array

Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
I have a string array that I converted to the data to 4 hex strings. I placed the hex strings into each location of the string array.
For example;
String [] hex_Str;
hex_Str[0] = Integer.toHexString(127);
hex_Str[1] = Integer.toHexString(0);
hex_Str[2] = Integer.toHexString(0);
hex_Str[3] = Integer.toHexString(1);
and return hex_Str
I have another method that takes the string array and wishes to place it into a byte array.
for(int i=0;i<4;i++) {
params[i]= (byte) hext_Str[i].charAt(i);
}
I get an index out of bounds error, what appears to be the problem?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You are using the same value ("i") as both the index to the array element (hext_Str[i]) and the character offset (.charAt(i)). That translates into the first character of the first entry, the second character of the second entry, etc. That's probably not what you want.
In fact, hext_Str[1] will be the string "0" [set from Integer.toHexString(0)]. The second time through your loop the system will do:
(byte)hext_Str[1].charAt(1);
This will cause a String index out of bounds error, since there is no second character [charAt(1)].
[ October 21, 2003: Message edited by: Wayne L Johnson ]
Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65

In fact, hext_Str[1] will be the string "0" [set from Integer.toHexString(0)]. The second time through your loop the system will do:
(byte)hext_Str[1].charAt(1);
This will cause a String index out of bounds error, since there is no second character [charAt(1)].

What do I need to do in order to correct the issue? Do I need to set the charAt() to a 0?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
If you do a ".charAt(0)" it will work, but you will only get the first byte of each string. Is that what you want? Are you trying to get the bytes from every element of hext_Str into one byte array, essentially doing a concatenation?
To convert a String into a byte array, all you need to do is call the "getBytes()" method, which returns a byte array. So to convert the String array into corresponding byte array, the following would work:
Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65

byte[] params = new byte[hext_Str.length];
for (int i=0; i<params.length; i++) {
params[i] = hext_Str.getBytes();
}

The compiler states that it cannot resolve symbol: method getBytes() in class java.lang.String[]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
That's what I get for quickly throwing together a code snippet as I was dashing off to a meeting. Try something like this:

hext_Str is a String array, so you need to call the "getBytes()" method on an array element. And the call returns a byte array, so you need an array of arrays (hence byte[][]) to store the results if you want to capture each one individually.
Sorry for the confusion.
[ October 21, 2003: Message edited by: Wayne L Johnson ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
You have to create the method getBytes.
My bad Wayne, you already posted. BLAAAAAAAAA
[ October 21, 2003: Message edited by: Steve Wysocki ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Convert String Array to Byte Array