Convert Byte() <-> String

B

Bob Altman

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
 
K

kimiraikkonen

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/Development-Class/ConvertbytearraytoStringwithBitConverter.htm
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

Hope this helps,

Onur Güzel
 
T

Teemu

Bob Altman said:
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
 
H

Herfried K. Wagner [MVP]

Bob Altman said:
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'.
 
S

Steven Cheng [MSFT]

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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
B

Bob Altman

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

Steven Cheng [MSFT]

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:
(e-mail address removed).

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

--------------------
 

Ask a Question

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.

Ask a Question

Top