By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,168 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,168 IT Pros & Developers. It's quick & easy.

copye char[] to byte[]

P: n/a
How to copy an char[] to a byte[] , where the char[] holds unicode
characters ?
Mar 31 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 :)

"John A Grandy" wrote:
How to copy an char[] to a byte[] , where the char[] holds unicode
characters ?

Mar 31 '06 #2

P: n/a
Use System.Text.Encoding.Unicode.getBytes().

"John A Grandy" <johnagrandy-at-yahoo-dot-com> glsD:%2***************@TK2MSFTNGP10.phx. gbl...
How to copy an char[] to a byte[] , where the char[] holds unicode
characters ?

Mar 31 '06 #3

P: n/a
I don't beleive you can pass a char[] to that method.

"Lau Lei Cheong" wrote:
Use System.Text.Encoding.Unicode.getBytes().

"John A Grandy" <johnagrandy-at-yahoo-dot-com> ¼¶¼g©ó¶l¥ó·s»D:%2***************@TK2MSFT NGP10.phx.gbl...
How to copy an char[] to a byte[] , where the char[] holds unicode
characters ?


Mar 31 '06 #4

P: n/a
Copied from MSDN:
ms-help://MS.MSDNQTR.2005JUL.1033/cpref/html/frlrfSystemTextEncodingClassGetBytesTopic.htm
ms-help://MS.MSDNQTR.2005JUL.1033/cpref/html/frlrfsystemtextencodingclassgetbytestopic3.htm
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(__wchar_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.Encoding.UTF8.GetBytes().

"Spectre" <Sp*****@discussions.microsoft.com> glsD:3B********************************* *@microsoft.com...
I don't beleive you can pass a char[] to that method.

"Lau Lei Cheong" wrote:
Use System.Text.Encoding.Unicode.getBytes().

"John A Grandy" <johnagrandy-at-yahoo-dot-com> ???gco?lDoPs?D:%2***************@TK2MSFTNGP10.ph x.gbl...
> How to copy an char[] to a byte[] , where the char[] holds unicode
> characters ?
>


Mar 31 '06 #5

P: n/a


Spectre wrote:
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 :)


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.Encoding and invoke
GetBytes() on that. UTF8 would be a prime candidate.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 31 '06 #6

P: n/a
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.

"Helge Jensen" <he**********@slog.dk> glsD:OZ**************@tk2msftngp13.phx.g bl...
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.Encoding and invoke
GetBytes() on that. UTF8 would be a prime candidate.

Mar 31 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.