ROSE 0.11.145.192
NumberToString.h
1#ifndef ROSE_StringUtility_NumberToString_H
2#define ROSE_StringUtility_NumberToString_H
3
4#include <Rose/BitOps.h>
5#include <rosedll.h>
6
7#include <Sawyer/IntervalSet.h>
8#include <string>
9
10namespace Rose {
11namespace StringUtility {
12
14// Functions for converting numbers to strings
16
22ROSE_UTIL_API std::string numberToString(long long);
23ROSE_UTIL_API std::string numberToString(unsigned long long);
24ROSE_UTIL_API std::string numberToString(long);
25ROSE_UTIL_API std::string numberToString(unsigned long);
26ROSE_UTIL_API std::string numberToString(int);
27ROSE_UTIL_API std::string numberToString(unsigned int);
28
29#if !defined(_MSC_VER) && \
30 defined(BACKEND_CXX_IS_GNU_COMPILER) && \
31 ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
32 BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
33 __WORDSIZE == 64
34ROSE_UTIL_API std::string numberToString(__int128 x);
35ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
36#endif
37
41ROSE_UTIL_API std::string numberToString(const void*);
42
46ROSE_UTIL_API std::string numberToString(double);
47
49ROSE_UTIL_API std::string intToHex(uint64_t);
50
60ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits,
61 bool show_unsigned_decimal=true, bool show_signed_decimal=true,
62 uint64_t decimal_threshold=256);
63ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
64ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
65
66template<typename T>
67std::string toHex(T value) {
68 return toHex2((uint64_t)value, 8*sizeof(T));
69}
70
71template<typename T>
72std::string signedToHex(T value) {
73 return signedToHex2((uint64_t)value, 8*sizeof(T));
74}
75
76template<typename T>
77std::string unsignedToHex(T value) {
78 return unsignedToHex2((uint64_t)value, 8*sizeof(T));
79}
87ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
88
93ROSE_UTIL_API std::string addrToString(const Sawyer::Container::Interval<uint64_t> &interval, size_t nbits = 0);
94
100 size_t nbits = 0);
101
105ROSE_UTIL_API std::string addrToString(const Sawyer::Optional<uint64_t>&, size_t nbits = 0);
106
108template<typename Unsigned>
109std::string toBinary(Unsigned value, size_t nBits = 0, size_t groupSize = 4, const std::string groupSeparator = "_") {
110 if (0 == nBits)
111 nBits = BitOps::nBits(value);
112 std::string retval;
113 for (size_t i = nBits; i > 0; --i) {
114 retval += BitOps::bit(value, i-1) ? '1' : '0';
115 if (groupSize > 0 && i > 1 && (i-1) % groupSize == 0)
116 retval += groupSeparator;
117 }
118 return retval;
119}
120
121} // namespace
122} // namespace
123
124#endif
A container holding a set of values.
Definition IntervalSet.h:53
Range of values delimited by endpoints.
Definition Interval.h:31
Holds a value or nothing.
Definition Optional.h:56
size_t nBits(Unsigned=Unsigned(0))
Number of bits in a type or value.
Definition BitOps.h:20
bool bit(Unsigned src, size_t i)
Extract a single bit.
Definition BitOps.h:205
ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits=0)
Convert a virtual address to a string.
std::string toBinary(Unsigned value, size_t nBits=0, size_t groupSize=4, const std::string groupSeparator="_")
Convert a number to a binary string.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
std::string signedToHex(T value)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits, bool show_unsigned_decimal=true, bool show_signed_decimal=true, uint64_t decimal_threshold=256)
Convert a number to a hexadecimal and decimal string.
std::string toHex(T value)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string intToHex(uint64_t)
Convert an integer to a hexadecimal string.
ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
std::string unsignedToHex(T value)
Convert a number to a hexadecimal and decimal string.
The ROSE library.