How do I convert a string
to a byte
array in .NET (C#)?
Update: Also please explain why encoding should be taken into consideration. Can't I simply get what bytes the string has been stored in? Why this dependency on encoding?!!!
Contrary to the answers here, you DON'T need to worry about encoding!
Like you mentioned, your goal is, simply, to "get what bytes the string has been stored in".
(And, of course, to be able to re-construct the string from the bytes.)
For those goals, I honestly do not understand why people keep telling you that you need the encodings. You certainly do NOT need to worry about encodings for this.
Just do this instead:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
As long as your program (or other programs) don't try to interpret the bytes somehow, which you obviously didn't mention you intend to do, then there is nothing wrong with this approach! Worrying about encodings just makes your life more complicated for no real reason.
Additional benefit to this approach:
It doesn't matter if the string contains invalid characters, because you can still get the data and reconstruct the original string anyway!
It will be encoded and decoded just the same, because you are just looking at the bytes.
If you used a specific encoding, though, it would've given you trouble with encoding/decoding invalid characters.
It depends on the encoding of your string (ASCII, UTF8, ...).
e.g.:
byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);
byte[] b2 = System.Text.Encoding.ASCII.GetBytes (myString);
Update: A small sample why encoding matters:
string pi = "\u03a0";
byte[] ascii = System.Text.Encoding.ASCII.GetBytes (pi);
byte[] utf8 = System.Text.Encoding.UTF8.GetBytes (pi);
Console.WriteLine (ascii.Length); //will print 1
Console.WriteLine (utf8.Length); //will print 2
Console.WriteLine (System.Text.Encoding.ASCII.GetString (ascii)); //will print '?'
ASCII simply isn't equipped to deal with special characters.
Internally, the .NET framework uses UTF16 to represent strings, so if you simply want to get the exact bytes that .NET uses, use System.Text.Encoding.Unicode.GetBytes (...)
.
See msdn for more information.
BinaryFormatter bf = new BinaryFormatter();
byte[] bytes;
MemoryStream ms = new MemoryStream();
string orig = "喂 Hello 谢谢 Thank You";
bf.Serialize(ms, orig);
ms.Seek(0, 0);
bytes = ms.ToArray();
MessageBox.Show("Original bytes Length: " + bytes.Length.ToString());
MessageBox.Show("Original string Length: " + orig.Length.ToString());
for (int i = 0; i < bytes.Length; ++i) bytes[i] ^= 168; // pseudo encrypt
for (int i = 0; i < bytes.Length; ++i) bytes[i] ^= 168; // pseudo decrypt
BinaryFormatter bfx = new BinaryFormatter();
MemoryStream msx = new MemoryStream();
msx.Write(bytes, 0, bytes.Length);
msx.Seek(0, 0);
string sx = (string)bfx.Deserialize(msx);
MessageBox.Show("Still intact :" + sx);
MessageBox.Show("Deserialize string Length(still intact): "
+ sx.Length.ToString());
BinaryFormatter bfy = new BinaryFormatter();
MemoryStream msy = new MemoryStream();
bfy.Serialize(msy, sx);
msy.Seek(0, 0);
byte[] bytesy = msy.ToArray();
MessageBox.Show("Deserialize bytes Length(still intact): "
+ bytesy.Length.ToString());
You need to take the encoding into account, because 1 character could be represented by 1 or more bytes (up to about 6), and different encodings will treat these bytes differently.
Joel has a posting on this:
The accepted answer is very, very complicated. Use the included .NET classes for this:
const string data = "A string with international characters: Norwegian: ÆØÅæøå, Chinese: 喂 谢谢";
var bytes = System.Text.Encoding.UTF8.GetBytes(data);
var decoded = System.Text.Encoding.UTF8.GetString(bytes);
Don't reinvent the wheel if you don't have too...
Just to demonstrate that Mehrdrad's sound answer works, his approach can even persist the unpaired surrogate characters(of which many had leveled against my answer, but of which everyone are equally guilty of, e.g. System.Text.Encoding.UTF8.GetBytes
, System.Text.Encoding.Unicode.GetBytes
; those encoding methods can't persist the high surrogate characters d800
for example, and those just merely replace high surrogate characters with value fffd
) :
using System;
class Program
{
static void Main(string[] args)
{
string t = "爱虫";
string s = "Test\ud800Test";
byte[] dumpToBytes = GetBytes(s);
string getItBack = GetString(dumpToBytes);
foreach (char item in getItBack)
{
Console.WriteLine("{0} {1}", item, ((ushort)item).ToString("x"));
}
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}
Output:
T 54
e 65
s 73
t 74
? d800
T 54
e 65
s 73
t 74
Try that with System.Text.Encoding.UTF8.GetBytes or System.Text.Encoding.Unicode.GetBytes, they will merely replace high surrogate characters with value fffd
Every time there's a movement in this question, I'm still thinking of a serializer(be it from Microsoft or from 3rd party component) that can persist strings even it contains unpaired surrogate characters; I google this every now and then: serialization unpaired surrogate character .NET. This doesn't make me lose any sleep, but it's kind of annoying when every now and then there's somebody commenting on my answer that it's flawed, yet their answers are equally flawed when it comes to unpaired surrogate characters.
Darn, Microsoft should have just used System.Buffer.BlockCopy
in its BinaryFormatter
ツ
谢谢!
The first part of your question (how to get the bytes) was already answered by others: look in the System.Text.Encoding
namespace.
I will address your follow-up question: why do you need to pick an encoding? Why can't you get that from the string class itself?
The answer is that the bytes used internally by the string class don't matter.
If your program is entirely within the .Net world then you don't need to worry about getting byte arrays for strings at all, even if you're sending data across a network. Instead, use .Net Serialization to worry about transmitting the data. You don't worry about the actual bytes any more: the Serialization formatter does it for you.
On the other hand, what if you are sending these bytes somewhere that you can't guarantee will pull in data from a .Net serialized stream? In this case you definitely do need to worry about encoding, because obviously this external system cares. So again, the internal bytes used by the string don't matter: you need to pick an encoding so you can be explicit about this encoding on the receiving end.
I understand that in this case you might prefer to use the actual bytes stored by the string variable in memory where possible, with the idea that it might save some work creating your byte stream. But that's just not important compared to making sure that your output is understood at the other end, and to guarantee that you must be explicit with your encoding. If you really want to match your internal bytes, just use the Unicode
encoding.
Try this, a lot less code. Encoding.UTF8.GetBytes("TEST String");
byte[] strToByteArray(string str)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(str);
}
This is a popular question. It is important to understand what the question author is asking, and that it is different from what is likely the most common need. To discourage misuse of the code where it is not needed, I've answered the later first.
Common Need
Every string has a character set and encoding. When you convert a System.String
object to an array of System.Byte
you still have a character set and encoding. For most usages, you'd know which character set and encoding you need and .NET makes it simple to "copy with conversion." Just choose the appropriate Encoding
class.
// using System.Text;
Encoding.UTF8.GetBytes(".NET String to byte array")
The conversion may need to handle cases where the target character set or encoding doesn't support a character that's in the source. You have some choices: exception, substitution or skipping. The default policy is to substitute a '?'.
// using System.Text;
var text = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes("You win €100"));
// -> "You win ?100"
Clearly, conversions are not necessarily lossless!
Note: For System.String
the source character set is Unicode.
The only confusing thing is that .NET uses the name of a character set for the name of one particular encoding of that character set. Encoding.Unicode
should be called Encoding.UTF16
.
That's it for most usages. If that's what you need, stop reading here. See the fun Joel Spolsky article if you don't understand what an encoding is.
Specific Need
Now, the question author asks, "Every string is stored as an array of bytes, right? Why can't I simply have those bytes?"
He doesn't want any conversion.
From the C# spec:
Character and string processing in C# uses Unicode encoding. The char type represents a UTF-16 code unit, and the string type represents a sequence of UTF-16 code units.
So, we know that if we ask for the null conversion (i.e., from UTF-16 to UTF-16), we'll get the desired result:
Encoding.Unicode.GetBytes(".NET String to byte array")
But to avoid the mention of encodings, we must do it another way. If an intermediate data type is acceptable, there is a conceptual shortcut for this:
".NET String to byte array".ToCharArray()
That doesn't get us the desired datatype but Mehrdad's answer shows how to convert this Char array to a Byte array using BlockCopy. However, this copies the string twice! And, it too explicitly uses encoding-specific code: the datatype System.Char
.
The only way to get to the actual bytes the String is stored in is to use a pointer. The fixed
statement allows taking the address of values. From the C# spec:
[For] an expression of type string, ... the initializer computes the address of the first character in the string.
To do so, the compiler writes code skip over the other parts of the string object with RuntimeHelpers.OffsetToStringData
. So, to get the raw bytes, just create a pointer to the string and copy the number of bytes needed.
// using System.Runtime.InteropServices
unsafe byte[] GetRawBytes(String s)
{
if (s == null) return null;
var codeunitCount = s.Length;
/* We know that String is a sequence of UTF-16 codeunits
and such codeunits are 2 bytes */
var byteCount = codeunitCount * 2;
var bytes = new byte[byteCount];
fixed(void* pRaw = s)
{
Marshal.Copy((IntPtr)pRaw, bytes, 0, byteCount);
}
return bytes;
}
As @CodesInChaos pointed out, the result depends on the endianness of the machine. But the question author is not concerned with that.
C# to convert a string to a byte array:
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
Well, I've read all answers and they were about using encoding or one about serialization that drops unpaired surrogates.
It's bad when the string, for example, comes from SQL Server where it was built from byte array storing, for example password hash. If we drop anything from it, it'll store invalid hash, and if we want to store it in XML, we want to leave it intact (because XML writer drops exception on any unpaired surrogate it finds).
So I myself use base64 encoding of byte arrays in such cases, but hey, on the internet is only one solution to this in c# and it has bug in it and is only one way, so i've fixed the bug and written back procedure, here you are, future googlers:
public static byte[] StringToBytes(string str)
{
byte[] data = new byte[str.Length * 2];
for (int i = 0; i < str.Length; ++i)
{
char ch = str[i];
data[i * 2] = (byte)(ch & 0xFF);
data[i * 2 + 1] = (byte)((ch & 0xFF00) >> 8);
}
return data;
}
public static string StringFromBytes(byte[] arr)
{
char[] ch = new char[arr.Length / 2];
for (int i = 0; i < ch.Length; ++i)
{
ch[i] = (char)((int)arr[i * 2] + (((int)arr[i * 2 + 1]) << 8));
}
return new String(ch);
}
Fastest way
public static byte[] GetBytes(string text)
{
return ASCIIEncoding.UTF8.GetBytes(text);
}
The key issue is that a glyph in a string takes 32 bits (16 bits for a character code) but a byte only has 8 bits to spare. A one-to-one mapping doesn't exist unless you restrict yourself to strings that only contain ASCII characters. System.Text.Encoding has lots of ways to map a string to byte[], you need to pick one that avoids loss of information and that is easy to use by your client when she needs to map the byte[] back to a string.
Utf8 is a popular encoding, it is compact and not lossy.
I'm not sure, but I think the string stores its info as an array of Chars, which is inefficient with bytes. Specifically, the definition of a Char is "Represents a Unicode character".
take this example sample:
String str = "asdf éß";
String str2 = "asdf gh";
EncodingInfo[] info = Encoding.GetEncodings();
foreach (EncodingInfo enc in info)
{
System.Console.WriteLine(enc.Name + " - "
+ enc.GetEncoding().GetByteCount(str)
+ enc.GetEncoding().GetByteCount(str2));
}
Take note that the Unicode answer is 14 bytes in both instances, whereas the UTF-8 answer is only 9 bytes for the first, and only 7 for the second.
So if you just want the bytes used by the string, simply use Encoding.Unicode, but it will be inefficient with storage space.
string text = "string";
byte[] array = System.Text.Encoding.UTF8.GetBytes(text);
the result is:
[0] = 115
[1] = 116
[2] = 114
[3] = 105
[4] = 110
[5] = 103
Also please explain why encoding should be taken into consideration. Can't I simply get what bytes the string has been stored in? Why this dependency on encoding?!!!
Because there is no such thing as "the bytes of the string".
A string (or more generically, a text) is composed of characters: letters, digits, and other symbols. That's all. Computers, however, do not know anything about characters; they can only handle bytes. Therefore, if you want to store or transmit text by using a computer, you need to transform the characters to bytes. How do you do that? Here's where encodings come to the scene.
An encoding is nothing but a convention to translate logical characters to phyisical bytes. The simplest and best known encoding is ASCII, and it is all you need if you write in english. For other languages you will need more complete encodings, being any of the Unicode flavours the safest choice nowadays.
So, in short, trying to "get the bytes of a string without using encodings" is as impossible as "writing a text without using any language".
Here is my unsafe implementation of String to Byte[] conversion:
public static unsafe Byte[] GetBytes(String s)
{
Int32 length = s.Length * sizeof(Char);
Byte[] bytes = new Byte[length];
fixed (Char* pInput = s)
fixed (Byte* pBytes = bytes)
{
Byte* source = (Byte*)pInput;
Byte* destination = pBytes;
if (length >= 16)
{
do
{
*((Int64*)destination) = *((Int64*)source);
*((Int64*)(destination + 8)) = *((Int64*)(source + 8));
source += 16;
destination += 16;
}
while ((length -= 16) >= 16);
}
if (length > 0)
{
if ((length & 8) != 0)
{
*((Int64*)destination) = *((Int64*)source);
source += 8;
destination += 8;
}
if ((length & 4) != 0)
{
*((Int32*)destination) = *((Int32*)source);
source += 4;
destination += 4;
}
if ((length & 2) != 0)
{
*((Int16*)destination) = *((Int16*)source);
source += 2;
destination += 2;
}
if ((length & 1) != 0)
{
++source;
++destination;
destination[0] = source[0];
}
}
}
return bytes;
}
It's way faster than the accepted anwser's one, even if not as elegant as it is. Here are my Stopwatch benchmarks over 10000000 iterations:
[Second String: Length 20]
Buffer.BlockCopy: 746ms
Unsafe: 557ms
[Second String: Length 50]
Buffer.BlockCopy: 861ms
Unsafe: 753ms
[Third String: Length 100]
Buffer.BlockCopy: 1250ms
Unsafe: 1063ms
In order to use it, you have to tick "Allow Unsafe Code" in your project build properties. As per .NET Framework 3.5, this method can also be used as String extension:
public static unsafe class StringExtensions
{
public static Byte[] ToByteArray(this String s)
{
// Method Code
}
}
simple code with LINQ
string s = "abc"
byte[] b = s.Select(e => (byte)e).ToArray();
EDIT : as commented below, it is not a good way.
but you can still use it to understand LINQ with a more appropriate coding :
string s = "abc"
byte[] b = s.Cast<byte>().ToArray();
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// C# to convert a byte array to a string.
byte [] dBytes = ...
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
Two ways:
public static byte[] StrToByteArray(this string s)
{
List<byte> value = new List<byte>();
foreach (char c in s.ToCharArray())
value.Add(c.ToByte());
return value.ToArray();
}
And,
public static byte[] StrToByteArray(this string s)
{
s = s.Replace(" ", string.Empty);
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
I tend to use the bottom one more often than the top, haven't benchmarked them for speed.
bytes[] buffer = UnicodeEncoding.UTF8.GetBytes(string something); //for converting to UTF then get its bytes
bytes[] buffer = ASCIIEncoding.ASCII.GetBytes(string something); //for converting to ascii then get its bytes
Here is the code:
// Input string.
const string input = "Dot Net Perls";
// Invoke GetBytes method.
// ... You can store this array as a field!
byte[] array = Encoding.ASCII.GetBytes(input);
// Loop through contents of the array.
foreach (byte element in array)
{
Console.WriteLine("{0} = {1}", element, (char)element);
}
You can use following code
string s_unicode = "abcéabc";
byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(s_unicode
Here is a class that can be easily converted into a byte array , I wrote this class for use in my chat application, you can modify and use this class as you want.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Network.Messages
{
public class SystemMessage
{
public enum Action
{
None = 0 ,
Request = 1,
RequestConfirmed = 2 ,
RequestDeclined = 3 ,
StatusChanged = 4 ,
RequirePendingMessages = 5 ,
KeepAlive = 6 ,
KeepAliveResponse = 7
}
int place;
private byte[] byteArray;
public byte[] ByteArray
{
get { return byteArray; }
set { byteArray = value; }
}
private int iD;
public int ID
{
get { return iD; }
set { iD = value; }
}
private int receiption_ID;
public int Receiption_ID
{
get { return receiption_ID; }
set { receiption_ID = value; }
}
private Action msg_Action;
public Action Msg_Action
{
get { return msg_Action; }
set { msg_Action = value; }
}
private string msg;
public string Msg
{
get { return msg; }
set { msg = value; }
}
private int msgLength;
private int MsgLength
{
get { return msgLength; }
set { msgLength = value; }
}
public SystemMessage ()
{
}
public SystemMessage (byte[] buffer)
{
place = 0;
ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Receiption_ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Msg_Action = (Action)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
MsgLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Msg = Encoding.ASCII.GetString(buffer, place, MsgLength);
place += MsgLength;
}
public byte[] GetByte()
{
place = 0;
byte[] byteOne;
byte[] byteTwo;
byte[] byteThree;
byte[] byteFour;
byte[] byteFive;
byteOne = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(ID));
place += byteOne.Length;
byteTwo = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Receiption_ID));
place += byteTwo.Length;
byteThree = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)Msg_Action));
place += byteThree.Length;
byteFour = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Msg.Length));
place += byteFour.Length;
byteFive = Encoding.ASCII.GetBytes(Msg);
place += byteFive.Length;
ByteArray = new byte[place];
place = 0;
Buffer.BlockCopy(byteOne, 0, ByteArray, 0, byteOne.Length);
place += byteOne.Length;
Buffer.BlockCopy(byteTwo, 0, ByteArray, place, byteTwo.Length);
place += byteTwo.Length;
Buffer.BlockCopy(byteThree, 0, ByteArray, place, byteThree.Length);
place += byteThree.Length;
Buffer.BlockCopy(byteFour, 0, ByteArray, place, byteFour.Length);
place += byteFour.Length;
Buffer.BlockCopy(byteFive, 0, ByteArray, place, byteFive.Length);
place += byteFive.Length;
return ByteArray;
}
}
}
The below class is a class that I used to get sizes of types
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Network.Messages
{
public class SizesOfBinaryDataTypes
{
public static int int_DataTypeSize
{
get
{
int i = 0;
byte[] checklength = BitConverter.GetBytes(i);
return checklength.Length;
}
}
public static int long_DataTypesize
{
get
{
long i = 0;
byte[] cheklength = BitConverter.GetBytes(i);
return cheklength.Length;
}
}
public static int IPAddress_DataTypeSize
{
get
{
IPAddress ip = IPAddress.Any;
byte[] checklength = ip.GetAddressBytes();
return checklength.Length;
}
}
}
}
OP's question: "How do I convert a string
to a byte
array in .NET (C#)?" [sic]
You can use the following code:
static byte[] ConvertString (string s) {
return new byte[0];
}
As a benefit, encoding does not matter! Oh wait, this is an ecoding... it's just trivial and highly lossy.
How do I convert a string
to a byte
array in .NET (C#)?
Update: Also please explain why encoding should be taken into consideration. Can't I simply get what bytes the string has been stored in? Why this dependency on encoding?!!!
Contrary to the answers here, you DON'T need to worry about encoding!
Like you mentioned, your goal is, simply, to "get what bytes the string has been stored in".
(And, of course, to be able to re-construct the string from the bytes.)
For those goals, I honestly do not understand why people keep telling you that you need the encodings. You certainly do NOT need to worry about encodings for this.
Just do this instead:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
As long as your program (or other programs) don't try to interpret the bytes somehow, which you obviously didn't mention you intend to do, then there is nothing wrong with this approach! Worrying about encodings just makes your life more complicated for no real reason.
Additional benefit to this approach:
It doesn't matter if the string contains invalid characters, because you can still get the data and reconstruct the original string anyway!
It will be encoded and decoded just the same, because you are just looking at the bytes.
If you used a specific encoding, though, it would've given you trouble with encoding/decoding invalid characters.
It depends on the encoding of your string (ASCII, UTF8, ...).
e.g.:
byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);
byte[] b2 = System.Text.Encoding.ASCII.GetBytes (myString);
Update: A small sample why encoding matters:
string pi = "\u03a0";
byte[] ascii = System.Text.Encoding.ASCII.GetBytes (pi);
byte[] utf8 = System.Text.Encoding.UTF8.GetBytes (pi);
Console.WriteLine (ascii.Length); //will print 1
Console.WriteLine (utf8.Length); //will print 2
Console.WriteLine (System.Text.Encoding.ASCII.GetString (ascii)); //will print '?'
ASCII simply isn't equipped to deal with special characters.
Internally, the .NET framework uses UTF16 to represent strings, so if you simply want to get the exact bytes that .NET uses, use System.Text.Encoding.Unicode.GetBytes (...)
.
See msdn for more information.
BinaryFormatter bf = new BinaryFormatter();
byte[] bytes;
MemoryStream ms = new MemoryStream();
string orig = "喂 Hello 谢谢 Thank You";
bf.Serialize(ms, orig);
ms.Seek(0, 0);
bytes = ms.ToArray();
MessageBox.Show("Original bytes Length: " + bytes.Length.ToString());
MessageBox.Show("Original string Length: " + orig.Length.ToString());
for (int i = 0; i < bytes.Length; ++i) bytes[i] ^= 168; // pseudo encrypt
for (int i = 0; i < bytes.Length; ++i) bytes[i] ^= 168; // pseudo decrypt
BinaryFormatter bfx = new BinaryFormatter();
MemoryStream msx = new MemoryStream();
msx.Write(bytes, 0, bytes.Length);
msx.Seek(0, 0);
string sx = (string)bfx.Deserialize(msx);
MessageBox.Show("Still intact :" + sx);
MessageBox.Show("Deserialize string Length(still intact): "
+ sx.Length.ToString());
BinaryFormatter bfy = new BinaryFormatter();
MemoryStream msy = new MemoryStream();
bfy.Serialize(msy, sx);
msy.Seek(0, 0);
byte[] bytesy = msy.ToArray();
MessageBox.Show("Deserialize bytes Length(still intact): "
+ bytesy.Length.ToString());
You need to take the encoding into account, because 1 character could be represented by 1 or more bytes (up to about 6), and different encodings will treat these bytes differently.
Joel has a posting on this:
The accepted answer is very, very complicated. Use the included .NET classes for this:
const string data = "A string with international characters: Norwegian: ÆØÅæøå, Chinese: 喂 谢谢";
var bytes = System.Text.Encoding.UTF8.GetBytes(data);
var decoded = System.Text.Encoding.UTF8.GetString(bytes);
Don't reinvent the wheel if you don't have too...
Just to demonstrate that Mehrdrad's sound answer works, his approach can even persist the unpaired surrogate characters(of which many had leveled against my answer, but of which everyone are equally guilty of, e.g. System.Text.Encoding.UTF8.GetBytes
, System.Text.Encoding.Unicode.GetBytes
; those encoding methods can't persist the high surrogate characters d800
for example, and those just merely replace high surrogate characters with value fffd
) :
using System;
class Program
{
static void Main(string[] args)
{
string t = "爱虫";
string s = "Test\ud800Test";
byte[] dumpToBytes = GetBytes(s);
string getItBack = GetString(dumpToBytes);
foreach (char item in getItBack)
{
Console.WriteLine("{0} {1}", item, ((ushort)item).ToString("x"));
}
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}
Output:
T 54
e 65
s 73
t 74
? d800
T 54
e 65
s 73
t 74
Try that with System.Text.Encoding.UTF8.GetBytes or System.Text.Encoding.Unicode.GetBytes, they will merely replace high surrogate characters with value fffd
Every time there's a movement in this question, I'm still thinking of a serializer(be it from Microsoft or from 3rd party component) that can persist strings even it contains unpaired surrogate characters; I google this every now and then: serialization unpaired surrogate character .NET. This doesn't make me lose any sleep, but it's kind of annoying when every now and then there's somebody commenting on my answer that it's flawed, yet their answers are equally flawed when it comes to unpaired surrogate characters.
Darn, Microsoft should have just used System.Buffer.BlockCopy
in its BinaryFormatter
ツ
谢谢!
The first part of your question (how to get the bytes) was already answered by others: look in the System.Text.Encoding
namespace.
I will address your follow-up question: why do you need to pick an encoding? Why can't you get that from the string class itself?
The answer is that the bytes used internally by the string class don't matter.
If your program is entirely within the .Net world then you don't need to worry about getting byte arrays for strings at all, even if you're sending data across a network. Instead, use .Net Serialization to worry about transmitting the data. You don't worry about the actual bytes any more: the Serialization formatter does it for you.
On the other hand, what if you are sending these bytes somewhere that you can't guarantee will pull in data from a .Net serialized stream? In this case you definitely do need to worry about encoding, because obviously this external system cares. So again, the internal bytes used by the string don't matter: you need to pick an encoding so you can be explicit about this encoding on the receiving end.
I understand that in this case you might prefer to use the actual bytes stored by the string variable in memory where possible, with the idea that it might save some work creating your byte stream. But that's just not important compared to making sure that your output is understood at the other end, and to guarantee that you must be explicit with your encoding. If you really want to match your internal bytes, just use the Unicode
encoding.
Try this, a lot less code. Encoding.UTF8.GetBytes("TEST String");
byte[] strToByteArray(string str)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(str);
}
This is a popular question. It is important to understand what the question author is asking, and that it is different from what is likely the most common need. To discourage misuse of the code where it is not needed, I've answered the later first.
Common Need
Every string has a character set and encoding. When you convert a System.String
object to an array of System.Byte
you still have a character set and encoding. For most usages, you'd know which character set and encoding you need and .NET makes it simple to "copy with conversion." Just choose the appropriate Encoding
class.
// using System.Text;
Encoding.UTF8.GetBytes(".NET String to byte array")
The conversion may need to handle cases where the target character set or encoding doesn't support a character that's in the source. You have some choices: exception, substitution or skipping. The default policy is to substitute a '?'.
// using System.Text;
var text = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes("You win €100"));
// -> "You win ?100"
Clearly, conversions are not necessarily lossless!
Note: For System.String
the source character set is Unicode.
The only confusing thing is that .NET uses the name of a character set for the name of one particular encoding of that character set. Encoding.Unicode
should be called Encoding.UTF16
.
That's it for most usages. If that's what you need, stop reading here. See the fun Joel Spolsky article if you don't understand what an encoding is.
Specific Need
Now, the question author asks, "Every string is stored as an array of bytes, right? Why can't I simply have those bytes?"
He doesn't want any conversion.
From the C# spec:
Character and string processing in C# uses Unicode encoding. The char type represents a UTF-16 code unit, and the string type represents a sequence of UTF-16 code units.
So, we know that if we ask for the null conversion (i.e., from UTF-16 to UTF-16), we'll get the desired result:
Encoding.Unicode.GetBytes(".NET String to byte array")
But to avoid the mention of encodings, we must do it another way. If an intermediate data type is acceptable, there is a conceptual shortcut for this:
".NET String to byte array".ToCharArray()
That doesn't get us the desired datatype but Mehrdad's answer shows how to convert this Char array to a Byte array using BlockCopy. However, this copies the string twice! And, it too explicitly uses encoding-specific code: the datatype System.Char
.
The only way to get to the actual bytes the String is stored in is to use a pointer. The fixed
statement allows taking the address of values. From the C# spec:
[For] an expression of type string, ... the initializer computes the address of the first character in the string.
To do so, the compiler writes code skip over the other parts of the string object with RuntimeHelpers.OffsetToStringData
. So, to get the raw bytes, just create a pointer to the string and copy the number of bytes needed.
// using System.Runtime.InteropServices
unsafe byte[] GetRawBytes(String s)
{
if (s == null) return null;
var codeunitCount = s.Length;
/* We know that String is a sequence of UTF-16 codeunits
and such codeunits are 2 bytes */
var byteCount = codeunitCount * 2;
var bytes = new byte[byteCount];
fixed(void* pRaw = s)
{
Marshal.Copy((IntPtr)pRaw, bytes, 0, byteCount);
}
return bytes;
}
As @CodesInChaos pointed out, the result depends on the endianness of the machine. But the question author is not concerned with that.
C# to convert a string to a byte array:
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
Well, I've read all answers and they were about using encoding or one about serialization that drops unpaired surrogates.
It's bad when the string, for example, comes from SQL Server where it was built from byte array storing, for example password hash. If we drop anything from it, it'll store invalid hash, and if we want to store it in XML, we want to leave it intact (because XML writer drops exception on any unpaired surrogate it finds).
So I myself use base64 encoding of byte arrays in such cases, but hey, on the internet is only one solution to this in c# and it has bug in it and is only one way, so i've fixed the bug and written back procedure, here you are, future googlers:
public static byte[] StringToBytes(string str)
{
byte[] data = new byte[str.Length * 2];
for (int i = 0; i < str.Length; ++i)
{
char ch = str[i];
data[i * 2] = (byte)(ch & 0xFF);
data[i * 2 + 1] = (byte)((ch & 0xFF00) >> 8);
}
return data;
}
public static string StringFromBytes(byte[] arr)
{
char[] ch = new char[arr.Length / 2];
for (int i = 0; i < ch.Length; ++i)
{
ch[i] = (char)((int)arr[i * 2] + (((int)arr[i * 2 + 1]) << 8));
}
return new String(ch);
}
Fastest way
public static byte[] GetBytes(string text)
{
return ASCIIEncoding.UTF8.GetBytes(text);
}
The key issue is that a glyph in a string takes 32 bits (16 bits for a character code) but a byte only has 8 bits to spare. A one-to-one mapping doesn't exist unless you restrict yourself to strings that only contain ASCII characters. System.Text.Encoding has lots of ways to map a string to byte[], you need to pick one that avoids loss of information and that is easy to use by your client when she needs to map the byte[] back to a string.
Utf8 is a popular encoding, it is compact and not lossy.
I'm not sure, but I think the string stores its info as an array of Chars, which is inefficient with bytes. Specifically, the definition of a Char is "Represents a Unicode character".
take this example sample:
String str = "asdf éß";
String str2 = "asdf gh";
EncodingInfo[] info = Encoding.GetEncodings();
foreach (EncodingInfo enc in info)
{
System.Console.WriteLine(enc.Name + " - "
+ enc.GetEncoding().GetByteCount(str)
+ enc.GetEncoding().GetByteCount(str2));
}
Take note that the Unicode answer is 14 bytes in both instances, whereas the UTF-8 answer is only 9 bytes for the first, and only 7 for the second.
So if you just want the bytes used by the string, simply use Encoding.Unicode, but it will be inefficient with storage space.
string text = "string";
byte[] array = System.Text.Encoding.UTF8.GetBytes(text);
the result is:
[0] = 115
[1] = 116
[2] = 114
[3] = 105
[4] = 110
[5] = 103
Also please explain why encoding should be taken into consideration. Can't I simply get what bytes the string has been stored in? Why this dependency on encoding?!!!
Because there is no such thing as "the bytes of the string".
A string (or more generically, a text) is composed of characters: letters, digits, and other symbols. That's all. Computers, however, do not know anything about characters; they can only handle bytes. Therefore, if you want to store or transmit text by using a computer, you need to transform the characters to bytes. How do you do that? Here's where encodings come to the scene.
An encoding is nothing but a convention to translate logical characters to phyisical bytes. The simplest and best known encoding is ASCII, and it is all you need if you write in english. For other languages you will need more complete encodings, being any of the Unicode flavours the safest choice nowadays.
So, in short, trying to "get the bytes of a string without using encodings" is as impossible as "writing a text without using any language".
Here is my unsafe implementation of String to Byte[] conversion:
public static unsafe Byte[] GetBytes(String s)
{
Int32 length = s.Length * sizeof(Char);
Byte[] bytes = new Byte[length];
fixed (Char* pInput = s)
fixed (Byte* pBytes = bytes)
{
Byte* source = (Byte*)pInput;
Byte* destination = pBytes;
if (length >= 16)
{
do
{
*((Int64*)destination) = *((Int64*)source);
*((Int64*)(destination + 8)) = *((Int64*)(source + 8));
source += 16;
destination += 16;
}
while ((length -= 16) >= 16);
}
if (length > 0)
{
if ((length & 8) != 0)
{
*((Int64*)destination) = *((Int64*)source);
source += 8;
destination += 8;
}
if ((length & 4) != 0)
{
*((Int32*)destination) = *((Int32*)source);
source += 4;
destination += 4;
}
if ((length & 2) != 0)
{
*((Int16*)destination) = *((Int16*)source);
source += 2;
destination += 2;
}
if ((length & 1) != 0)
{
++source;
++destination;
destination[0] = source[0];
}
}
}
return bytes;
}
It's way faster than the accepted anwser's one, even if not as elegant as it is. Here are my Stopwatch benchmarks over 10000000 iterations:
[Second String: Length 20]
Buffer.BlockCopy: 746ms
Unsafe: 557ms
[Second String: Length 50]
Buffer.BlockCopy: 861ms
Unsafe: 753ms
[Third String: Length 100]
Buffer.BlockCopy: 1250ms
Unsafe: 1063ms
In order to use it, you have to tick "Allow Unsafe Code" in your project build properties. As per .NET Framework 3.5, this method can also be used as String extension:
public static unsafe class StringExtensions
{
public static Byte[] ToByteArray(this String s)
{
// Method Code
}
}
simple code with LINQ
string s = "abc"
byte[] b = s.Select(e => (byte)e).ToArray();
EDIT : as commented below, it is not a good way.
but you can still use it to understand LINQ with a more appropriate coding :
string s = "abc"
byte[] b = s.Cast<byte>().ToArray();
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// C# to convert a byte array to a string.
byte [] dBytes = ...
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
Two ways:
public static byte[] StrToByteArray(this string s)
{
List<byte> value = new List<byte>();
foreach (char c in s.ToCharArray())
value.Add(c.ToByte());
return value.ToArray();
}
And,
public static byte[] StrToByteArray(this string s)
{
s = s.Replace(" ", string.Empty);
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
I tend to use the bottom one more often than the top, haven't benchmarked them for speed.
bytes[] buffer = UnicodeEncoding.UTF8.GetBytes(string something); //for converting to UTF then get its bytes
bytes[] buffer = ASCIIEncoding.ASCII.GetBytes(string something); //for converting to ascii then get its bytes
Here is the code:
// Input string.
const string input = "Dot Net Perls";
// Invoke GetBytes method.
// ... You can store this array as a field!
byte[] array = Encoding.ASCII.GetBytes(input);
// Loop through contents of the array.
foreach (byte element in array)
{
Console.WriteLine("{0} = {1}", element, (char)element);
}
You can use following code
string s_unicode = "abcéabc";
byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(s_unicode
Here is a class that can be easily converted into a byte array , I wrote this class for use in my chat application, you can modify and use this class as you want.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Network.Messages
{
public class SystemMessage
{
public enum Action
{
None = 0 ,
Request = 1,
RequestConfirmed = 2 ,
RequestDeclined = 3 ,
StatusChanged = 4 ,
RequirePendingMessages = 5 ,
KeepAlive = 6 ,
KeepAliveResponse = 7
}
int place;
private byte[] byteArray;
public byte[] ByteArray
{
get { return byteArray; }
set { byteArray = value; }
}
private int iD;
public int ID
{
get { return iD; }
set { iD = value; }
}
private int receiption_ID;
public int Receiption_ID
{
get { return receiption_ID; }
set { receiption_ID = value; }
}
private Action msg_Action;
public Action Msg_Action
{
get { return msg_Action; }
set { msg_Action = value; }
}
private string msg;
public string Msg
{
get { return msg; }
set { msg = value; }
}
private int msgLength;
private int MsgLength
{
get { return msgLength; }
set { msgLength = value; }
}
public SystemMessage ()
{
}
public SystemMessage (byte[] buffer)
{
place = 0;
ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Receiption_ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Msg_Action = (Action)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
MsgLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, place));
place += SizesOfBinaryDataTypes.int_DataTypeSize;
Msg = Encoding.ASCII.GetString(buffer, place, MsgLength);
place += MsgLength;
}
public byte[] GetByte()
{
place = 0;
byte[] byteOne;
byte[] byteTwo;
byte[] byteThree;
byte[] byteFour;
byte[] byteFive;
byteOne = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(ID));
place += byteOne.Length;
byteTwo = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Receiption_ID));
place += byteTwo.Length;
byteThree = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)Msg_Action));
place += byteThree.Length;
byteFour = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Msg.Length));
place += byteFour.Length;
byteFive = Encoding.ASCII.GetBytes(Msg);
place += byteFive.Length;
ByteArray = new byte[place];
place = 0;
Buffer.BlockCopy(byteOne, 0, ByteArray, 0, byteOne.Length);
place += byteOne.Length;
Buffer.BlockCopy(byteTwo, 0, ByteArray, place, byteTwo.Length);
place += byteTwo.Length;
Buffer.BlockCopy(byteThree, 0, ByteArray, place, byteThree.Length);
place += byteThree.Length;
Buffer.BlockCopy(byteFour, 0, ByteArray, place, byteFour.Length);
place += byteFour.Length;
Buffer.BlockCopy(byteFive, 0, ByteArray, place, byteFive.Length);
place += byteFive.Length;
return ByteArray;
}
}
}
The below class is a class that I used to get sizes of types
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Network.Messages
{
public class SizesOfBinaryDataTypes
{
public static int int_DataTypeSize
{
get
{
int i = 0;
byte[] checklength = BitConverter.GetBytes(i);
return checklength.Length;
}
}
public static int long_DataTypesize
{
get
{
long i = 0;
byte[] cheklength = BitConverter.GetBytes(i);
return cheklength.Length;
}
}
public static int IPAddress_DataTypeSize
{
get
{
IPAddress ip = IPAddress.Any;
byte[] checklength = ip.GetAddressBytes();
return checklength.Length;
}
}
}
}
OP's question: "How do I convert a string
to a byte
array in .NET (C#)?" [sic]
You can use the following code:
static byte[] ConvertString (string s) {
return new byte[0];
}
As a benefit, encoding does not matter! Oh wait, this is an ecoding... it's just trivial and highly lossy.
0 commentaires:
Enregistrer un commentaire