How does one convert between byte[] and string?
MF 9 4018
Matt F wrote: How does one convert between byte[] and string?
Will this help?
ASCIIEncoding encoding = new ASCIIEncoding();
char[] charArray = new char[encoding.GetCharCount(byteArray, 0, byteArray.Length)];
encoding.GetDecoder().GetChars(byteArray, 0, byteArray.Length, charArray, 0);
string s = new string(charArray);
string to byte[]:
byte[] b = Encoding.ASCII.GetBytes("Hello World");
byte[] to string:
string s = Encoding.ASCII.GetString(b);
Of course you should use the appropriate encoding in place of ASCII in
my examples.
You use the appropriate System.Text.Decoder class to convert the array to
the Text encoding that it represents/contains. This is typically done by
using an instance of the appropriate System.Text.Encoding class, and calling
the GetDecoder() method. Of course, you have to know what encoding was used
to create the byte array.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Matt F" <ma********************@gmail.com> wrote in message
news:o8Edg.75169$IZ2.51400@dukeread07... How does one convert between byte[] and string?
MF
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote: You use the appropriate System.Text.Decoder class to convert the array to the Text encoding that it represents/contains. This is typically done by using an instance of the appropriate System.Text.Encoding class, and calling the GetDecoder() method. Of course, you have to know what encoding was used to create the byte array.
Why use a Decoder explicitly, when Encoding.GetString will do it for
you? You only need a Decoder if you're decoding multiple chunks which
may introduce an issue of half of an encoded character being in one
chunk, and half being in the other chunk.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Matt F <ma********************@gmail.com> wrote: How does one convert between byte[] and string?
That entirely depends on the contents of the byte array, and how you
want to represent it.
The other answers in this thread have focused on the situation where
the byte array contains text which has been encoded with a particular
character encoding. If, however, your byte array contains arbitrary
binary data, you should consider using Convert.ToBase64String and
Convert.FromBase64String.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sericinus hunter <se*****@flash.net> wrote: Matt F wrote: How does one convert between byte[] and string?
Will this help?
ASCIIEncoding encoding = new ASCIIEncoding(); char[] charArray = new char[encoding.GetCharCount(byteArray, 0, byteArray.Length)]; encoding.GetDecoder().GetChars(byteArray, 0, byteArray.Length, charArray, 0); string s = new string(charArray);
That looks like a very long way of writing:
string s = Encoding.ASCII.GetString(byteArray);
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
The qyestion was asked in a geneneral way, so I have a less targeted
approach, which show the underling process, rather than a specific situation
and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set
of rules to establish the specific need, I am left to drop to more
elelementary explanation of the underling technology, from which I hop the
OP to glean the spedific implementation.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote: You use the appropriate System.Text.Decoder class to convert the array to the Text encoding that it represents/contains. This is typically done by using an instance of the appropriate System.Text.Encoding class, and calling the GetDecoder() method. Of course, you have to know what encoding was used to create the byte array.
Why use a Decoder explicitly, when Encoding.GetString will do it for you? You only need a Decoder if you're decoding multiple chunks which may introduce an issue of half of an encoded character being in one chunk, and half being in the other chunk.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote: The qyestion was asked in a geneneral way, so I have a less targeted approach, which show the underling process, rather than a specific situation and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set of rules to establish the specific need, I am left to drop to more elelementary explanation of the underling technology, from which I hop the OP to glean the spedific implementation.
While I see your point, I would say that probably 99% of the time
people *don't* need to use a Decoder - and that using a Decoder
explicitly will make their code more complicated.
I can't remember the last time I needed to use Decoder rather than just
calling Encoding.GetString - I suspect it may have been when I was
writing my own version of BinaryReader, which is far from a normal
situation :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Boy, I must have been tired when I wrote my reply! I can't believe how many
speeling errors it contained! I'm so ashamed!
BTW, I too almost never use a Decoder explicitly!
--
;-),
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote: The qyestion was asked in a geneneral way, so I have a less targeted approach, which show the underling process, rather than a specific situation and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set of rules to establish the specific need, I am left to drop to more elelementary explanation of the underling technology, from which I hop the OP to glean the spedific implementation.
While I see your point, I would say that probably 99% of the time people *don't* need to use a Decoder - and that using a Decoder explicitly will make their code more complicated.
I can't remember the last time I needed to use Decoder rather than just calling Encoding.GetString - I suspect it may have been when I was writing my own version of BinaryReader, which is far from a normal situation :)
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: matt melton |
last post by:
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.
ie. I'd like to be able to do...
|
by: Hal Vaughan |
last post by:
If I have a byte and I convert it to string (String sData = new
String(byte bData), then convert it back (byte bData = sData.getBytes()),
will all data be intact, or do Strings have problems with...
|
by: Prabhu |
last post by:
Hi,
We are having problem in converting a byte array to string, The byte array
has char(174), char(175), char(240), char(242) and char(247) as delimiters
for the message.
when we use...
|
by: iyuen |
last post by:
I'm having problems with converting a byte array to an image object~
My byte array is an picture in VB6 StdPicture format. I've used propertybag
to convert the picture into base64Array format in...
|
by: Marius Cabas |
last post by:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file...
| |
by: dlarock |
last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I
think) with the conversion from the Byte MD5 hash back to string.
Watching this through the debugger it appears as if the...
|
by: David |
last post by:
I note that you can null teminate a string by adding controlchar.null.
Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.
I have packets coming in from a...
|
by: moondaddy |
last post by:
I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array. However, its
getting scrambled in the conversion. In short,...
|
by: manmit.walia |
last post by:
Hello All,
I have tried multiple online tools to convert an VB6 (bas) file to
VB.NET file and no luck. I was hoping that someone could help me covert
this. I am new to the .NET world and still...
|
by: ogtheterror |
last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working.
Is there anyone that knows how to get this into a .net...
|
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,...
| |
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |