FastLED 3.9.15
Loading...
Searching...
No Matches

◆ hex()

fl::string fl::detail::hex ( u64 value,
HexIntWidth width,
bool is_negative,
bool uppercase,
bool pad_to_width )

Internal hex conversion function (implementation in charconv.cpp)

Parameters
valueThe unsigned 64-bit value to convert
widthThe bit width classification of the original type
is_negativeWhether the original value was negative (for signed types)
uppercaseIf true, use uppercase hex digits (A-F), otherwise lowercase (a-f)
pad_to_widthIf true, pad with leading zeros to full type width
Returns
Hexadecimal string representation

Definition at line 12 of file charconv.cpp.hpp.

12 {
13 // Determine target width in hex characters based on integer bit width
14 size_t target_width = 0;
15 switch (width) {
16 case HexIntWidth::Width8: target_width = 2; break; // 8 bits = 2 hex chars
17 case HexIntWidth::Width16: target_width = 4; break; // 16 bits = 4 hex chars
18 case HexIntWidth::Width32: target_width = 8; break; // 32 bits = 8 hex chars
19 case HexIntWidth::Width64: target_width = 16; break; // 64 bits = 16 hex chars
20 }
21
23 const char* digits = uppercase ? "0123456789ABCDEF" : "0123456789abcdef";
24
25 // Convert value to hex string (minimal representation)
26 u64 temp_value = value;
27 if (temp_value == 0) {
28 // Special case: zero should be "0" not empty string
29 result = fl::string("0");
30 } else {
31 while (temp_value > 0) {
32 char ch = digits[temp_value % 16];
33 // Convert char to string since fl::string::append treats char as number
34 char temp_ch_str[2] = {ch, '\0'};
35 fl::string digit_str(temp_ch_str);
36 // Use += since + operator is not defined for fl::string
37 fl::string temp = digit_str;
38 temp += result;
39 result = temp;
40 temp_value /= 16;
41 }
42 }
43
44 // Pad with leading zeros to target width (only if padding is requested)
45 if (pad_to_width) {
46 while (result.size() < target_width) {
47 fl::string zero_str("0");
48 zero_str += result;
49 result = zero_str;
50 }
51 }
52
53 // Add negative sign if needed
54 if (is_negative) {
55 fl::string minus_str("-");
56 minus_str += result;
57 result = minus_str;
58 }
59
60 return result;
61}
constexpr int type_rank< T >::value
u8 width
Definition blur.h:186
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
constexpr int numeric_limits< char >::digits
fl::u64 u64
Definition s16x16x4.h:221

References fl::numeric_limits< char >::digits, fl::type_rank< T >::value, fl::width, Width16, Width32, Width64, and Width8.

Referenced by fl::to_hex().

+ Here is the caller graph for this function: