473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

StringBuilder to byte array

What is the easiest way to convert StringBuilder to byte array?
Thanks
Nov 20 '07 #1
16 41731
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]
- mv*@spam.guard.caspershouse.com

"Peter" <pc*****@nospam.nospamwrote in message
news:ui**************@TK2MSFTNGP06.phx.gbl...
What is the easiest way to convert StringBuilder to byte array?
Thanks

Nov 20 '07 #2
Well, you'll need to pick an encoding... the simplest way is then
(using UTF8 here):
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());

How complex you need to make it depends on the scenario.

Marc
Nov 20 '07 #3
Marc,

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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:d2**********************************@y5g2000h sf.googlegroups.com...
Well, you'll need to pick an encoding... the simplest way is then
(using UTF8 here):
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());

How complex you need to make it depends on the scenario.

Marc

Nov 20 '07 #4
It seems (and I could be wrong here) that the OP just wants the
bytes that make up the string
Yes, but that statement itself is ambiguous. It would be rare to
want to inspect the actual machine memory of a .NET string when
the char-buffer is readily available, so I'll assume that this is for
serialization purposes.
and the Unicode Encoding instance is the most resiliant way
of doing that
Well, the most resiliant way is to agree in advance which encoding
is being used ;-p
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.

Marc
Nov 20 '07 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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).
I don't think we really have enough information to say what the OP
really wants, to be honest. If they can pick an encoding, then UTF-8 is
usually a very good choice; if not, that's a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 20 '07 #6
Marc,

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. Granted, for the data that you are using, it
is what you most commonly see, but if you simply want to make sure that all
data that can be stored in a .NET string can be accurately represented in a
byte array, you use the Unicode encoding.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:8a**********************************@c30g2000 hsa.googlegroups.com...
>It seems (and I could be wrong here) that the OP just wants the
bytes that make up the string
Yes, but that statement itself is ambiguous. It would be rare to
want to inspect the actual machine memory of a .NET string when
the char-buffer is readily available, so I'll assume that this is for
serialization purposes.
>and the Unicode Encoding instance is the most resiliant way
of doing that
Well, the most resiliant way is to agree in advance which encoding
is being used ;-p
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.

Marc

Nov 20 '07 #7
Marc Gravell <ma**********@gmail.comwrote:
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.
And also UTF-8 does *support* full unicode - there's no real compromise
here unless you're talking about situations where UTF-8 is bigger, or
you need to quickly access a specific character index (which is where
any variable-width encoding falls down).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 20 '07 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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.
In what way? Any valid string can be represented in UTF-8.

The only situation in which you may run into problems is if you've got
a surrogate pair issue (e.g. a high surrogate with no corresponding low
surrogate, or vice versa), but I'm not sure that other encodings would
(or should) handle that situation losslessly either. It's basically a
corrupt string at that point.

Could you give an example string where encoding to UTF-8 and then
decoding risks losing data?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 20 '07 #9
there's no real compromise here unless...
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).

Marc
Nov 20 '07 #10
"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:8c**********************************@w34g2000 hsg.googlegroups.com...
>there's no real compromise here unless...
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).
Then you've got to use UTF32 to make it work in all cases ;-)

Christof

Nov 21 '07 #11
I think I'll just choose to accept that those days are gone, and stick
with UTF-8 (when there is a choice).

Marc
Nov 21 '07 #12

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eR**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Peter" <pc*****@nospam.nospamwrote in message
news:ui**************@TK2MSFTNGP06.phx.gbl...
>What is the easiest way to convert StringBuilder to byte array?
Thanks


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.


Nov 21 '07 #13
On 2007-11-21 11:10:24 -0800, "Peter" <pc*****@nospam.nospamsaid:
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.
I'm not clear on why you're using StringBuilder, or strings at all.

If you're just trying to copy a PDF file, you should be able to just
transfer the bytes of the file and save them to disk verbatim. Running
the PDF through some text decoding and reencoding can only create more
hassles, IMHO.

Pete

Nov 21 '07 #14
Peter <pc*****@nospam.nospamwrote:

<snip>
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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 21 '07 #15

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Peter <pc*****@nospam.nospamwrote:

<snip>
>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.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
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

Nov 21 '07 #16
Peter <pc*****@nospam.nospamwrote:
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
That's far from an ideal example, unfortunately - in particular, it
only ever works with ASCII text. In some cases that's what you want,
but not always.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 21 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gene | last post by:
I'm not sure I understand this behavior (at least in the context of C#). I understand the use of "ref" and "out" keywords as they apply to method arguments, and then something this morning...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
2
by: Ron | last post by:
Hello, I am trying to read a list of files from an FTP server (mainframe) to a byte array using sockets as follows, but not getting all the files in a given directory: private readonly static...
6
by: realgeek | last post by:
I have a byte array with binary data and I want to get its contants into a string. tagContent = System.Text.Encoding.ASCII.GetString(str); screws binary data. foreach (byte b in str)
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
3
by: marfi95 | last post by:
Hi all. I need to copy a byte array into a string, but starting at a specific location in the byte array. This is where I get hung up. For example if my byte array is (100) big, I might want to...
1
by: Brian Mitchell | last post by:
I'm sorry if this is the wrong group but I couldn't find one relating to cryptography. I have a byte array that I am encrypting using the System.Cryptography classes and it encrypts just...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: zeroonea | last post by:
hi there, i playing around with an async server, i use BeginReceive with byte array to store incoming data, when a "big" data come, a call back receive can not read it all in one time, so i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.