-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex.cpp
49 lines (45 loc) · 1.59 KB
/
hex.cpp
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
#include "hex.h"
#include<sstream>
#include<iomanip>
#include<cstdint>
#include<iostream>
/**
* Returns a std::string with exactly 2 hex digits representing the 8 bits of the i argument.
*
* @param i is a uint8_t that contains the 8 bits to be converted.
*
* @return The std::string with exactly 2 hex digits representing the i
* argument
***********************************************************************/
std::string hex8(uint8_t i)
{
std::ostringstream os;
os << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(i);
return os.str();
}
/**
* Returns a std::string with exactly 8 hex digits representing the 32 bits of the i argument.
*
* @param i is a uint32_t that contains the 32 bits to be converted.
*
* @return The std::string with exactly 8 hex digits representing the i
* argument
***********************************************************************/
std::string hex32(uint32_t i)
{
std::ostringstream os;
os << std::hex << std::setfill('0') << std::setw(8) << static_cast<uint32_t>(i);
return os.str();
}
/**
* Returns a std::string beginning with 0x,
* followed by the 8 hex digits representing the 32 bits of the i arg.
*
* @param i is a uint32_t that contains the 32 bits to be converted.
*
* @return The std::string with formatting to represent the i argument
***********************************************************************/
std::string hex0x32(uint32_t i)
{
return std::string("0x")+hex32(i);
}