Answered by:
How to set string to content of byte array (no encoding)

Question
-
Hi,
Im working on packet sniffer but I cannot find out how to set string to the exact content of byte array. When debugging, there are "E/// ... " and many ASCII characters, but set to string there is only "E".
I would need to show exactly the same as in byte array.
ThanksThursday, December 11, 2008 12:31 PM
Answers
-
All of the Windows controls and the Visual Studio debugger are written using C/C++. That's a language where a binary zero means something special inside a string: it indicates end-of-string. There are other codes like that, you've probably seen \r and \n before. Long story short, you can't just translate a byte[] to a string. The standard way to display bytes is to use Hex notation. You can get that from the BitConverter class:
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
byte[] binary = new byte[] { 0, 1, 2, 3, 4, 65 };
string hex = BitConverter.ToString(binary);
Console.WriteLine(hex);
Console.ReadLine();
}
}
}
Hans Passant.- Proposed as answer by Michael Sun [MSFT]Microsoft employee, Moderator Monday, December 15, 2008 7:29 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, December 18, 2008 12:50 PM
Friday, December 12, 2008 12:21 PMModerator -
Take a look at this thread. Might make you want to rethink your approach.
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/79fc4fbf-58fe-4e13-8b62-75f667a7f917
Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Proposed as answer by Michael Sun [MSFT]Microsoft employee, Moderator Monday, December 15, 2008 7:29 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, December 18, 2008 12:50 PM
Thursday, December 11, 2008 9:28 PM
All replies
-
I am not sure this is what you want.
byte[] bytes = { 72, 101, 108, 108, 111 }; char[] chars = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) chars[i] = (char)bytes[i]; string str = new string(chars); Thursday, December 11, 2008 1:35 PM -
Unfortunately when I add node to treeView (str), it still shows only "E" :(
GOT IT. If byte is empty, it will mess up the string.- Edited by Blizna Thursday, December 11, 2008 2:02 PM
Thursday, December 11, 2008 1:48 PM -
Take a look at this thread. Might make you want to rethink your approach.
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/79fc4fbf-58fe-4e13-8b62-75f667a7f917
Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."- Proposed as answer by Michael Sun [MSFT]Microsoft employee, Moderator Monday, December 15, 2008 7:29 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, December 18, 2008 12:50 PM
Thursday, December 11, 2008 9:28 PM -
All of the Windows controls and the Visual Studio debugger are written using C/C++. That's a language where a binary zero means something special inside a string: it indicates end-of-string. There are other codes like that, you've probably seen \r and \n before. Long story short, you can't just translate a byte[] to a string. The standard way to display bytes is to use Hex notation. You can get that from the BitConverter class:
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
byte[] binary = new byte[] { 0, 1, 2, 3, 4, 65 };
string hex = BitConverter.ToString(binary);
Console.WriteLine(hex);
Console.ReadLine();
}
}
}
Hans Passant.- Proposed as answer by Michael Sun [MSFT]Microsoft employee, Moderator Monday, December 15, 2008 7:29 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, December 18, 2008 12:50 PM
Friday, December 12, 2008 12:21 PMModerator -
Of course you should not translate a byte[] to a string unless you know that the byte[] represents a string of characters.
Like the method SerialPort.ReadLine(). You use it only when you know that the bytes you will receive on the serial port represent a string of characters.Friday, December 12, 2008 4:17 PM