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

convert type 'byte[]' to 'string'

What would be the "right" way to convert type 'byte[]' to 'string'?

Thanks
EitanB
Oct 5 '07 #1
7 2461
And vise vers, i.e.: convert type 'string' to 'byte[]'

"Eitan" wrote:
What would be the "right" way to convert type 'byte[]' to 'string'?

Thanks
EitanB
Oct 5 '07 #2
"Eitan" <Ei***@discussions.microsoft.comschrieb im Newsbeitrag
news:4A**********************************@microsof t.com...
What would be the "right" way to convert type 'byte[]' to 'string'?
The method GetString of the right Encoding instance.
For the other direction use GetBytes.

In either case, you have to know the right encoding

Christof

Oct 5 '07 #3
Thanks Christof
Eitan

"Christof Nordiek" wrote:
"Eitan" <Ei***@discussions.microsoft.comschrieb im Newsbeitrag
news:4A**********************************@microsof t.com...
What would be the "right" way to convert type 'byte[]' to 'string'?
The method GetString of the right Encoding instance.
For the other direction use GetBytes.

In either case, you have to know the right encoding

Christof

Oct 5 '07 #4
On Oct 5, 2:51 pm, Eitan <Ei...@discussions.microsoft.comwrote:
What would be the "right" way to convert type 'byte[]' to 'string'?
That entirely depends on what the data is. If it's intrinsically text
data, you should pick the right encoding and use Encoding.GetString.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase64String
and Convert.FromBase64String.

Jon

Oct 5 '07 #5
Thanks
EitanB

"Jon Skeet [C# MVP]" wrote:
On Oct 5, 2:51 pm, Eitan <Ei...@discussions.microsoft.comwrote:
What would be the "right" way to convert type 'byte[]' to 'string'?

That entirely depends on what the data is. If it's intrinsically text
data, you should pick the right encoding and use Encoding.GetString.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase64String
and Convert.FromBase64String.

Jon

Oct 5 '07 #6
Assuming the data is in a format and encoding that will correctly convert,
you can use shorthand static methods like:

string myString =System.Text.Encoding.UTF8.GetString(myByteArray);
and
byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Eitan" wrote:
What would be the "right" way to convert type 'byte[]' to 'string'?

Thanks
EitanB
Oct 5 '07 #7
Thanks,
EitanB

"Peter Bromberg [C# MVP]" wrote:
Assuming the data is in a format and encoding that will correctly convert,
you can use shorthand static methods like:

string myString =System.Text.Encoding.UTF8.GetString(myByteArray);
and
byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Eitan" wrote:
What would be the "right" way to convert type 'byte[]' to 'string'?

Thanks
EitanB
Oct 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
4
by: Serge Klokov | last post by:
Hello! I have a Oracle table with a BLOB field. Each field is actually a simple text file of several lines. How to get this BLOB fields in readable format? I tryied something like...
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...
4
by: dale zhang | last post by:
Hi, I am trying to save an image to MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp For save button, I converted his VB to the following C#....
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
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...
4
by: Peter | last post by:
Does anyone know how to convert the following VB6 code to C# code? Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long) Dim...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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
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,...

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.