P
Peter
What is the easiest way to convert StringBuilder to byte array?
Thanks
Thanks
Yes, but that statement itself is ambiguous. It would be rare tobytes that make up the string
Well, the most resiliant way is to agree in advance which encodingand the Unicode Encoding instance is the most resiliant way
of doing that
Nicholas Paldino said:I don't know that I would pick UTF8, but rather, I'd use the Unicode
encoding. It seems (and I could be wrong here) that the OP just wants the
bytes that make up the string, and the Unicode Encoding instance is the most
resiliant way of doing that (assuming readability isn't a factor, and at a
cost of twice the storage space).
Marc Gravell said:Maybe it is just the data I work with, but I still see more UFT8
(heck, mainly ascii) than I do unicode, so UTF8 makes a good compromise
between working with legacy files and supporting full unicode. The
space saving is nice but (generally) a side benefit.
Nicholas Paldino said:To elaborate, when I say "the bytes that make up the string", what I
mean is a serialized, ^lossless^ transformation of the string into bytes.
This can't be done with UTF-8.
Then you've got to use UTF32 to make it work in all cases ;-)Marc Gravell said:I was indeed (although I didn't make it clear) thinking of the gool
ol' days of being able to seek a stream by the character offset (give
or take a fixed multiple).
Nicholas Paldino said:Well, you should just create a string from the StringBuilder (calling
ToString) and then you can serialize that using the BinaryFormatter, or
call the GetBytes method on the Unicode Encoding instance exposed by the
static Unicode property on the Encoding class to return the bytes.
The latter option is more than likely easier. The former was mentioned
just to show that there are multiple ways to do it.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Peter said:What is the easiest way to convert StringBuilder to byte array?
Thanks
FYI:
what I am trying to do is retrieve a PDF file through a socket, I read a
socket and store the data into a StringBuilder and after it's done I want to
save the StringBuilder data which is a PDF file on to a hard drive.
Thanks everyone for the input, it was very educational.
FYI:
what I am trying to do is retrieve a PDF file through a socket, I read a
socket and store the data into a StringBuilder and after it's done I want to
save the StringBuilder data which is a PDF file on to a hard drive.
Jon Skeet said:As far as I'm aware, PDFs are binary data - they shouldn't be treated
as text.
Don't store the data in a StringBuilder, either stream it straight to
disk or store it in a MemoryStream.
Peter said:Thanks for advice, the only reason I am using the StringBuilder is because I
found an example on MSDN
http://msdn2.microsoft.com/en-us/library/bew39x2a.aspx
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.