Created
October 30, 2016 18:44
-
-
Save PJensen/8a128410b2ebb97f423f1ef3be0dbcc6 to your computer and use it in GitHub Desktop.
A small command line tool to convert an input file into a C# byte array
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.IO; | |
using System.Linq; | |
using System.Text; | |
namespace ByteArray | |
{ | |
internal class EntryPoint | |
{ | |
private static readonly string[] helpCmds = { "help", "/?", "--help", "-help", "-h" }; | |
private const string separator = ", "; | |
private const string csStart = "private static readonly byte[] _rawData = new byte[] { "; | |
private const string csEnd = "};"; | |
private const int EXIT_FAILURE = 1; | |
private const int EXIT_SUCCESS = 0; | |
private static int Main(string[] argv) | |
{ | |
if (argv.Length == 0 || argv.Select(s => s.ToLowerInvariant()).Intersect(helpCmds).Any()) | |
{ | |
return ShowHelp(); | |
} | |
if (!File.Exists(argv[0])) | |
{ | |
Console.WriteLine("no input file!"); | |
return EXIT_FAILURE; | |
} | |
var rawData = File.ReadAllBytes(argv[0]); | |
var sb = new StringBuilder(rawData.Length).Append(csStart); | |
foreach (var b in rawData) { sb.Append(b + separator); } | |
Console.WriteLine(sb.ToString().TrimEnd(separator.ToCharArray()) + csEnd); | |
return EXIT_SUCCESS; | |
} | |
private static int ShowHelp() | |
{ | |
Console.WriteLine(""); | |
Console.WriteLine("Usage: ByteArray.exe <filename>"); | |
Console.WriteLine("Author: @jensen_petej"); | |
Console.WriteLine(""); | |
return EXIT_SUCCESS; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment