Created
July 10, 2016 19:24
-
-
Save DreamVB/dd2fa9e209111ddc3cda1316a9e15721 to your computer and use it in GitHub Desktop.
Convert Hex To Binary By DreamVB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.IO; | |
namespace bin2hex | |
{ | |
class Program | |
{ | |
private static bool _abort = false; | |
private static void HexToBinary(string lzInput , string lzOutput) | |
{ | |
string Buffer = string.Empty; | |
string sHex = string.Empty; | |
FileInfo fi = null; | |
int I = 0; | |
int bcode = 0; | |
try | |
{ | |
fi = new FileInfo(lzInput); | |
using (StreamReader sr = new StreamReader(File.OpenRead(lzInput))) | |
{ | |
//Read the file. | |
Buffer = sr.ReadToEnd(); | |
//Close file | |
sr.Close(); | |
} | |
//Remove whitespace. | |
Buffer = Buffer.Replace(" ", ""); | |
Buffer = Buffer.Replace("\r", ""); | |
Buffer = Buffer.Replace("\n", ""); | |
Buffer = Buffer.Trim(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
_abort = true; | |
return; | |
} | |
try | |
{ | |
using (StreamWriter sw=new StreamWriter(File.OpenWrite(lzOutput))) | |
{ | |
//Convert and write to file. | |
for (I = 0; I < Buffer.Length;I+=2) | |
{ | |
//output binary value. | |
sHex = Buffer.Substring(I, 2); | |
//Convert hex to byte | |
bcode = int.Parse(sHex, System.Globalization.NumberStyles.HexNumber); | |
//Write char to file. | |
sw.BaseStream.WriteByte((byte)bcode); | |
} | |
//Close file. | |
sw.Close(); | |
} | |
} | |
catch(Exception err) | |
{ | |
Console.WriteLine(err.Message); | |
_abort = true; | |
return; | |
} | |
} | |
static int Main(string[] args) | |
{ | |
if (args.Length != 2) | |
{ | |
Console.WriteLine("Incorrect syntext."); | |
return 0; | |
} | |
//Convert file to binary | |
HexToBinary(args[0], args[1]); | |
//StringBuilder sb = BinaryToHex(args[0]); | |
if (_abort) { return 0; } | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment