Skip to content

Instantly share code, notes, and snippets.

@jeffangelion
Created September 22, 2020 07:47
Show Gist options
  • Save jeffangelion/e087a3d1e54ca928a24ec325f44bdc04 to your computer and use it in GitHub Desktop.
Save jeffangelion/e087a3d1e54ca928a24ec325f44bdc04 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <cmath>
/* This function converts char array (little endian) to integer
* For example: {0xCA, 0xFE, 0xBA, 0xBE} -> 0xBEBAFECA -> 3199925962
* I'm a bad programmer, so here is workaround:
* You need to specify length of char array by second function argument
* Kill me :(
*/
int hex_to_dec (const unsigned char input[], const unsigned int count)
{
unsigned int sum;
for (int i = count; i >= 0; i--)
{
unsigned int tmp = (int) input[i] * (int) pow (16, i * 2);
sum += tmp;
}
return sum;
}
int main()
{
const unsigned char address[4] = {0x00, 0xF8, 0x1E, 0x12};
printf("%u\n", hex_to_dec(address, 4));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment