Click here to Skip to main content
12,457,226 members (57,449 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
What is the best way to convert char[] to byte[] in C# 2010?

Please note that we do not know in advance the format of a single char. It can either be ASCII or Unicode or UTF-8 or UTF-16.

Is there one universal conversion to overcome this?

And what is the vice versa conversion in this case, say to Unicode?
Posted 23-Aug-12 2:54am
Updated 23-Aug-12 2:58am
v2
Comments
Manfred R. Bihy 23-Aug-12 9:30am
   
What is C# 2010? There is no such thing! .NET has version numbers as in 1.1, 2.0, 3.0, 3.5, 4.0 etc.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

There are more solutions.
The first is:
byte[] byteArray = Encoding.yourEncoding.GetBytes(charArray1);
More information about this method:
http://msdn.microsoft.com/en-us/library/5881c0k9.aspx[^]

The second is:
byte[] byteArray = Convert.FromBase64CharArray(charArray1,0,charArray1.Length);
More information about this method: http://msdn.microsoft.com/en-us/library/system.convert.frombase64chararray.aspx[^]


Hope this helps.
  Permalink  
Comments
Vitaly Tomilov 23-Aug-12 12:32pm
   
For the third time, I have no information about the type of encoding the array has. Anyway, the issue has been resolved. Cheers!
ProgramFOX 23-Aug-12 12:38pm
   
You can try all encodings, and then
you'll see what's the best.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Vitaly Tomilov 23-Aug-12 11:02am
   
That approach works only if you know the type of encoding, which I don't, as I clearly stated. I decided to re-post the problem that I'm having closer to what it actually is: CLOB: char[] serialization
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Turns out I didn't need it after all. I was trying to serialize char[] via a string, for which I could simply call:

Serialization:

- Convert char[] to String using String(char[]) constructor;

De-serialization:

- Call String.ToCharArray();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 23 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100