yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
hex.cc
Go to the documentation of this file.
1#include "hex.h"
2
3#include <string>
4
5#include "absl/strings/str_cat.h"
6#include "absl/strings/str_format.h"
7#include "absl/strings/str_join.h"
8
9namespace yaze {
10namespace util {
11
12namespace {
13
14void HandleHexStringParams(const std::string &hex,
15 const HexStringParams &params) {
16 std::string result = hex;
17 switch (params.prefix) {
19 result = absl::StrCat("$", result);
20 break;
22 result = absl::StrCat("#", result);
23 break;
25 result = absl::StrCat("0x", result);
27 default:
28 break;
29 }
30}
31} // namespace
32
33std::string HexByte(uint8_t byte, HexStringParams params) {
34 std::string result;
35 const static std::string kLowerFormat = "%02x";
36 const static std::string kUpperFormat = "%02X";
37 if (params.uppercase) {
38 result = absl::StrFormat(kUpperFormat.c_str(), byte);
39 } else {
40 result = absl::StrFormat(kLowerFormat.c_str(), byte);
41 }
42 HandleHexStringParams(result, params);
43 return result;
44}
45
46std::string HexWord(uint16_t word, HexStringParams params) {
47 std::string result;
48 const static std::string kLowerFormat = "%04x";
49 const static std::string kUpperFormat = "%04X";
50 if (params.uppercase) {
51 result = absl::StrFormat(kUpperFormat.c_str(), word);
52 } else {
53 result = absl::StrFormat(kLowerFormat.c_str(), word);
54 }
55 HandleHexStringParams(result, params);
56 return result;
57}
58
59std::string HexLong(uint32_t dword, HexStringParams params) {
60 std::string result;
61 const static std::string kLowerFormat = "%06x";
62 const static std::string kUpperFormat = "%06X";
63 if (params.uppercase) {
64 result = absl::StrFormat(kUpperFormat.c_str(), dword);
65 } else {
66 result = absl::StrFormat(kLowerFormat.c_str(), dword);
67 }
68 HandleHexStringParams(result, params);
69 return result;
70}
71
72std::string HexLongLong(uint64_t qword, HexStringParams params) {
73 std::string result;
74 const static std::string kLowerFormat = "%08x";
75 const static std::string kUpperFormat = "%08X";
76 if (params.uppercase) {
77 result = absl::StrFormat(kUpperFormat.c_str(), qword);
78 } else {
79 result = absl::StrFormat(kLowerFormat.c_str(), qword);
80 }
81 HandleHexStringParams(result, params);
82 return result;
83}
84
85} // namespace util
86} // namespace yaze
void HandleHexStringParams(const std::string &hex, const HexStringParams &params)
Definition hex.cc:14
std::string HexWord(uint16_t word, HexStringParams params)
Definition hex.cc:46
std::string HexByte(uint8_t byte, HexStringParams params)
Definition hex.cc:33
std::string HexLongLong(uint64_t qword, HexStringParams params)
Definition hex.cc:72
std::string HexLong(uint32_t dword, HexStringParams params)
Definition hex.cc:59
Main namespace for the application.
Definition controller.cc:18
enum yaze::util::HexStringParams::Prefix prefix