• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split the string based on byte array  RSS feed

 
yuvipoy poy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to split a string based on byte array
Byte array length.
---------------------
4 bytes
8 bytes
2 bytes
4 bytes
.
.
.
.
i know the byte array i need to split the string based on byte array.

Thanks!
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String split based on delimiter and yours seems to be based on size?

Would the following be useful for you?
String.getBytes()
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Your question is by no means clear; please explain a bit more.
 
yuvipoy poy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stay a string "abcd12345@1!#^*($!"
i need to split
abcd----4 bytes
12345@1!----8 bytes
#^----2 bytes
*($!----4 bytes
the above are not bytes in the example it is character.
here i have represented as character where the string will have array of information, combination of byte arrays.

Thanks!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yuvipoy poy wrote:here i have represented as character where the string will have array of information

Yes, but you haven't represented how we are supposed to know where to split this string/byte array up.

How are we supposed to know that you want "abcd" in one portion and "12345@1" in another? Divine guidance? Hazel twigs?

You need to tell us ALL the problem before we can help.

Winston
 
yuvipoy poy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the byte lenght(here 4,8,2,4) where i need to get the data as

abcd----4 bytes one portion
12345@1!----8 bytes another
#^----2 bytes
*($!----4 bytes
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings don't consist of bytes; they consist of characters. Don't use strings to store arbitrary bytes, they are not suited for that.

Assuming that you actually mean characters and not bytes, you can use methods in class String, such as substring(), to get parts of the string. See the API documentation.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Strings don't consist of bytes; they consist of characters. Don't use strings to store arbitrary bytes, they are not suited for that.

A VERY important point, yuvipoy. Bytes and characters are NOT the same thing.

And if you are, in fact, using byte arrays, you should have a look at the Arrays class
(java.util.Arrays (←click)), because it has very useful methods for extracting portions of arrays.

Winston
 
yuvipoy poy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings don't consist of bytes; they consist of characters. Don't use strings to store arbitrary bytes, they are not suited for that.

what is the type i need to use to store the information and split.

Assuming that you actually mean characters and not bytes, you can use methods in class String, such as substring(), to get parts of the string. See the API documentation.

No i mean bytes not chracters.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yuvipoy poy wrote:No i mean bytes not chracters.

Then use a byte[]. Or (possibly even better), write your own class (Bytes?) that wraps a byte[].

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!