473,233 Members | 1,543 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,233 software developers and data experts.

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 5214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lamberti Fabrizio | last post by:
Hi all. I've got an ActiveX Object that gives me OleColor codes for its graphical components. I need to convert these OleColor codes into RGB codes using JScript or VBScript, becuase I have...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
6
by: Mimi | last post by:
Is there a way to convert int to byte easily? I want to write my int indexTbl to a MemoryStream but the MemoryStream class only accepts byte buffer. I have more than one int indexTbl to write...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
2
by: Tedmond | last post by:
Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Tedmond
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
10
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.