473,406 Members | 2,707 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,406 software developers and data experts.

Converting from byte[] to string and viceversa???

Hello,

a simple question (if you know the answer) ... how to convert from and array
of bytes to a string (and viceversa). I haven't the conversion to be simple
... but even using the Convert or the Encoding class I seem to be having
trouble accomplishing it.
Bob Rock


Jul 21 '05 #1
6 1562
I would use Encoding.Default.GetBytes for the String->Byte [] conversion

and Encoding.Default.GetString for the Byte []->String conversion

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Hello,

a simple question (if you know the answer) ... how to convert from and array of bytes to a string (and viceversa). I haven't the conversion to be simple .. but even using the Convert or the Encoding class I seem to be having
trouble accomplishing it.
Bob Rock

Jul 21 '05 #2
Dennis Myrén <de****@oslokb.no> wrote:
I would use Encoding.Default.GetBytes for the String->Byte [] conversion

and Encoding.Default.GetString for the Byte []->String conversion


That's only applicable if you *do* want to use the default encoding for
the current computer though - which in many cases isn't the right
choice. Guessing isn't a good idea with character encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Bob Rock <no***************************@hotmail.com> wrote:
a simple question (if you know the answer) ... how to convert from and array
of bytes to a string (and viceversa). I haven't the conversion to be simple
.. but even using the Convert or the Encoding class I seem to be having
trouble accomplishing it.


You need to know what encoding to use.

See http://www.pobox.com/~skeet/csharp/unicode.html for an explanation
of what it's all about.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Hehe.

Yes, i know that.
It was an _example_.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dennis Myrén <de****@oslokb.no> wrote:
I would use Encoding.Default.GetBytes for the String->Byte [] conversion

and Encoding.Default.GetString for the Byte []->String conversion


That's only applicable if you *do* want to use the default encoding for
the current computer though - which in many cases isn't the right
choice. Guessing isn't a good idea with character encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
System.Byte[] inputData;
string inputstream = System.Text.Encoding.ASCII.GetString(inputData) ;

Thats it !! :)

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Hello,

a simple question (if you know the answer) ... how to convert from and array of bytes to a string (and viceversa). I haven't the conversion to be simple .. but even using the Convert or the Encoding class I seem to be having
trouble accomplishing it.
Bob Rock

Jul 21 '05 #6
Sahil Malik <co*****************@nospam.com> wrote:
System.Byte[] inputData;
string inputstream = System.Text.Encoding.ASCII.GetString(inputData) ;

Thats it !! :)


Again, that's it if Encoding.ASCII *happens* to be the encoding you
want.

The code is going to be simple virtually whatever encoding you're
interested in, but working out which encoding is the right one to use
is very important - much more so than your post implied.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
6
by: Bob Rock | last post by:
Hello, a simple question (if you know the answer) ... how to convert from and array of bytes to a string (and viceversa). I haven't the conversion to be simple ... but even using the Convert or...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
5
by: Trapulo | last post by:
Hello, I need to send to a webservice a parameter that is a string containing an XML doc. In this xml, a node value came from a byte array (it's an RSA signature). What is the best way to convert...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
0
by: rrp83 | last post by:
Hi All Can anyone fwd me the code for converting a string to bytes similar to the GetBytes function in C# and viceversa?? Regards, RRP83
0
by: tarlino | last post by:
Hi! I'm play with the sound generation (c++ and directX). Now I would like to manage same filter on my sample. But now I must to convert my audio buffer "BYTE" (from DirectX) to an arry of "short"...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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
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
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.