Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- class FileUtils
- {
- #region VARIABLES
- private const int OFFSET_CHECKSUM = 0x12;
- #endregion
- #region METHODS
- public static ushort GetCheckSum(string fileName)
- {
- if (!File.Exists(fileName))
- throw new FileNotFoundException("Invalid fileName");
- return GetCheckSum(File.ReadAllBytes(fileName));
- }
- public static ushort GetCheckSum(byte[] fileData)
- {
- if (fileData.Length < OFFSET_CHECKSUM + 1)
- throw new ArgumentException("Invalid fileData");
- return BitConverter.ToUInt16(fileData, OFFSET_CHECKSUM);
- }
- public static void WriteCheckSum(string sourceFile, string destFile, ushort checkSum)
- {
- if (!File.Exists(sourceFile))
- throw new FileNotFoundException("Invalid fileName");
- WriteCheckSum(File.ReadAllBytes(sourceFile), destFile, checkSum);
- }
- public static void WriteCheckSum(byte[] data, string destFile, ushort checkSum)
- {
- byte[] checkSumData = BitConverter.GetBytes(checkSum);
- checkSumData.CopyTo(data, OFFSET_CHECKSUM);
- File.WriteAllBytes(destFile, data);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement