• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String bytes manipulation

 
Gustavo Berndorfer Goldhardt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

Someone could help me doing the following operation:

I have a String(a big one), and I need to "split" this String in smaller Strings of 50 bytes each, and put all these 50 bytes Strings into an ArrayList.

Thank you guys!!
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We love to help folks here, but we also ask that you Do Your Own Work. What have you tried to solve your problem? Do you have some code we can discuss?
 
Gustavo Berndorfer Goldhardt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in Java, and I have just a little experience in String manipulation. This is a quick problem to solve if you are a Java expert or at least have a considerable experience in it, but if you are a newbie(like me) this is an extremelly hard operation. I not asking you to solve the problem for me, but if you can point me a solution, or even indicate examples in manipulating bytes of a String. But if you have the algoritihm, I would be very happy if you share it with me and the other newbies in this community...

Thank you very much!

 
Gustavo Berndorfer Goldhardt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes, I tryed to solve the problem myself... But I'm getting lost and confused when I need to manipulate bytes instead of characters.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I understand this, in the ArrayList you will store Strings of around 50 bytes, correct?

You must know that in Java, String may contain characters in different encodings (i.e ASCII, UTF-8, UTF-16, etc, etc, etc).

Therefore, the binary representation of the String characters could vary depending on the enconding. If your encoding is ASCII then in binary form most probably every byte would correspond to a character, but the same cannot be said of all other encodings. Also, it is important to highligh that the number of bytes required to store the characters of a String in particular enconding has nothing to do with the size in bytes of a String object if serialized.

That being said, in what encoding should your strings fit in 50 bytes chunks?

I ask this because spliting the string based on bytes might leave you with a chuck of bytes that has not sense in particular encoding.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic