Copied from MSDN:
ms-help://MS.MSDNQTR.2005 JUL.1033/cpref/html/frlrfSystemText EncodingClassGe tBytesTopic.htm
ms-help://MS.MSDNQTR.2005 JUL.1033/cpref/html/frlrfsystemtext encodingclassge tbytestopic3.ht m
Encodes a range of characters from a character array into a byte array.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Overridable Function GetBytes(Char() ,
Integer, Integer) As Byte()
[C#] public virtual byte[] GetBytes(char[], int, int);
[C++] public: virtual unsigned char GetBytes(__wcha r_t __gc[], int, int)
__gc[];
[JScript] public function GetBytes(Char[], int, int) : Byte[];
-- End of Copy --
If there is error, I suspect you really means
System.Text.Enc oding.UTF8.GetB ytes().
Spectre wrote:[color=blue]
> public static byte[] CharToByte(char[] c)
> {
> byte[] b = new byte[c.Length];
> for(int i = 0; i < c.Length; i++)
> {
> b[i] = (byte) c[i];
> }
> return b;
> }
>
> Simple as that :)[/color]
Perhaps OP should have stated that he wated some usable bytes, not just
junk :)
The above code will not convert an array of unicode-char to a
byte-encoding of those same chars in any kind of encoding, unless the
input-character-set is roughly ASCII.
You can select an encoding from System.Text.Enc oding and invoke
GetBytes() on that. UTF8 would be a prime candidate.
Yes. Especially considering what people refered to as Unicode means "the
real Unicode", "UTF-7"(this is less likely), "UTF-8" and "UCS-2", with
different size among them, I think it's better to let the Framework routines
handle them for you.
Comment