473,785 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert type 'byte[]' to 'string'

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

Thanks
EitanB
Oct 5 '07 #1
7 2484
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***@discussi ons.microsoft.c omschrieb im Newsbeitrag
news:4A******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c omschrieb im Newsbeitrag
news:4A******** *************** ***********@mic rosoft.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...@discussi ons.microsoft.c omwrote:
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.GetStr ing.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase6 4String
and Convert.FromBas e64String.

Jon

Oct 5 '07 #5
Thanks
EitanB

"Jon Skeet [C# MVP]" wrote:
On Oct 5, 2:51 pm, Eitan <Ei...@discussi ons.microsoft.c omwrote:
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.GetStr ing.

If it's arbitrary binary data that you want to preserve as a string
and then convert back to binary data later, use Convert.ToBase6 4String
and Convert.FromBas e64String.

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.En coding.UTF8.Get String(myByteAr ray);
and
byte[] myByteArray = System.Text.Enc oding.UTF8.GetB ytes(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.En coding.UTF8.Get String(myByteAr ray);
and
byte[] myByteArray = System.Text.Enc oding.UTF8.GetB ytes(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
16429
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
49651
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 row.ToString() But it give result "System.Byte" instead of actual text...
14
29520
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'
4
2398
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#. But the compiler complains: C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(333): Cannot
1
2308
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
53725
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 do I convert it to: dim myStr as string = "11,22,33,44"
8
5911
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 ?
4
4660
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 sngArray(0) As Single strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65) CopyMemory sngArray(0), ByVal strString, Len(strString) After running the code sngArray(0) = 17.40548
19
5343
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 str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
0
10783
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9645
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
10327
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
10151
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
10092
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
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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 we have to send another system
3
2879
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.