473,494 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5235
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
4795
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
4102
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
14
29280
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
10341
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
34561
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
25574
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
12660
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
2874
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
5889
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
4841
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
7157
Oralloy
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,...
0
7195
jinu1996
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...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
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...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.