Last active
May 2, 2019 17:40
-
-
Save MitchellCash/e6e83aa347437eb530341341d69cc497 to your computer and use it in GitHub Desktop.
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
#include <stdint.h> | |
#include <string> | |
#include <sstream> | |
#include <iomanip> | |
#include <iostream> | |
#include <vector> | |
inline uint16_t swap_endianness_16(uint16_t x) | |
{ | |
return (x >> 8) | (x << 8); | |
} | |
inline uint32_t swap_endianness_32(uint32_t x) | |
{ | |
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | | |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); | |
} | |
std::string ToHex(uint32_t value, int length) | |
{ | |
std::stringstream strBuffer; | |
if (length == 2){ | |
strBuffer << std::hex << std::setw(length) << std::setfill('0') << value; | |
} else if (length == 4){ | |
uint16_t be_value = (uint16_t)value; | |
uint16_t le_value = swap_endianness_16(be_value); | |
strBuffer << std::hex << std::setw(length) << std::setfill('0') << le_value; | |
} else if (length == 8){ | |
uint32_t le_value = swap_endianness_32(value); | |
strBuffer << std::hex << std::setw(length) << std::setfill('0') << le_value; | |
} | |
return strBuffer.str(); | |
} | |
uint32_t FromChars(unsigned char a, unsigned char b) | |
{ | |
uint32_t n = b; | |
n <<= 8; | |
n += a; | |
return n; | |
} | |
uint32_t FromChars(unsigned char a, unsigned char b, unsigned char c, unsigned char d) | |
{ | |
uint32_t n = d; | |
n <<= 8; | |
n += c; | |
n <<= 8; | |
n += b; | |
n <<= 8; | |
n += a; | |
return n; | |
} | |
const signed char p_util_hexdigit[256] = | |
{ | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, | |
-1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
}; | |
signed char HexDigit(char c) | |
{ | |
return p_util_hexdigit[(unsigned char)c]; | |
} | |
std::vector<unsigned char> ParseHex(const char* psz) | |
{ | |
// convert hex dump to vector | |
std::vector<unsigned char> vch; | |
while (true) { | |
while (isspace(*psz)) | |
psz++; | |
signed char c = HexDigit(*psz++); | |
if (c == (signed char)-1) | |
break; | |
unsigned char n = (c << 4); | |
c = HexDigit(*psz++); | |
if (c == (signed char)-1) | |
break; | |
n |= c; | |
vch.push_back(n); | |
} | |
return vch; | |
} | |
int main() | |
{ | |
// OP CODE produced from Python script. | |
const char* vOpCode = "420105c10100005091000098530000e0920000"; | |
std::vector<unsigned char> v = ParseHex(vOpCode); | |
std::string opCode(v.begin(), v.end()); | |
std::cout << FromChars(opCode[3], opCode[4], opCode[5], opCode[6]) << "\n"; | |
std::cout << FromChars(opCode[7], opCode[8], opCode[9], opCode[10]) << "\n"; | |
std::cout << FromChars(opCode[11], opCode[12], opCode[13], opCode[14]) << "\n"; | |
std::cout << FromChars(opCode[15], opCode[16], opCode[17], opCode[18]) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment