3

Possible Duplicate:
Equivalent of getBytes() in Java to C#

In Java:

String s="abcde"; 
byte bar[] = s.getBytes();

In C#:

for (int i = 0; i < s.Length; i++)
{
    bar[i] = Convert.ToByte(s.Substring(i, 1)); 
}

I worked Java example getBytes to convert C# code. But the above C# code does not work.

Community
  • 1
  • 1
Fatih Türkeri
  • 89
  • 1
  • 1
  • 9

1 Answers1

5

Encoding.GetBytes

http://msdn.microsoft.com/en-us/library/system.text.encoding.getbytes.aspx

byte[] bytes = Encoding.ASCII.GetBytes("Sweet!");
Brian
  • 1,164
  • 1
  • 9
  • 27