Skip to content

Instantly share code, notes, and snippets.

@CallumCarmicheal
Last active March 25, 2018 18:34
Show Gist options
  • Save CallumCarmicheal/d7d444e5d7706c4c2c354fc086bfb4e3 to your computer and use it in GitHub Desktop.
Save CallumCarmicheal/d7d444e5d7706c4c2c354fc086bfb4e3 to your computer and use it in GitHub Desktop.
Print INT's / HEX Data to the Console, this was a usefull piece of code i written for a VM to print the stack and memory easily.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
static class HowToUse {
public static void Main() {
// Our Data Set
int[] data = new int[20] {
1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20
};
Console.WriteLine("Displaying table with decimal addresses and values!");
SimpleVM.Table.PrintData(
start: 0, // Start of Data to print
end: data.Length, // End of Data to print
startingPad: 3, // How many characters to indent from the left (spaces), MIN = 3
columns: 5, // How many columns per line
addrSize: 2, // How many chars per address (2 = 01, 50 | 5 = 00001, 00050)
colSize: 3, // How many chars per column ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
padSize: 3, // How many spaces inbetween each column
data: data, // Data Set
displayHexAddr: false, // If you want the addresses / indexes to be displayed in hex
displayHexVal: false, // If you want the data to be displayed in hex
hideLeadingZeros: true // Hide any leading 0's
);
Console.WriteLine("Displaying table with decimal addresses and hex values!");
SimpleVM.Table.PrintData(
start: 0, // Start of Data to print
end: data.Length, // End of Data to print
startingPad: 2, // How many characters to indent from the left (spaces), MIN = 3
columns: 5, // How many columns per line
addrSize: 2, // How many chars per address (2 = 01, 50 | 5 = 00001, 00050)
colSize: 3, // How many chars per column ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
padSize: 3, // How many spaces inbetween each column
data: data, // Data Set
displayHexAddr: false, // If you want the addresses / indexes to be displayed in hex
displayHexVal: true, // If you want the data to be displayed in hex
hideLeadingZeros: true // Hide any leading 0's
);
Console.WriteLine("Displaying table with hex addresses and decimal values!");
SimpleVM.Table.PrintData(
start: 0, // Start of Data to print
end: data.Length, // End of Data to print
startingPad: 2, // How many characters to indent from the left (spaces), MIN = 3
columns: 5, // How many columns per line
addrSize: 2, // How many chars per address (2 = 01, 50 | 5 = 00001, 00050)
colSize: 3, // How many chars per column ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
padSize: 3, // How many spaces inbetween each column
data: data, // Data Set
displayHexAddr: true, // If you want the addresses / indexes to be displayed in hex
displayHexVal: false, // If you want the data to be displayed in hex
hideLeadingZeros: true // Hide any leading 0's
);
Console.WriteLine("Displaying table with hex addresses and values!");
SimpleVM.Table.PrintData(
start: 0, // Start of Data to print
end: data.Length, // End of Data to print
startingPad: 2, // How many characters to indent from the left (spaces), MIN = 3
columns: 5, // How many columns per line
addrSize: 2, // How many chars per address (2 = 01, 50 | 5 = 00001, 00050)
colSize: 3, // How many chars per column ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
padSize: 3, // How many spaces inbetween each column
data: data, // Data Set
displayHexAddr: true, // If you want the addresses / indexes to be displayed in hex
displayHexVal: true, // If you want the data to be displayed in hex
hideLeadingZeros: true // Hide any leading 0's
);
Console.WriteLine("\n\nFinished!");
Console.ReadLine();
}
}
/**
* You are Not Expected to Understand This
* - Lions' Commentary on UNIX 6th Edition, with Source Code
*
*
* Created by Callum Carmicheal, You may not remove this header
* <https://gist.github.com/CallumCarmicheal>
* <callumcarmicheal@gmail.com>
*
* Gist: <https://gist.github.com/CallumCarmicheal/d7d444e5d7706c4c2c354fc086bfb4e3>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// A Virtual Machine created by Callum Carmicheal
/// with a programming language to compile to it.
///
/// You are not allowed to remove my name from this header
/// </summary>
namespace SimpleVM {
/// <summary>
/// Print of a table used for quick viewing
/// of memory, stacks or other binary data.
/// </summary>
public static class Table {
/// <summary>
/// Print the offset heading
/// </summary>
/// <param name="lp"></param>
/// <param name="addrSize"></param>
/// <param name="columns"></param>
/// <param name="colSize"></param>
/// <param name="padSize"></param>
/// <param name="displayHex"></param>
static void Data_PrintHeaderOffsetLine(string lp, int addrSize, int columns, int colSize, int padSize, bool displayHex = true) {
string format = displayHex ? "{0:X" + colSize + "}" : "{0:D" + colSize + "}";
Con.Write(lp + "│ ");
for (int x = 0; x < addrSize; x++)
Con.Write("-");
Con.Write(" │ ");
for (int x = 0; x < columns; x++) {
Con.Write(string.Format(format, x));
// Print Padding
if (x != columns - 1) {
for (int z = 0; z < padSize; z++)
Con.Write(" ");
} else {
Con.Write(" │");
}
}
Con.Write("\n");
}
/// <summary>
/// Print Header Line
/// </summary>
/// <param name="lp"></param>
/// <param name="addrSize"></param>
/// <param name="columns"></param>
/// <param name="colSize"></param>
/// <param name="padSize"></param>
static void Data_PrintHeaderPaddedLine(string lp, int addrSize, int columns, int colSize, int padSize) {
Con.Write(lp + "├──");
for (int x = 0; x < addrSize; x++)
Con.Write("─");
Con.Write("──┼─");
Con.Write("─");
for (int x = 0; x < columns; x++) {
for (int y = 0; y < colSize; y++)
Con.Write("─"); // Column Data
if (x != columns - 1) {
for (int z = 0; z < padSize; z++)
Con.Write("─");
} else {
Con.Write("──┤");
}
}
Con.Write("\n");
}
/// <summary>
/// Print Data Element from Table
/// </summary>
/// <param name="lp"></param>
/// <param name="addrSize"></param>
/// <param name="colSize"></param>
/// <param name="padSize"></param>
/// <param name="data"></param>
/// <param name="start"></param>
/// <param name="columns"></param>
/// <param name="displayHexAddr"></param>
/// <param name="displayHexVal"></param>
static void Data_PrintData(string lp, int addrSize, int colSize, int padSize, int[] data, int start, int columns, bool displayHexAddr = true, bool displayHexVal = true) {
string padStr = "";
string emptyData = "";
string formatAddr = displayHexAddr ? "{0:X" + addrSize + "}" : "{0:D" + addrSize + "}";
string formatVal = displayHexVal ? "{0:X" + colSize + "}" : "{0:D" + colSize + "}";
for (int x = 0; x < padSize; x++) padStr += " ";
for (int x = 0; x < colSize; x++) emptyData += " ";
Con.Write(lp + "│ ");
string addrStr = displayHexAddr && start < 0 ? "-" + string.Format(formatAddr, start * -1) : string.Format(formatAddr, start);
if (addrStr.Length > addrSize) {
Console.CursorLeft = Console.CursorLeft - 1;
if (addrStr.StartsWith("-")) {
addrStr = addrStr.Substring(1);
if (addrStr.Length > addrSize) {
addrStr = "@" + addrStr.ReverseSubstringL(0, addrSize);
} else {
addrStr = "-" + addrStr.ReverseSubstringL(0, addrSize);
}
} else {
addrStr = "~" + addrStr.ReverseSubstringL(0, addrSize);
}
}
Con.Write(ConsoleColor.Green, addrStr);
Con.Write(" │ ");
for (int x = start; x < start + columns; x++) {
// Check if we are out of data
if (x == data.Length) {
// We are out of data print the rest as padded
string _str = "";
for (int y = x; y < start + columns; y++) {
_str += emptyData;
if (y != start + columns - 1)
_str += padStr;
else _str += " │";
}
Console.Write(_str);
break;
}
string str = displayHexVal && data[x] < 0 ? "-" + string.Format(formatVal, data[x] * -1) : string.Format(formatVal, data[x]);
if (str.Length > colSize) {
Console.CursorLeft = Console.CursorLeft - 1;
if (str.StartsWith("-")) {
str = str.Substring(1);
if (str.Length > colSize) {
str = "@" + str.ReverseSubstringL(0, colSize);
} else {
str = "-" + str.ReverseSubstringL(0, colSize);
}
} else {
str = "~" + str.ReverseSubstringL(0, colSize);
}
}
if (x != start + columns - 1)
str += padStr;
else str += " │";
Console.Write(str);
}
Con.WriteLine("");
//Program.WriteLine(ConsoleColor.Yellow, $"data[{start}] = {data[start]}, amount = {amount}, end = {start+amount}, size={data.Length}");
}
/// <summary>
/// Print Table
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="startingPad"></param>
/// <param name="columns"></param>
/// <param name="addrSize"></param>
/// <param name="colSize"></param>
/// <param name="padSize"></param>
/// <param name="data"></param>
/// <param name="displayHexAddr"></param>
/// <param name="displayHexVal"></param>
/// <param name="hideLeadingZeros"></param>
public static void PrintData(int start, int end, int startingPad, int columns, int addrSize, int colSize, int padSize, int[] data, bool displayHexAddr = true, bool displayHexVal = true, bool hideLeadingZeros = false) {
if (padSize < 2) padSize = 2;
string lp = "";
for (int x = 0; x < startingPad; x++)
lp += ' ';
Data_PrintHeaderOffsetLine(lp, addrSize, columns, colSize, padSize, displayHexAddr);
Data_PrintHeaderPaddedLine(lp, addrSize, columns, colSize, padSize);
while (end - start > 0) {
Data_PrintData(lp, addrSize, colSize, padSize, data, start, columns, displayHexAddr, displayHexVal);
int t = end - start;
start += columns;
}
}
/// <summary>
/// Print Table using relative length for data size
/// </summary>
/// <param name="start"></param>
/// <param name="length"></param>
/// <param name="startingPad"></param>
/// <param name="columns"></param>
/// <param name="addrSize"></param>
/// <param name="colSize"></param>
/// <param name="padSize"></param>
/// <param name="data"></param>
/// <param name="displayHexAddr"></param>
/// <param name="displayHexVal"></param>
/// <param name="hideLeadingZeros"></param>
public static void PrintDataL(int start, int length, int startingPad, int columns, int addrSize, int colSize, int padSize, int[] data, bool displayHexAddr = true, bool displayHexVal = true, bool hideLeadingZeros = false) =>
PrintData(start, start + length, startingPad, columns, addrSize, colSize, padSize, data, displayHexAddr, displayHexVal, hideLeadingZeros);
}
/// <summary>
/// Console helpers
/// </summary>
class Con {
/// <summary>
/// Write to console with arguments
/// </summary>
/// <param name="format"></param>
/// <param name="arg"></param>
public static void Write(string format, params object[] arg) =>
Console.Write(format, arg);
/// <summary>
/// WriteLine to console with arguments
/// </summary>
/// <param name="format"></param>
/// <param name="arg"></param>
public static void WriteLine(string format, params object[] arg) =>
Console.WriteLine(format, arg);
/// <summary>
/// Write to Console
/// </summary>
/// <param name="fc">Forecolour</param>
/// <param name="format">String Format</param>
/// <param name="arg">Arguments</param>
public static void Write(ConsoleColor fc, string format, params object[] arg) {
var f = Console.ForegroundColor;
Console.ForegroundColor = fc;
Console.Write(format, arg);
Console.ForegroundColor = f;
}
/// <summary>
/// Write to Console
/// </summary>
/// <param name="fc">Forecolour</param>
/// <param name="bc">Backcolour</param>
/// <param name="format">String Format</param>
/// <param name="arg">Arguments</param>
public static void Write(ConsoleColor fc, ConsoleColor bc, string format, params object[] arg) {
var f = Console.ForegroundColor;
var b = Console.ForegroundColor;
Console.ForegroundColor = fc;
Console.BackgroundColor = bc;
Console.Write(format, arg);
Console.ForegroundColor = f;
Console.BackgroundColor = b;
}
/// <summary>
/// WriteLine to Console
/// </summary>
/// <param name="fc">Forecolour</param>
/// <param name="format">String Format</param>
/// <param name="arg">Arguments</param>
public static void WriteLine(ConsoleColor fc, string format, params object[] arg) {
var f = Console.ForegroundColor;
Console.ForegroundColor = fc;
Console.WriteLine(format, arg);
Console.ForegroundColor = f;
}
/// <summary>
/// WriteLine to Console
/// </summary>
/// <param name="fc">Forecolour</param>
/// <param name="bc">Backcolour</param>
/// <param name="format">String Format</param>
/// <param name="arg">Arguments</param>
public static void WriteLine(ConsoleColor fc, ConsoleColor bc, string format, params object[] arg) {
var f = Console.ForegroundColor;
var b = Console.ForegroundColor;
Console.ForegroundColor = fc;
Console.BackgroundColor = bc;
Console.WriteLine(format, arg);
Console.ForegroundColor = f;
Console.BackgroundColor = b;
}
}
}
public static class Extensions {
/// <summary>
/// Reverse substring (relative length)
/// </summary>
/// <param name="str"></param>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string ReverseSubstringL(this string str, int startIndex, int length) =>
ReverseSubstring(str, str.Length - startIndex, length);
/// <summary>
/// Reverse substring
/// </summary>
/// <param name="str"></param>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string ReverseSubstring(this string str, int startIndex, int length) {
char[] cArr = new char[length];
int index = length - 1;
int target = startIndex - length;
for (int x = startIndex - 1; x >= target; x--) {
cArr[index] = str[x];
index--;
}
return new string(cArr);
}
/// <summary>
/// Clamp a number or value which uses IComparable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="val"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T> {
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment