How does one convert between byte[] and string?
MF 9 4019
Matt F wrote: How does one convert between byte[] and string?
Will this help?
ASCIIEncoding encoding = new ASCIIEncoding() ;
char[] charArray = new char[encoding.GetCha rCount(byteArra y, 0, byteArray.Lengt h)];
encoding.GetDec oder().GetChars (byteArray, 0, byteArray.Lengt h, charArray, 0);
string s = new string(charArra y);
string to byte[]:
byte[] b = Encoding.ASCII. GetBytes("Hello World");
byte[] to string:
string s = Encoding.ASCII. GetString(b);
Of course you should use the appropriate encoding in place of ASCII in
my examples.
You use the appropriate System.Text.Dec oder class to convert the array to
the Text encoding that it represents/contains. This is typically done by
using an instance of the appropriate System.Text.Enc oding class, and calling
the GetDecoder() method. Of course, you have to know what encoding was used
to create the byte array.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Matt F" <ma************ ********@gmail. com> wrote in message
news:o8Edg.7516 9$IZ2.51400@duk eread07... How does one convert between byte[] and string?
MF
Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. com> wrote: You use the appropriate System.Text.Dec oder class to convert the array to the Text encoding that it represents/contains. This is typically done by using an instance of the appropriate System.Text.Enc oding class, and calling the GetDecoder() method. Of course, you have to know what encoding was used to create the byte array.
Why use a Decoder explicitly, when Encoding.GetStr ing will do it for
you? You only need a Decoder if you're decoding multiple chunks which
may introduce an issue of half of an encoded character being in one
chunk, and half being in the other chunk.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Matt F <ma************ ********@gmail. com> wrote: How does one convert between byte[] and string?
That entirely depends on the contents of the byte array, and how you
want to represent it.
The other answers in this thread have focused on the situation where
the byte array contains text which has been encoded with a particular
character encoding. If, however, your byte array contains arbitrary
binary data, you should consider using Convert.ToBase6 4String and
Convert.FromBas e64String.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sericinus hunter <se*****@flash. net> wrote: Matt F wrote: How does one convert between byte[] and string?
Will this help?
ASCIIEncoding encoding = new ASCIIEncoding() ; char[] charArray = new char[encoding.GetCha rCount(byteArra y, 0, byteArray.Lengt h)]; encoding.GetDec oder().GetChars (byteArray, 0, byteArray.Lengt h, charArray, 0); string s = new string(charArra y);
That looks like a very long way of writing:
string s = Encoding.ASCII. GetString(byteA rray);
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
The qyestion was asked in a geneneral way, so I have a less targeted
approach, which show the underling process, rather than a specific situation
and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set
of rules to establish the specific need, I am left to drop to more
elelementary explanation of the underling technology, from which I hop the
OP to glean the spedific implementation.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. com> wrote: You use the appropriate System.Text.Dec oder class to convert the array to the Text encoding that it represents/contains. This is typically done by using an instance of the appropriate System.Text.Enc oding class, and calling the GetDecoder() method. Of course, you have to know what encoding was used to create the byte array.
Why use a Decoder explicitly, when Encoding.GetStr ing will do it for you? You only need a Decoder if you're decoding multiple chunks which may introduce an issue of half of an encoded character being in one chunk, and half being in the other chunk.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. com> wrote: The qyestion was asked in a geneneral way, so I have a less targeted approach, which show the underling process, rather than a specific situation and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set of rules to establish the specific need, I am left to drop to more elelementary explanation of the underling technology, from which I hop the OP to glean the spedific implementation.
While I see your point, I would say that probably 99% of the time
people *don't* need to use a Decoder - and that using a Decoder
explicitly will make their code more complicated.
I can't remember the last time I needed to use Decoder rather than just
calling Encoding.GetStr ing - I suspect it may have been when I was
writing my own version of BinaryReader, which is far from a normal
situation :)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Boy, I must have been tired when I wrote my reply! I can't believe how many
speeling errors it contained! I'm so ashamed!
BTW, I too almost never use a Decoder explicitly!
--
;-),
Kevin Spencer
Microsoft MVP
Professional Numbskull
This is, by definition, not that.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. com> wrote: The qyestion was asked in a geneneral way, so I have a less targeted approach, which show the underling process, rather than a specific situation and a simplified soltution for that situation.
Quite often, the solutin is indeed simle, but wihtout a specific enough set of rules to establish the specific need, I am left to drop to more elelementary explanation of the underling technology, from which I hop the OP to glean the spedific implementation.
While I see your point, I would say that probably 99% of the time people *don't* need to use a Decoder - and that using a Decoder explicitly will make their code more complicated.
I can't remember the last time I needed to use Decoder rather than just calling Encoding.GetStr ing - I suspect it may have been when I was writing my own version of BinaryReader, which is far from a normal situation :)
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 something like:
char chars = "Hello!";
byte bytes = (byte) chars;
|
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 bytes that are
not printable characters? I've tested this and it seems to work fine, but
I want to make sure there isn't some condition or situation...
|
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net
library, we found that the char (delimiters) specified above are replaced
with different char.
|
by: iyuen |
last post by:
I'm having problems with converting a byte array to an image object~
My byte array is an picture in VB6 StdPicture format. I've used propertybag
to convert the picture into base64Array format in XML, and embedded
the array as some child element in an xml file, i.e.:
...
|
by: Marius Cabas |
last post by:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file from that string, the continment is altered from it's original
state. Below is a code snipped I'm using:
// Here I read the wave file and I convert...
| |
by: dlarock |
last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I
think) with the conversion from the Byte MD5 hash back to string.
Watching this through the debugger it appears as if the MD5 is
computing the right Byte for the hash when compared to other MD5 hash
generators online. However, when I attempt to convert it back to tring...
|
by: David |
last post by:
I note that you can null teminate a string by adding controlchar.null.
Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.
I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).
At present I read through each Byte and convert...
|
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, here's the code:
======================================
Dim textConverter As New ASCIIEncoding
Dim sParam As String = "This is my cool param"
Dim...
|
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 learning all help would be
greatly apperciated.
Attribute VB_Name = "Module1"
Option Explicit
|
by: ogtheterror |
last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working.
Is there anyone that knows how to get this into a .net assembly?
_________________________________________________________
def encrypt_password(challenge, password):
if challenge == 'md5':
...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |