forked from antoyo/libelfin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_hex.hh
34 lines (30 loc) · 916 Bytes
/
to_hex.hh
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
// Copyright (c) 2013 Austin T. Clements. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
#ifndef _ELFPP_TO_HEX_HH_
#define _ELFPP_TO_HEX_HH_
#include <string>
#include <type_traits>
template<typename T>
std::string
to_hex(T v)
{
static_assert(std::is_integral<T>::value,
"to_hex applied to non-integral type");
if (v == 0)
return std::string("0");
char buf[sizeof(T)*2 + 1];
char *pos = &buf[sizeof(buf)-1];
*pos-- = '\0';
while (v && pos >= buf) {
int digit = v & 0xf;
if (digit < 10)
*pos = '0' + digit;
else
*pos = 'a' + (digit - 10);
pos--;
v >>= 4;
}
return std::string(pos + 1);
}
#endif // _ELFPP_TO_HEX_HH_