aboutsummaryrefslogtreecommitdiff
path: root/02-usart/src/kern/lib.c
blob: 88188cc92938e821d0402988a89753cacc6c142c (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "kern/lib.h"

#define nybble_to_hex(n) \
  ((n) < 10 ? 0x30 + (n) : ('A' + ((n) - 10)))

void hexify(uint32_t v, char* into)
{
  into += 8;

  *(into--) = 0;

  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;

  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *(into--) = nybble_to_hex(v & 0x0F);
  v >>= 4;
  *into = nybble_to_hex(v & 0x0F);
  v >>= 4;
}

void decimalify(int v, char* into)
{
  int c = 0;
  int i;

  if (v == 0) {
    *(into ++) = '0';
    *into = 0;
    return;
  } else {
    while (v > 0) {
      *(into ++) = 0x30 + (v % 10);
      v /= 10;
      ++ c;
    }
  }
  *into = 0;

  into -= c;
  for (i = 0; i < c / 2; ++ i) {
    char tmp = into[i];
    into[i] = into[c - i - 1];
    into[c - i - 1] = tmp;
  }

}