xtd - Reference Guide  0.1.0
Modern c++17 framework to create console, gui and unit test applications on Windows, macOS and Linux.
xtd::bit_converter Class Referencefinal

Converts base data types to an std::vector of uint8_ts, and an std::vector of uint8_ts to base data types. More...

#include <bit_converter.h>

Inheritance diagram for xtd::bit_converter:

Static Public Member Functions

static int64_t double_to_int64_bits (double value)
 Converts the specified double-precision floating point number to a 64-bit signed integer. More...
 
static std::vector< uint8_t > get_bytes (bool value)
 Returns the specified Boolean value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (char value)
 Returns the specified Char value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (char16_t value)
 Returns the specified Char value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (char32_t value)
 Returns the specified Char value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (wchar_t value)
 Returns the specified Char value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (double value)
 Returns the specified double value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (int16_t value)
 Returns the specified int16_t value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (int32_t value)
 Returns the specified int32_t value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (int64_t value)
 Returns the specified int64_t value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (float value)
 Returns the specified single value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (uint16_t value)
 Returns the specified uint16_t value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (uint32_t value)
 Returns the specified uint32_t value as an std::vector of uint8_ts. More...
 
static std::vector< uint8_t > get_bytes (uint64_t value)
 Returns the specified uint64_t value as an std::vector of uint8_ts. More...
 
static float int32_bits_to_single (int32_t value)
 Converts the specified 32-bit signed integer to a single-precision floating point number. More...
 
static double int64_bits_to_double (int64_t value)
 Converts the specified 64-bit signed integer to a double-precision floating point number. More...
 
static int32_t single_to_int32_bits (float value)
 Converts the specified single-precision floating point number to a 32-bit signed integer. More...
 
static bool to_boolean (const std::vector< uint8_t > &value, size_t start_index)
 Returns a Boolean value converted from one uint8_t at a specified position in a uint8_t std::vector. More...
 
static char32_t to_char (const std::vector< uint8_t > &value, size_t start_index)
 Returns a char32_t converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static double to_double (const std::vector< uint8_t > &value, size_t start_index)
 Returns a double-precision floating point number converted from eight uint8_ts at a specified position in a uint8_t std::vector. More...
 
static int16_t to_int16 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 16-bit signed integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static int32_t to_int32 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 32-bit signed integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static int64_t to_int64 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 64-bit signed integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static float to_single (const std::vector< uint8_t > &value, size_t start_index)
 Returns a single-precision floating point number converted from eight uint8_ts at a specified position in a uint8_t std::vector. More...
 
static std::string to_string (const std::vector< uint8_t > &value)
 Converts the numeric value of each element of a specified std::vector of uint8_ts to its equivalent hexadecimal std::string representation. More...
 
static std::string to_string (const std::vector< uint8_t > &value, size_t start_index)
 Converts the numeric value of each element of a specified substd::vector of uint8_ts to its equivalent hexadecimal std::string representation. More...
 
static std::string to_string (const std::vector< uint8_t > &value, size_t start_index, size_t length)
 Converts the numeric value of each element of a specified substd::vector of uint8_ts to its equivalent hexadecimal std::string representation. More...
 
static uint16_t to_uint16 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 16-bit unsigned integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static uint32_t to_uint32 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 32-bit signed integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 
static uint64_t to_uint64 (const std::vector< uint8_t > &value, size_t start_index)
 Returns a 64-bit unsigned integer converted from two uint8_ts at a specified position in a uint8_t std::vector. More...
 

Static Public Attributes

static const bool is_little_endian
 Indicates the uint8_t order ("endianness") in which data is stored in this computer architecture. More...
 

Detailed Description

Converts base data types to an std::vector of uint8_ts, and an std::vector of uint8_ts to base data types.

Library
xtd.core
Remarks
The bit_converter class helps manipulate value types in their fundamental form, as a series of uint8_ts. A uint8_t is defined as an 8-bit unsigned integer. The bit_converter class includes static methods to convert each of the primitive types to and from an std::vector of uint8_ts, as the following table illustrates.
Type To uint8_t conversion From uint8_t conversion
bool get_bytes(bool) to_boolean(const std::vector< uint8_t >&, int32_t)
char32_t get_bytes(char32_t) to_char(const std::vector< uint8_t >&, int32_t)
double get_bytes(double) - or - double_to_int64_bits(double) to_double(const std::vector< uint8_t >&, int32_t) - or - int64_bits_to_double(int64_t)
int16_t get_bytes(int16_t) to_int16(const std::vector< uint8_t >&, int32_t)
int32_t get_bytes(int32_t) to_int32(const std::vector< uint8_t >&, int32_t)
int64_t get_bytes(int64_t) to_int64(const std::vector< uint8_t >&, int32_t)
float get_bytes(float) - or - single_to_int32_bits(float) to_single(const std::vector< uint8_t >&, int32_t) - or - int32_bits_to_single(int32_t)
uint16_t get_bytes(uint16_t) to_uint16(const std::vector< uint8_t >&, int32_t)
uint32_t get_bytes(uint32_t) to_uint32(const std::vector< uint8_t >&, int32_t)
uint64_t get_bytes(uint64_t) to_uint64(const std::vector< uint8_t >&, int32_t)
If you use bit_converter methods to round-trip data, make sure that the get_bytes overload and the ToType method specify the same type. As the following example illustrates, restoring an std::vector that represents a signed integer by calling the to_uint32 method can result in a value that is different from the original. For more information, see the entry Working with Signed Non-Decimal and Bitwise Values in the BCL Team Blog.
The order of uint8_ts in the std::vector returned by the get_bytes method overloads (as well as the order of bits in the integer returned by the double_to_int64_bits method and the order of hexadecimal strings returned by the to_string(uint8_t[]) method) depends on whether the computer architecture is little-endian or big-endian. Similarly, the order of uint8_ts in the std::vector and returned by the ToIntegerValue methods and the to_char method depends on whether the computer architecture is little-endian or big-endian. The endianness of an architecture is indicated by the is_little_endian property, which returns true on little-endian systems and false on big-endian systems. On little-endian systems, lower-order uint8_ts precede higher-order uint8_ts. On big-endian system, higher-order uint8_ts precede lower-order uint8_ts. The following table illustrates the difference in the uint8_t std::vectors that result from passing the integer 1,234,567,890 (0x499602D2) to the get_bytes(int32_t) method. The uint8_ts are listed in order from the uint8_t at index 0 to the uint8_t at index 3. |------------—|----------—|
Little-endian D2-02-96-49
Big-endian 49-96-02-D2
------------—----------—
Because the return value of some methods depends on system architecture, be careful when transmitting uint8_t data beyond machine boundaries:
  • If all systems sending and receiving data are guaranteed to have the same endianness, nothing has be done to the data.
  • If systems sending and receiving data can have different endianness, always transmit data in a particular order. This means that the order of uint8_ts in the std::vector may have to be reversed either before sending them or after receiving them. A common convention is to transmit data in network uint8_t order (big-endian order). The following example provides an implementation for sending an integer value in network uint8_t order.
  • If systems sending and receiving data can have different endianness and the data to be transmitted consists of signed integers, call the IPAddress.HostToNetworkOrder method to convert the data to network uint8_t order and the IPAddress.NetworkToHostOrder method to convert it to the order required by the recipient.
Example
The following code example illustrates the use of several bit_converter class methods.

The documentation for this class was generated from the following file: