Write bytes to file - c#

I have a hexadecimal string (e.g 0CFE9E69271557822FE715A8B3E564BE) and I want to write it to a file as bytes. For example,
Offset 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
00000000 0C FE 9E 69 27 15 57 82 2F E7 15 A8 B3 E5 64 BE .þži'.W‚/ç.¨³åd¾
How can I accomplish this using .NET and C#?

If I understand you correctly, this should do the trick. You'll need add using System.IO at the top of your file if you don't already have it.
public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in process: {0}", ex);
return false;
}
}

The simplest way would be to convert your hexadecimal string to a byte array and use the File.WriteAllBytes method.
Using the StringToByteArray() method from this question, you'd do something like this:
string hexString = "0CFE9E69271557822FE715A8B3E564BE";
File.WriteAllBytes("output.dat", StringToByteArray(hexString));
The StringToByteArray method is included below:
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}

Try this:
private byte[] Hex2Bin(string hex)
{
if ((hex == null) || (hex.Length < 1)) {
return new byte[0];
}
int num = hex.Length / 2;
byte[] buffer = new byte[num];
num *= 2;
for (int i = 0; i < num; i++) {
int num3 = int.Parse(hex.Substring(i, 2), NumberStyles.HexNumber);
buffer[i / 2] = (byte) num3;
i++;
}
return buffer;
}
private string Bin2Hex(byte[] binary)
{
StringBuilder builder = new StringBuilder();
foreach(byte num in binary) {
if (num > 15) {
builder.AppendFormat("{0:X}", num);
} else {
builder.AppendFormat("0{0:X}", num); /////// 大于 15 就多加个 0
}
}
return builder.ToString();
}

You convert the hex string to a byte array.
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
Credit: Jared Par
And then use WriteAllBytes to write to the file system.

This example reads 6 bytes into a byte array and writes it to another byte array. It does an XOR operation with the bytes so that the result written to the file is the same as the original starting values. The file is always 6 bytes in size, since it writes at position 0.
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
byte[] b1 = { 1, 2, 4, 8, 16, 32 };
byte[] b2 = new byte[6];
byte[] b3 = new byte[6];
byte[] b4 = new byte[6];
FileStream f1;
f1 = new FileStream("test.txt", FileMode.Create, FileAccess.Write);
// write the byte array into a new file
f1.Write(b1, 0, 6);
f1.Close();
// read the byte array
f1 = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
f1.Read(b2, 0, 6);
f1.Close();
// make changes to the byte array
for (int i = 1; i < b2.Length; i++)
{
b2[i] = (byte)(b2[i] ^ (byte)10); //xor 10
}
f1 = new FileStream("test.txt", FileMode.Open, FileAccess.Write);
// write the new byte array into the file
f1.Write(b2, 0, 6);
f1.Close();
f1 = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
// read the byte array
f1.Read(b3, 0, 6);
f1.Close();
// make changes to the byte array
for (int i = 1; i < b3.Length; i++)
{
b4[i] = (byte)(b3[i] ^ (byte)10); //xor 10
}
f1 = new FileStream("test.txt", FileMode.Open, FileAccess.Write);
// b4 will have the same values as b1
f1.Write(b4, 0, 6);
f1.Close();
}
}
}

Related

