Skip to content

Instantly share code, notes, and snippets.

@realduke2000
Created May 29, 2013 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realduke2000/5668363 to your computer and use it in GitHub Desktop.
Save realduke2000/5668363 to your computer and use it in GitHub Desktop.
Diff two file per byte. Read file and represent as HEX string. Read Hex string, convert to byte and write to file.
public static void DiffPerByte(string path1, string path2)
{
byte[] file1 = System.IO.File.ReadAllBytes(path1);
byte[] file2 = File.ReadAllBytes(path2);
Console.WriteLine("{0} len: {1}", path1, file1.Length);
Console.WriteLine("{0} len: {1}", path2, file2.Length);
int len = file2.Length < file1.Length ? file2.Length : file1.Length;
for (int i = 0; i < len; i++)
{
if (file1[i] != file2[i])
{
Console.WriteLine("They are diffrence since index: {0}", i);
return;
}
}
Console.WriteLine("same");
}
public static string jpgToByteString(string jpgPath)
{
if (string.IsNullOrEmpty(jpgPath))
{
throw new ArgumentNullException("jpgPath");
}
byte[] bytes = System.IO.File.ReadAllBytes(jpgPath);
string ret = string.Empty;
for (int i = 0; i < bytes.Length; i++)
{
ret = string.Concat(ret, bytes[i].ToString("X2")); // 00~FF
}
return ret;
}
public static void byteStringToJpg(string hexStringFilePath, string jpgPath)
{
if (string.IsNullOrEmpty(jpgPath))
{
throw new ArgumentNullException("jpgPath");
}
if (string.IsNullOrEmpty(hexStringFilePath))
{
throw new ArgumentNullException("hexStringFilePath");
}
if (File.Exists(jpgPath))
{
File.Delete(jpgPath);
}
if (!File.Exists(hexStringFilePath))
{
throw new FileNotFoundException(hexStringFilePath + " not found");
}
char[] buff = new char[2]; // 00~FF
using (FileStream fstream = new FileStream(hexStringFilePath, FileMode.Open))
using(StreamReader reader = new StreamReader(fstream))
using(FileStream jpgStream = new FileStream(jpgPath, FileMode.CreateNew, FileAccess.Write))
{
int index = 0;
int count = 0;
do
{
count = reader.Read(buff, 0, buff.Length);
if (count <= 0)
{
break; // read complete
}
byte b = 0;
if (count > 1)
{
b = byte.Parse(string.Concat<char>(buff), System.Globalization.NumberStyles.HexNumber);
}
else
{
b = byte.Parse(buff[0].ToString(), System.Globalization.NumberStyles.HexNumber);
}
jpgStream.WriteByte(b);
index += count;
} while (count > 0 && !reader.EndOfStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment