summaryrefslogtreecommitdiff
path: root/src/big_endian.c
blob: 2db862444f41d1301db9b8e9c4a279d33650e6f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "big_endian.h"

void to_big_endian(uint64_t num, unsigned char *result)
{
  *(result+0) = (num >> 56) & 0xff;
  *(result+1) = (num >> 48) & 0xff;
  *(result+2) = (num >> 40) & 0xff;
  *(result+3) = (num >> 32) & 0xff;
  *(result+4) = (num >> 24) & 0xff;
  *(result+5) = (num >> 16) & 0xff;
  *(result+6) = (num >>  8) & 0xff;
  *(result+7) = (num >>  0) & 0xff;
}

uint64_t from_big_endian(unsigned char *num)
{
  uint64_t result = ((uint64_t) (*num) << 56);
  
  result += ((uint64_t) (*(num+1)) << 48);
  result += ((uint64_t) (*(num+2)) << 40);
  result += ((uint64_t) (*(num+3)) << 32);
  result += ((uint64_t) (*(num+4)) << 24);
  result += ((uint64_t) (*(num+5)) << 16);
  result += ((uint64_t) (*(num+6)) <<  8);
  result += ((uint64_t) (*(num+7)) <<  0);

  return result;
}