How can I convert a git hash to a byte array with length 20 in C#? [duplicate]

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 5 years ago.
Can we convert a hex string to a byte array using a built-in function in C# or do I have to make a custom method for this?
Here's a nice fun LINQ example.
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
I did some research and found out that byte.Parse is even slower than Convert.ToByte.
The fastest conversion I could come up with uses approximately 15 ticks per byte.
public static byte[] StringToByteArrayFastest(string hex) {
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex) {
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
// also works on .NET Micro Framework where (in SDK4.3) byte.Parse(string) only permits integer formats.
The following code changes the hexadecimal string to a byte array by parsing the string byte-by-byte.
public static byte[] ConvertHexStringToByteArray(string hexString)
{
if (hexString.Length % 2 != 0)
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
}
byte[] data = new byte[hexString.Length / 2];
for (int index = 0; index < data.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
data[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return data;
}
I think this may work.
public static byte[] StrToByteArray(string str)
{
Dictionary<string, byte> hexindex = new Dictionary<string, byte>();
for (int i = 0; i <= 255; i++)
hexindex.Add(i.ToString("X2"), (byte)i);
List<byte> hexres = new List<byte>();
for (int i = 0; i < str.Length; i += 2)
hexres.Add(hexindex[str.Substring(i, 2)]);
return hexres.ToArray();
}

What am I doing wrong when parsing a wav file?

I'm trying to parse a wav file. I'm not sure if there can be multiple data chunks in a wav file, but I originally assumed there was only 1 since the wav file format description I was reading only mentioned there being 1.
But I noticed that the subchunk2size was very small (like 26) when the wav file being parsed was something like 36MB and the sample rate was 44100.
So I tried to parse it assuming there were multiple chunks, but after the 1st chunk, there was no subchunk2id to be found.
To go chunk by chunk, I was using the below code
int chunkSize = System.BitConverter.ToInt32(strm, 40);
int widx = 44; //wav data starts at the 44th byte
//strm is a byte array of the wav file
while(widx < strm.Length)
{
widx += chunkSize;
if(widx < 1000)
{
//log "data" or "100 97 116 97" for the subchunkid
//This is only getting printed the 1st time though. All prints after that are garbage
Debug.Log( strm[widx] + " " + strm[widx+1] + " " + strm[widx+2] + " " + strm[widx+3]);
}
if(widx + 8 < strm.Length)
{
widx += 4;
chunkSize = System.BitConverter.ToInt32(strm, widx);
widx += 4;
}else
{
widx += 8;
}
}
A .wav-File has 3 chunks:
Each chunk has a size of 4 Byte
The first chunk is the "RIFF"-chunk. It includes 8 Byte the filesize(4 Byte) and the name of the format(4byte, usually "WAVE").
The next chunk is the "fmt "-chunk (the space in the chunk-name is important). It includes the audio-format(2 Byte), the number of channels (2 Byte), the sample rate (4 Byte), the byte rate (4 Byte), blockalign (2 Byte) and the bits per sample (2 Byte).
The third and last chunk is the data-chunk. Here are the real data and the amplitudes of the samples. It includes 4 Byte for the datasize, which is the number of bytes for the data.
You can find further explanations of the properties of a .wav-file here.
From this knowledge I have already created the following class:
public sealed class WaveFile
{
//privates
private int fileSize;
private string format;
private int fmtChunkSize;
private int audioFormat;
private int numChannels;
private int sampleRate;
private int byteRate;
private int blockAlign;
private int bitsPerSample;
private int dataSize;
private int[][] data;//One array per channel
//publics
public int FileSize => fileSize;
public string Format => format;
public int FmtChunkSize => fmtChunkSize;
public int AudioFormat => audioFormat;
public int NumChannels => numChannels;
public int SampleRate => sampleRate;
public int ByteRate => byteRate;
public int BitsPerSample => bitsPerSample;
public int DataSize => dataSize;
public int[][] Data => data;
public WaveFile(string path)
{
FileStream fs = File.OpenRead(path);
LoadChunk(fs); //read RIFF Chunk
LoadChunk(fs); //read fmt Chunk
LoadChunk(fs); //read data Chunk
fs.Close();
}
private void LoadChunk(FileStream fs)
{
ASCIIEncoding Encoder = new ASCIIEncoding();
byte[] bChunkID = new byte[4];
fs.Read(bChunkID, 0, 4);
string sChunkID = Encoder.GetString(bChunkID);
byte[] ChunkSize = new byte[4];
fs.Read(ChunkSize, 0, 4);
if (sChunkID.Equals("RIFF"))
{
fileSize = BitConverter.ToInt32(ChunkSize, 0);
byte[] Format = new byte[4];
fs.Read(Format, 0, 4);
this.format = Encoder.GetString(Format);
}
if (sChunkID.Equals("fmt "))
{
fmtChunkSize = BitConverter.ToInt32(ChunkSize, 0);
byte[] audioFormat = new byte[2];
fs.Read(audioFormat, 0, 2);
this.audioFormat = BitConverter.ToInt16(audioFormat, 0);
byte[] numChannels = new byte[2];
fs.Read(numChannels, 0, 2);
this.numChannels = BitConverter.ToInt16(numChannels, 0);
byte[] sampleRate = new byte[4];
fs.Read(sampleRate, 0, 4);
this.sampleRate = BitConverter.ToInt32(sampleRate, 0);
byte[] byteRate = new byte[4];
fs.Read(byteRate, 0, 4);
this.byteRate = BitConverter.ToInt32(byteRate, 0);
byte[] blockAlign = new byte[2];
fs.Read(blockAlign, 0, 2);
this.blockAlign = BitConverter.ToInt16(blockAlign, 0);
byte[] bitsPerSample = new byte[2];
fs.Read(bitsPerSample, 0, 2);
this.bitsPerSample = BitConverter.ToInt16(bitsPerSample, 0);
}
if (sChunkID.Equals("data"))
{
dataSize = BitConverter.ToInt32(ChunkSize, 0);
data = new int[this.numChannels][];
byte[] temp = new byte[dataSize];
for (int i = 0; i < this.numChannels; i++)
{
data[i] = new int[this.dataSize / (numChannels * bitsPerSample / 8)];
}
for (int i = 0; i < data[0].Length; i++)
{
for (int j = 0; j < numChannels; j++)
{
if (fs.Read(temp, 0, blockAlign / numChannels) > 0)
{
if (blockAlign / numChannels == 2)
{ data[j][i] = BitConverter.ToInt32(temp, 0); }
else
{ data[j][i] = BitConverter.ToInt16(temp, 0); }
}
}
}
}
}
}
Needed using-directives:
using System;
using System.IO;
using System.Text;
This class reads all chunks byte per byte and sets the properties. You just have to initialize this class and it will return all properties of your selected wave-file.
In the reference you added I dont see any mention of the chunk size being repeated for each data chunk...
Try something like this:
int chunkSize = System.BitConverter.ToInt32(strm, 40);
int widx = 44; //wav data starts at the 44th byte
//strm is a byte array of the wav file
while(widx < strm.Length)
{
if(widx < 1000)
{
//log "data" or "100 97 116 97" for the subchunkid
//This is only getting printed the 1st time though. All prints after that are garbage
Debug.Log( strm[widx] + " " + strm[widx+1] + " " + strm[widx+2] + " " + strm[widx+3]);
}
widx += chunkSize;
}

How to promote a byte by specific value?

So i have this file that i am opening:
static void Encrypt(string fileName)
{
using (FileStream stream = File.OpenRead(fileName))
{
using (BinaryReader reader = new BinaryReader(stream))
{
for (int i = 0; i < stream.Length; i++)
{
byte b = reader.ReadByte();
byte newByte = (byte(b + 5))
}
}
}
}
And i want to add specific value to each byte in my file and save it.
So just store the new bytes in a collection and save them after reading whole file.
var newBytes = new List<byte>();
...
for (int i = 0; i < stream.Length; i++)
{
byte b = reader.ReadByte();
newBytes.Add(b + 5);
}
...
File.WriteAllBytes(filePath, newBytes.ToArray());
You could do something like this:
byte b = reader.ReadByte();
int newNumber = (int)b + 5;
byte newByte = (byte)(newNumber % 256);
To have control over the overflow you may create, I suggest you change from byte to int.
Then this adds 5 to the byte value you read, wrapping to zero when you reach b == 251, as 251 + 5 == 256 and 256 % 256 == 0.

C# store int in byte array

I work on a small project and i need to store 4 int types in a byte array(which will be sent later on a socket).
This is the code:
int a = 566;
int b = 1106;
int c = 649;
int d = 299;
byte[] bytes = new byte[16];
bytes[0] = (byte)(a >> 24);
bytes[1] = (byte)(a >> 16);
bytes[2] = (byte)(a >> 8);
bytes[3] = (byte)a;
I shifted the bits of the first value,but i'm not sure now how to retrieve it back...doing the reversed process.
I hope my question is clear,if i missed somthing i'll be glad to explain it again.
Thanks.
To extract the Int32 back out from the byte array, use this expression:
int b = bytes[0] << 24
| bytes[1] << 16
| bytes[2] << 8
| bytes[3]; // << 0
Here is a .NET Fiddle that demonstrates.
Depends on you comment reply, you can do it like this:
int a = 10;
byte[] aByte = BitConverter.GetBytes(a);
int b = 20;
byte[] bByte = BitConverter.GetBytes(b);
List<byte> listOfBytes = new List<byte>(aByte);
listOfBytes.AddRange(bByte);
byte[] newByte = listOfBytes.ToArray();
You can use a MemoryStream to wrap an array of bytes, and then use BinaryWriter to write items to the array, and BinaryReader to read items from the array.
Sample code:
int a = 566;
int b = 1106;
int c = 649;
int d = 299;
// Writing.
byte[] data = new byte[sizeof(int) * 4];
using (MemoryStream stream = new MemoryStream(data))
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(a);
writer.Write(b);
writer.Write(c);
writer.Write(d);
}
// Reading.
using (MemoryStream stream = new MemoryStream(data))
using (BinaryReader reader = new BinaryReader(stream))
{
a = reader.ReadInt32();
b = reader.ReadInt32();
c = reader.ReadInt32();
d = reader.ReadInt32();
}
// Check results.
Trace.Assert(a == 566);
Trace.Assert(b == 1106);
Trace.Assert(c == 649);
Trace.Assert(d == 299);

Writing Hard drive serial string to Binary file

I have a simple function that grabs the hard drive serial number from the C:\ drive and puts it into a string:
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");
disk.Get();
string hdStr = Convert.ToString(disk["VolumeSerialNumber"]);
I'm then attempting to convert the string above into ASCII and then write it out to a binary file, the issue I'm having is, when converting this string and saving the file using streamwriter, and opening the file in a hex editor, I'm seeing more bytes that I originally wanted to write so for example "16342D1F4A61BC"
Will come out as: 08 16 34 2d 1f 4a 61 c2 bc
It's adding the 08 and c2 in there somehow...
The more complete version is as follows:
string constructor2 = "16342D1F4A61BC";
string StrValue = "";
while (constructor2.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(constructor2.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
constructor2 = constructor2.Substring(2, constructor2.Length - 2);
}
FileStream writeStream;
try
{
writeStream = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + "\\license.mgr", FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream);
writeBinay.Write(StrValue);
writeBinay.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Can anyone help me understand how these are getting added in?
Try this:
string constructor2 = "16342D1F4A61BC";
File.WriteAllBytes("test.bin", ToBytesFromHexa(constructor2));
With the following helper routines:
public static byte[] ToBytesFromHexa(string text)
{
if (text == null)
throw new ArgumentNullException("text");
List<byte> bytes = new List<byte>();
bool low = false;
byte prev = 0;
for (int i = 0; i < text.Length ; i++)
{
byte b = GetHexaByte(text[i]);
if (b == 0xFF)
continue;
if (low)
{
bytes.Add((byte)(prev * 16 + b));
}
else
{
prev = b;
}
low = !low;
}
return bytes.ToArray();
}
public static byte GetHexaByte(char c)
{
if ((c >= '0') && (c <= '9'))
return (byte)(c - '0');
if ((c >= 'A') && (c <= 'F'))
return (byte)(c - 'A' + 10);
if ((c >= 'a') && (c <= 'f'))
return (byte)(c - 'a' + 10);
return 0xFF;
}
Try using System.Text.Encoding.ASCII.GetBytes(hdStr) to get the bytes that represent the string in ASCII.
How important is endian-ness to you in the file?
Perhaps you can use something like:
byte[] b = BitConverter.GetBytes(Convert.ToUInt32(hdStr, 16));

Categories

Resources