473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ 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.ToBase6 4String and Convert.FromBas e64String 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 5276
On Jun 22, 8:05 pm, "Bob Altman" <r...@nospam.no spamwrote:
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.ToBase6 4String and Convert.FromBas e64String 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.nos pamkirjoitti viestissä
news:OE******** ******@TK2MSFTN GP04.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.ToBase6 4String and Convert.FromBas e64String 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.UTF 8Encoding
Dim OriginalString = "This is a test."
Dim Bytes As Byte() = UTF8Converter.G etBytes(Origina lString)

MsgBox(UTF8Conv erter.GetString (Bytes))

There are other encodings as well.

-Teemu

Jun 27 '08 #3
"Bob Altman" <rd*@nospam.nos pamschrieb:
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.ToBase6 4String and Convert.FromBas e64String 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.En coding.GetStrin g' and 'System.Text.En coding.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.Enc oding 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.Enc oding.UTF8.GetB ytes(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****@microsof t.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.nos pam>
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.ToBase 64String and Convert.FromBas e64String 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.Enc oding namespace has other members suggested.
I thought about using System.Text.Enc oding, 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.To String 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****@microsof t.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.nos pam>
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.En coding namespace has other members suggested.

I thought about using System.Text.Enc oding, 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
4815
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 to using that color inside color style attribute of some HTML components. Anyone can help me ?
19
4150
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
14
29506
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 a = (bool)i1; // ERROR : convert type 'byte' to 'bool'
3
10364
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, etc. I need: short = byte+byte, short = byte+byte, etc.
15
34601
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
25597
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 to the stream so it would be nice if I could just write the whole int as a block and then read it back later when needed. I thought the Buffer class might be handy here but it seems to be best for converting byte to int with Block Copy.
9
12687
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
2893
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
5909
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 vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
10
4896
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 performance in my case.
0
8984
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.