File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes String bytes manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "String bytes manipulation" Watch "String bytes manipulation" New topic

String bytes manipulation

Gustavo Berndorfer Goldhardt

Joined: Nov 16, 2009
Posts: 6
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

Joined: Oct 29, 2001
Posts: 9188

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?

[How To Ask Questions On JavaRanch]
Gustavo Berndorfer Goldhardt

Joined: Nov 16, 2009
Posts: 6
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

Joined: Nov 16, 2009
Posts: 6
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

Joined: Dec 31, 2004
Posts: 961
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.
I agree. Here's the link:
subject: String bytes manipulation
It's not a secret anymore!