Skip to content

Instantly share code, notes, and snippets.

@PJensen
Created October 30, 2016 18:44
Show Gist options
  • Save PJensen/8a128410b2ebb97f423f1ef3be0dbcc6 to your computer and use it in GitHub Desktop.
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
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