Yes well byte swapping is not exactly rocket science. I can probably get my cat to understand it (no shit).
Really though, I've started using pointers in C# lately, and I've descovered other useful feature.
Just being insane really, using a bazooka to swat a fly.
You see, it's easier to test out features of a language by writing a small program that uses it, rather than starting a large project*.
Also,
Code: Select all
unsafe static int getSize(object type) {
if (null == type) throw new NullReferenceException(); // Fail fast, fail early
object[] types = new object[] { (byte)0, (ushort)0, (uint)0, (ulong)0 }; // The types that are supported
int index = 0; // This is used to index an array of sizes, corresponding to the object[] array above
for (int i = 0; !type.GetType().IsInstanceOfType(types[index = i++]); ) // Loop until the type is verified
if (types.Length == i)
throw new Exception("getSize(object) ERROR! UNSUPPORTED TYPE ''''"
+ type.GetType() + "''''. Stop"); // Fast fast, fail early
return (int)(new int[] { 1, sizeof(ushort), sizeof(uint), sizeof(ulong) }[index]); } // return the size
Code: Select all
unsafe static string chex(object type, bool literal) {
if (null == type) throw new NullReferenceException(); // fast fast, fail early
int size = getSize(type);
ulong x = Convert.ToUInt64(type);
byte* ptr = getStart((byte*)&x, size, sizeof(ulong), ref literal);
string r = "", hex = "0123456789ABCDEF"; // The strings used for the hex conversion
for (int i = 0; i < size; i++) {
r += hex[(*ptr) >> 4] + "" + hex[(*ptr) & 0xF];
ptr = literal ? ++ptr : --ptr; } // if literal, read forwards,
// otherwise read backwards (little endian only)
return (string)r; }
Code: Select all
unsafe static byte* getStart(byte* ptr, int objSize, int containerSize, ref bool literal) {
if (null == ptr) throw new NullReferenceException();
return ptr + (isLittleEndian() ? // Point to the first byte in x
(literal ? 0 : objSize - 1) : // for little endian machines
(literal = true) ? containerSize - objSize : 0); } // for big endian machines
Code: Select all
unsafe static byte* getStart(byte* ptr, int objSize, int containerSize, ref bool literal) {
if (null == ptr) throw new NullReferenceException();
return ptr + (isLittleEndian() ? // Point to the first byte in x
(literal ? 0 : objSize - 1) : // for little endian machines
(literal = true) ? containerSize - objSize : 0); } // for big endian machines
Code: Select all
unsafe static bool isNegative(object type) {
if (null == type) throw new NullReferenceException();
ulong x = Convert.ToUInt64(type);
bool literal = false;
byte* ptr = getStart((byte*)&x, getSize(type), sizeof(ulong), ref literal);
*ptr >>= 7;
return *(bool*)ptr; }
Code: Select all
unsafe static bool isZero(object type) // check if an unsigned integer of any size is zero, the insane batshit way
{
if(null == type) throw new NullReferenceException();
bool literal = true;
int size = getSize(type);
ulong x = Convert.ToUInt64(type);
byte* index = getStart((byte*)&x, size, sizeof(ulong), ref literal);
byte* ptr = index + 1;
for(int i = 1; i < size; i++)
*index |= *ptr++;
return !*(bool*)index;
}
(note: most of these are not related to byte-swapping, I just felt like posting them)
*like my emulator for example, which is the first ever program I wrote in C#, and is actually something I started writing in order to get use to the language, which I started writing in Decemember. More info will be available in June, when I leave college. It emulates a theoretical 8-bit CPU of my design, features a debugger, memory editor and assembler, and it is for educational use as a teaching aide in order to teach students assembly programming concepts. It is my project for the course I'm taking, but the software is mine, written by me, and only me, from scratch, though I can't release it until I complete the course because until then it is "owned" by the college, so to speak.
Yes, I am insane, how did you know? I *choose*, willingly, to write a fucking Virtual Machine for an A-Level computing course.