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

What is the opposite of BitConverter.ToString(byte[]) ?

If I run the following code:

Byte[] bytes = System.Text.Encoding.ASCII.GetBytes("test");
return BitConverter.ToString(bytes);

I get the string "74-65-73-74" back. My question is, what is the
easiest way to convert the string back to "test"?

Can it be done without breaking the string and using a loop, etc.? I
have tried many methods, but can't find a way without using a loop. It
seems silly that the framework can go one direction but not the other.

Any help please?
Jul 21 '05 #1
4 16188
System.Text.Encoding.ASCII.GetString(bytes);

i think that's what you're looking for.

Chris

"Greg Ennis" <gr*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
If I run the following code:

Byte[] bytes = System.Text.Encoding.ASCII.GetBytes("test");
return BitConverter.ToString(bytes);

I get the string "74-65-73-74" back. My question is, what is the
easiest way to convert the string back to "test"?

Can it be done without breaking the string and using a loop, etc.? I
have tried many methods, but can't find a way without using a loop. It
seems silly that the framework can go one direction but not the other.

Any help please?

Jul 21 '05 #2
Chris <ct******@hotmail.com> wrote:
System.Text.Encoding.ASCII.GetString(bytes);

i think that's what you're looking for.


I don't think so - that certainly doesn't do the reverse of
BitConverter.ToString.

For me, BitConverter.ToString is really for debug purposes, to easily
see the values in a byte array. That's why (I believe) there's no
reverse method.

--
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
System.Text.Encoding.ASCII.GetString(bytes);

i think that's what you're looking for.


Thanks but this is not correct; this will give me a string of length 11
whereas I am looking for the original string of length 4. This is
because GetString() does not *decode* the hex values in the bytes.

So, I am still looking for a 1-step solution...

-Greg

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #4
Greg,

I believe Jon is correct with his statement:
For me, BitConverter.ToString is really for debug purposes, to easily
see the values in a byte array. That's why (I believe) there's no
reverse method.
This will just give you a nice human readable representation of the bytes.

System.Text.Encoding.ASCII.GetBytes(string)
System.Text.Encoding.ASCII.GetString(bytes)
would be complimentary functions dealing with the underlying data.

Just like MyObject.ToString almost certainly would not give you a String from
which you could create an identical copy of your Object.

Gerald

"Greg Ennis" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
System.Text.Encoding.ASCII.GetString(bytes);

i think that's what you're looking for.


Thanks but this is not correct; this will give me a string of length 11
whereas I am looking for the original string of length 4. This is
because GetString() does not *decode* the hex values in the bytes.

So, I am still looking for a 1-step solution...

-Greg

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 21 '05 #5

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

Similar topics

12
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
4
by: CSharpener | last post by:
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C# In JavaScript, it goes like this for an int: javascript:(123).toString(2 or...
2
by: Greg Ennis | last post by:
If I run the following code: Byte bytes = System.Text.Encoding.ASCII.GetBytes("test"); return BitConverter.ToString(bytes); I get the string "74-65-73-74" back. My question is, what is the...
4
by: Ping | last post by:
Hi, All, We can use BitConverter.ToString(byte) to a string, but how to get the byte from a string like "AD-A6-0D-1F"?
13
by: Pohihihi | last post by:
I am getting Guid using new Guid() when I say new Guid().ToString() I get 10856f25-2759-0358-159c-ff7e0759c800 but when I say
2
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
2
by: gizmo | last post by:
Hi, Here's a little hack I put together to try to get to the bottom of a problem I'm having with trying to base64 encode a hash value. The hash value contains character codes 135 and 130...
1
lifeisgreat20009
by: lifeisgreat20009 | last post by:
This is what i am getting on running the program......... C:\PROGRA~1\Java\JDK15~1.0\bin>javac Editor.java Note: Editor.java uses or overrides a deprecated API. Note: Recompile with...
0
by: laszlobarta | last post by:
Hello, For saving 1 hour of your precious time use this function for parsing a text to a byte array: /// <summary> /// Converts the string representation of an array of numbers in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.