conversion in nice way

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello
i have(had) several problems durring convesrion.
For example:
how can i convert String to byte array ?
(byte b[] = .....)
(i need it for Socket.Send())

Thanx
 
Hi,

You first have to select the encoding of the string, let's say it's ASCII ,
then you can do this ( taken from MSDN ) :

Byte[] bytes;
String chars = "ASCII Encoding Example";

ASCIIEncoding ascii = new ASCIIEncoding();

bytes = ascii.GetBytes(chars );

Console.Write("Encoded bytes: ");
foreach (Byte b in bytes) {
Console.Write("[{0}]", b);
}


Hope this help,
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
You first have to select the encoding of the string, let's say it's ASCII ,
then you can do this ( taken from MSDN ) :

Byte[] bytes;
String chars = "ASCII Encoding Example";

ASCIIEncoding ascii = new ASCIIEncoding();

It's a shame this example is from MSDN - there's really no point in
creating another instance of ASCIIEncoding when there's already an
instance of it available via the Encoding.ASCII property. There's also
no need to declare the byte array first. I would have changed this to:

byte[] bytes = Encoding.ASCII.GetBytes (chars);

which I think is considerably simpler and easier to read...
 
Back
Top