This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String Array to Byte Array

 
Randall Stevens
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to create the method getBytes.
My bad Wayne, you already posted. BLAAAAAAAAA
[ October 21, 2003: Message edited by: Steve Wysocki ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic