C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the Encoding.ASCII.GetBytes
method to convert an ASCII character to a byte in C#. Here is an example:
char myChar = 'A'; byte[] bytes = Encoding.ASCII.GetBytes(myChar.ToString());
This will convert the ASCII character 'A'
to a byte with the value 65
.
You can use the Encoding.UTF8.GetBytes
method to convert a char array to a UTF-8 byte array in C#. Here is an example:
char[] myChars = { 'H', 'e', 'l', 'l', 'o' }; byte[] bytes = Encoding.UTF8.GetBytes(myChars);
This will convert the char array { 'H', 'e', 'l', 'l', 'o' }
to a UTF-8 byte array.
You can use the MemoryStream
class and the Stream.CopyTo
method to get a byte array from a stream in C#. Here is an example:
Stream myStream = new FileStream("myFile.txt", FileMode.Open); MemoryStream memoryStream = new MemoryStream(); myStream.CopyTo(memoryStream); byte[] bytes = memoryStream.ToArray();
This will read the contents of the file "myFile.txt" into a MemoryStream
, and then get a byte array from the MemoryStream
.
Alternatively, if you already have a MemoryStream
, you can simply use the ToArray
method to get a byte array:
MemoryStream memoryStream = new MemoryStream(); // Write to memoryStream byte[] bytes = memoryStream.ToArray();
This will get a byte array from the MemoryStream
.
C# Byte Data Type:
The byte
data type in C# represents an 8-bit unsigned integer.
byte myByte = 255;
Byte Array in C#:
Declare and initialize a byte array in C#.
byte[] byteArray = new byte[] { 0x01, 0x02, 0x03 };
Convert Byte Array to String in C#:
Convert a byte array to a string in C#.
byte[] byteArray = new byte[] { 72, 101, 108, 108, 111 }; string resultString = Encoding.UTF8.GetString(byteArray);
Binary Operations with Bytes in C#:
Perform binary operations using bytes in C#.
byte result = byte1 | byte2; // OR operation
Read and Write Bytes in C#:
Read and write bytes using various I/O operations.
byte[] buffer = new byte[1024]; FileStream fileStream = new FileStream("example.bin", FileMode.Open); fileStream.Read(buffer, 0, buffer.Length);
BitConverter Class in C#:
Utilize the BitConverter
class to convert between different data types and byte arrays.
int intValue = 12345; byte[] byteArray = BitConverter.GetBytes(intValue);
Byte Order in C#:
Be aware of byte order (endianess) when working with multi-byte data types.
byte[] byteArray = BitConverter.GetBytes(12345); if (BitConverter.IsLittleEndian) { Array.Reverse(byteArray); }
Byte Arithmetic in C#:
Perform arithmetic operations with bytes in C#.
byte result = (byte)(byte1 + byte2); // Addition
Convert Int to Byte Array in C#:
Convert an integer to a byte array in C#.
int intValue = 12345; byte[] byteArray = BitConverter.GetBytes(intValue);
Byte Parsing in C#:
Parse bytes to extract meaningful information.
byte[] data = new byte[] { 0x01, 0x02, 0x03 }; int parsedValue = BitConverter.ToInt32(data, 0);
Hexadecimal to Byte Conversion in C#:
Convert a hexadecimal string to a byte array in C#.
string hexString = "1A2B3C"; byte[] byteArray = Enumerable.Range(0, hexString.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hexString.Substring(x, 2), 16)) .ToArray();
Working with Bytes in File I/O C#:
Read and write bytes in file I/O operations.
byte[] data = File.ReadAllBytes("example.bin"); File.WriteAllBytes("output.bin", data);