csharp string to byte array
Code Example - csharp string to byte array
string author = "Mahesh Chand";
// Convert a C# string to a byte array
byte[] bytes = Encoding.ASCII.GetBytes(author);
// Convert a byte array to a C# string.
string str = Encoding.ASCII.GetString(bytes);
convert system.byte a string csharp
string result = System.Text.Encoding.UTF8.GetString(byteArray);
string from byte array csharp
var str = System.Text.Encoding.Default.GetString(result);
convert bytes to string and back csharp
string base64 = Convert.ToBase64String(bytes);
byte[] bytes = Convert.FromBase64String(base64);
csharp store byte array as string
public static void Main()
{
byte[] bytes = Encoding.Default.GetBytes("ABC123");
Console.WriteLine("Byte Array is: " + String.Join(" ", bytes));
string str = Encoding.Default.GetString(bytes);
Console.WriteLine("The String is: " + str);
}
stream to byte array csharp
public static byte[] GetBytes(Stream stream)
{
var bytes = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.ReadAsync(bytes, 0, bytes.Length);
stream.Dispose();
return bytes;
}
c sharp stream to byte array
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
csharp write byte[] to stream
static void Write(Stream s, Byte[] bytes)
{
using (var writer = new BinaryWriter(s))
{
writer.Write(bytes);
}
}
convert memorystream to byte array csharp
Memory stream to byte array
csharp string to byte[]
using System.Text;
public static byte[] encode(string stringToEncode)
{
UTF8Encoding utf8 = new UtF8Encoding();
byte[] bytename = new byte[1024];
bytename = utf8.GetBytes(stringToEncode);
return bytename;
}
csharp byte array to string
var str = System.Text.Encoding.Default.GetString(result);
csharp class to byte array
// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object) binForm.Deserialize(memStream);
return obj;
}
csharp get bytes from string
byte[] bytes = Encoding.ASCII.GetBytes(someString);
String to byte array csharp
string string = "Hello";
byte[] bytes = Encoding.ASCII.GetBytes(string);