469,081 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,081 developers. It's quick & easy.

Convert Byte() <-> String

Hi all,

I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class library.

Can anyone suggest a better way to do this?

TIA - Bob

Jun 27 '08 #1
6 4977
On Jun 22, 8:05 pm, "Bob Altman" <r...@nospam.nospamwrote:
Hi all,

I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class library.

Can anyone suggest a better way to do this?

TIA - Bob
Hi,
Would you consider using BitConverter class:

http://www.java2s.com/Code/CSharp/De...tConverter.htm
http://msdn.microsoft.com/en-us/libr...converter.aspx

Hope this helps,

Onur Güzel
Jun 27 '08 #2

"Bob Altman" <rd*@nospam.nospamkirjoitti viestissä
news:OE**************@TK2MSFTNGP04.phx.gbl...
Hi all,

I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class
library.

Can anyone suggest a better way to do this?
This might be what you are looking for:

Dim UTF8Converter As New System.Text.UTF8Encoding
Dim OriginalString = "This is a test."
Dim Bytes As Byte() = UTF8Converter.GetBytes(OriginalString)

MsgBox(UTF8Converter.GetString(Bytes))

There are other encodings as well.

-Teemu

Jun 27 '08 #3
"Bob Altman" <rd*@nospam.nospamschrieb:
I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class
library.
In addition to the above methods, take a look at
'System.Text.Encoding.GetString' and 'System.Text.Encoding.GetBytes'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 27 '08 #4
Hi Bob,

I think Base64 encoding is the common and sophisciated approach. Also, for
built-in text binary converting, you can have a look at the
System.Text.Encoding namespace has other members suggested. There contains
many encoding types(mainly used for converting Text characters to binary
encoding stream). You can use those unicode encoding for your scenario.
e.g.

=====================
Dim bytes() As Byte

bytes = System.Text.Encoding.UTF8.GetBytes(StringText)
=====================

Utf8 encoding is efficient for compression since it use different length
binary format for different characters. this helps when you want to get
compressed size of the encoded binary.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Bob Altman" <rd*@nospam.nospam>
Subject: Convert Byte() <-String
Date: Sun, 22 Jun 2008 10:05:48 -0700
>Hi all,

I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class library.

Can anyone suggest a better way to do this?

TIA - Bob

Jun 27 '08 #5
Also, for
built-in text binary converting, you can have a look at the
System.Text.Encoding namespace has other members suggested.
I thought about using System.Text.Encoding, but the problem with that is
that the Byte() data I am trying to convert to a String can contain any
arbitrary data. Most encodings (such as UTF-8) are only valid for a subset
of possible byte values or combinations of values.

I wasn't aware of the BitConverter class that Onur pointed out. The major
problem with that class is that it doesn't seem to provide symmetrical
encoding and decoding between strings and byte arrays. In other words, it
can convert a byte array to a string of hex digits separated by dashes, but
a quick look at the docs didn't reveal a way to convert the string back to a
byte array. Also, while the string created by BitConverter has the
advantage (in some applications) of being human-readable, it's twice as long
as Base64 string representation.

Jun 27 '08 #6
Thanks for your reply Bob,

Yes, if the data is pure binary with all kinds for values, those dedicated
binary/text encoding schemas (such as Base64 or Hex) are perferred. So far
I think the base64 encoding is the proper one here. The
BitConvertor.ToString method just help produce the hex encoding like
output, it will be effecient on processing time since it will always use
two character to represent each byte, however, it will consume much more
space.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Bob Altman" <rd*@nospam.nospam>
Subject: Re: Convert Byte() <-String
Date: Mon, 23 Jun 2008 07:53:52 -0700
>
>Also, for
built-in text binary converting, you can have a look at the
System.Text.Encoding namespace has other members suggested.

I thought about using System.Text.Encoding, but the problem with that is
that the Byte() data I am trying to convert to a String can contain any
arbitrary data. Most encodings (such as UTF-8) are only valid for a
subset
>of possible byte values or combinations of values.

I wasn't aware of the BitConverter class that Onur pointed out. The major
problem with that class is that it doesn't seem to provide symmetrical
encoding and decoding between strings and byte arrays. In other words, it
can convert a byte array to a string of hex digits separated by dashes,
but
>a quick look at the docs didn't reveal a way to convert the string back to
a
>byte array. Also, while the string created by BitConverter has the
advantage (in some applications) of being human-readable, it's twice as
long
>as Base64 string representation.

Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lamberti Fabrizio | last post: by
14 posts views Thread by Chris | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
6 posts views Thread by Mimi | last post: by
9 posts views Thread by Charles Law | last post: by
2 posts views Thread by Tedmond | last post: by
10 posts views Thread by =?Utf-8?B?Um95?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.