ROSE  0.11.96.11
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 
10 namespace Rose {
11 namespace StringUtility {
12 
14 // Functions for converting numbers to strings
16 
22 ROSE_UTIL_API std::string numberToString(long long);
23 ROSE_UTIL_API std::string numberToString(unsigned long long);
24 ROSE_UTIL_API std::string numberToString(long);
25 ROSE_UTIL_API std::string numberToString(unsigned long);
26 ROSE_UTIL_API std::string numberToString(int);
27 ROSE_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
34 ROSE_UTIL_API std::string numberToString(__int128 x);
35 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
36 #endif
37 
41 ROSE_UTIL_API std::string numberToString(const void*);
42 
46 ROSE_UTIL_API std::string numberToString(double);
47 
49 ROSE_UTIL_API std::string intToHex(uint64_t);
50 
60 ROSE_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);
63 ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
64 ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
65 
66 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
67 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
68 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
76 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
77 
82 ROSE_UTIL_API std::string addrToString(const Sawyer::Container::Interval<uint64_t> &interval, size_t nbits = 0);
83 
89  size_t nbits = 0);
90 
94 ROSE_UTIL_API std::string addrToString(const Sawyer::Optional<uint64_t>&, size_t nbits = 0);
95 
97 template<typename Unsigned>
98 std::string toBinary(Unsigned value, size_t nBits = 0, size_t groupSize = 4, const std::string groupSeparator = "_") {
99  if (0 == nBits)
100  nBits = BitOps::nBits(value);
101  std::string retval;
102  for (size_t i = nBits; i > 0; --i) {
103  retval += BitOps::bit(value, i-1) ? '1' : '0';
104  if (groupSize > 0 && i > 1 && (i-1) % groupSize == 0)
105  retval += groupSeparator;
106  }
107  return retval;
108 }
109 
110 } // namespace
111 } // namespace
112 
113 #endif
Sawyer::Optional
Holds a value or nothing.
Definition: Optional.h:49
Sawyer::Container::IntervalSet
A container holding a set of values.
Definition: IntervalSet.h:55
Rose::StringUtility::numberToString
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
Rose::StringUtility::toBinary
std::string toBinary(Unsigned value, size_t nBits=0, size_t groupSize=4, const std::string groupSeparator="_")
Convert a number to a binary string.
Definition: NumberToString.h:98
Rose::StringUtility::toHex
std::string toHex(T value)
Convert a number to a hexadecimal and decimal string.
Definition: NumberToString.h:66
Rose::BitOps::bit
bool bit(Unsigned src, size_t i)
Extract a single bit.
Definition: BitOps.h:172
Rose::StringUtility::unsignedToHex2
ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
Rose::BitOps::nBits
size_t nBits(Unsigned x=Unsigned(0))
Number of bits in a type or value.
Definition: BitOps.h:17
Rose::StringUtility::signedToHex
std::string signedToHex(T value)
Convert a number to a hexadecimal and decimal string.
Definition: NumberToString.h:67
Rose::StringUtility::unsignedToHex
std::string unsignedToHex(T value)
Convert a number to a hexadecimal and decimal string.
Definition: NumberToString.h:68
Sawyer::Container::Interval
Range of values delimited by endpoints.
Definition: Interval.h:33
Rose
Main namespace for the ROSE library.
Definition: BinaryTutorial.dox:3
Rose::StringUtility::addrToString
ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits=0)
Convert a virtual address to a string.
Rose::StringUtility::signedToHex2
ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
Rose::StringUtility::intToHex
ROSE_UTIL_API std::string intToHex(uint64_t)
Convert an integer to a hexadecimal string.
Rose::StringUtility::toHex2
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.