xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Static Public Attributes | Static Public Member Functions | List of all members
xtd::bit_converter Class Referencefinal

#include <bit_converter.h>

Definition

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

Namespace
xtd
Library
xtd.core
Remarks
The xtd::bit_converter class helps manipulate value types in their fundamental form, as a series of bytes. A byte_t is defined as an 8-bit unsigned integer. The xtd::bit_converter class includes static methods to convert each of the primitive types to and from an std::vector of bytes, as the following table illustrates.
Type To byte_t conversion From byte_t conversion
bool xtd::bit_converter::get_bytes(bool) xtd::bit_converter::to_boolean(const std::vector< byte_t >&, int32_t)
char32_t xtd::bit_converter::get_bytes(char32_t) xtd::bit_converter::to_char(const std::vector< byte_t >&, int32_t)
double xtd::bit_converter::get_bytes(double) - or - double_to_int64_bits(double) xtd::bit_converter::to_double(const std::vector< byte_t >&, int32_t) - or - int64_bits_to_double(int64_t)
int16_t xtd::bit_converter::get_bytes(int16_t) xtd::bit_converter::to_int16(const std::vector< byte_t >&, int32_t)
int32_t xtd::bit_converter::get_bytes(int32_t) xtd::bit_converter::to_int32(const std::vector< byte_t >&, int32_t)
int64_t xtd::bit_converter::get_bytes(int64_t) xtd::bit_converter::to_int64(const std::vector< byte_t >&, int32_t)
float xtd::bit_converter::get_bytes(float) - or - single_to_int32_bits(float) xtd::bit_converter::to_single(const std::vector< byte_t >&, int32_t) - or - int32_bits_to_single(int32_t)
uint16_t xtd::bit_converter::get_bytes(uint16_t) xtd::bit_converter::to_uint16(const std::vector< byte_t >&, int32_t)
uint32_t xtd::bit_converter::get_bytes(uint32_t) xtd::bit_converter::to_uint32(const std::vector< byte_t >&, int32_t)
uint64_t xtd::bit_converter::get_bytes(uint64_t) xtd::bit_converter::to_uint64(const std::vector< byte_t >&, int32_t)
If you use bit_converter methods to round-trip data, make sure that the xtd::bit_converter::get_bytes overload and the to_<type> method specify the same type. As the following example illustrates, restoring an std::vector that represents a signed integer by calling the xtd::bit_converter::to_uint32 method can result in a value that is different from the original.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
namespace examples {
class program {
public:
// The main entry point for the application.
static void main() {
int value = -16;
vector<byte_t> bytes = bit_converter::get_bytes(value);
// Convert bytes back to int32.
int int_value = bit_converter::to_int32(bytes, 0);
console::write_line("{0} = {1}: {2}", value, int_value, value == int_value ? "Round-trips" : "Does not round-trip");
// Convert bytes to uint32.
uint uint_value = bit_converter::to_uint32(bytes, 0);
console::write_line("{0} = {1}: {2}", value, uint_value, static_cast<uint>(value) == uint_value ? "Round-trips" : "Does not round-trip");
}
};
}
startup_(examples::program);
// This code produces the following output:
//
// -16 = -16: Round-trips
// -16 = 4294967280: Round-trip
static std::vector< byte_t > get_bytes(bool value)
Returns the specified Boolean value as an std::vector of bytes.
static int32_t to_int32(const std::vector< byte_t > &value, size_t start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a byte_t std::vec...
static uint32_t to_uint32(const std::vector< byte_t > &value, size_t start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a byte_t std::vec...
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
#define startup_(main_class)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition: startup.h:49
unsigned int uint
Represents a 32-bit unsigned integer.
Definition: types.h:195
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
The order of bytes in the std::vector returned by the xtd::bit_converter::get_bytes method overloads (as well as the order of bits in the integer returned by the xtd::bit_converter:double_to_int64_bits method and the order of hexadecimal strings returned by the to_string(byte_t[]) method) depends on whether the computer architecture is little-endian or big-endian. Similarly, the order of bytes 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 bytes precede higher-order bytes. On big-endian system, higher-order bytes precede lower-order bytes. The following table illustrates the difference in the byte_t std::vectors that result from passing the integer 1,234,567,890 (0x499602D2) to the get_bytes(int32_t) method. The bytes are listed in order from the byte_t at index 0 to the byte_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 byte_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 bytes 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 byte_t order (big-endian order). The following example provides an implementation for sending an integer value in network byte_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 byte_t order and the IPAddress.NetworkToHostOrder method to convert it to the order required by the recipient.
Examples
The following code example illustrates the use of several bit_converter class methods.
#include <xtd/xtd>
using namespace xtd;
int main() {
const ustring formatter = "{0,25}{1,30}";
double a_doubl = 0.1111111111111111111;
float a_singl = 0.1111111111111111111F;
long long a_long = 1111111111111111111;
int an_int = 1111111111;
short a_short = 11111;
char a_char = '*';
bool a_bool = true;
console::write_line("This example of methods of the BitConverter class"
"\ngenerates the following output.\n");
console::write_line(formatter, "argument", "byte array");
console::write_line(formatter, "--------", "----------");
// Convert values to Byte arrays and display them.
}
// This code produces the following output:
//
// This example of methods of the BitConverter class
// generates the following output.
//
// argument byte array
// -------- ----------
// 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F
// 0.1111111 39-8E-E3-3D
// 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F
// 1111111111 C7-35-3A-42
// 11111 67-2B
// * 2A-00-00-00
// True 01
static xtd::ustring to_string(const std::vector< byte_t > &value)
Converts the numeric value of each element of a specified std::vector of bytes to its equivalent hexa...

Inherits xtd::static_object.

Static Public Attributes

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

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

Member Function Documentation

◆ double_to_int64_bits()

static int64_t xtd::bit_converter::double_to_int64_bits ( double  value)
static

Converts the specified double-precision floating point number to a 64-bit signed integer.

Parameters
valueThe number to convert.
Returns
A 64-bit signed integer whose value is equivalent to value.
Remarks
The order of bits in the integer returned by the double_to_int64_bits method depends on whether the computer architecture is little-endian or big-endian.
Examples
The following code example converts the bit patterns of several Double values to int64_t values with the double_to_int64_bits method.

◆ get_bytes() [1/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( bool  value)
static

Returns the specified Boolean value as an std::vector of bytes.

Parameters
valueA Boolean value.
Returns
An std::vector of bytes with length 1.
Examples
The following code example converts the bit patterns of Boolean values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [2/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( char  value)
static

Returns the specified Char value as an std::vector of bytes.

Parameters
valueA Char value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of Char values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [3/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( char16_t  value)
static

Returns the specified Char value as an std::vector of bytes.

Parameters
valueA Char value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of Char values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [4/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( char32_t  value)
static

Returns the specified Char value as an std::vector of bytes.

Parameters
valueA Char value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of Char values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [5/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( double  value)
static

Returns the specified double value as an std::vector of bytes.

Parameters
valueA double value.
Returns
An std::vector of bytes with length 8.
Examples
The following code example converts the bit patterns of double values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [6/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( float  value)
static

Returns the specified single value as an std::vector of bytes.

Parameters
valueA single value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of single values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [7/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( int16_t  value)
static

Returns the specified int16_t value as an std::vector of bytes.

Parameters
valueA int16_t value.
Returns
An std::vector of bytes with length 2.
Examples
The following code example converts the bit patterns of int16_t values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [8/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( int32_t  value)
static

Returns the specified int32_t value as an std::vector of bytes.

Parameters
valueA int32_t value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of int32_t values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [9/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( int64_t  value)
static

Returns the specified int64_t value as an std::vector of bytes.

Parameters
valueA int64_t value.
Returns
An std::vector of bytes with length 8.
Examples
The following code example converts the bit patterns of int64_t values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [10/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( uint16_t  value)
static

Returns the specified uint16_t value as an std::vector of bytes.

Parameters
valueA uint16_t value.
Returns
An std::vector of bytes with length 2.
Examples
The following code example converts the bit patterns of uint16_t values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [11/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( uint32_t  value)
static

Returns the specified uint32_t value as an std::vector of bytes.

Parameters
valueA uint32_t value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of uint32_t values to byte_t std::vectors with the get_bytes method.
;

◆ get_bytes() [12/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( uint64_t  value)
static

Returns the specified uint64_t value as an std::vector of bytes.

Parameters
valueA uint64_t value.
Returns
An std::vector of bytes with length 8.
Examples
The following code example converts the bit patterns of uint64_t values to byte_t std::vectors with the get_bytes method.

◆ get_bytes() [13/13]

static std::vector<byte_t> xtd::bit_converter::get_bytes ( wchar_t  value)
static

Returns the specified Char value as an std::vector of bytes.

Parameters
valueA Char value.
Returns
An std::vector of bytes with length 4.
Examples
The following code example converts the bit patterns of Char values to byte_t std::vectors with the get_bytes method.

◆ int32_bits_to_single()

static float xtd::bit_converter::int32_bits_to_single ( int32_t  value)
static

Converts the specified 32-bit signed integer to a single-precision floating point number.

Parameters
valueThe number to convert.
Returns
A single-precision floating point number whose value is equivalent to value.
Remarks
Typically, value is an integer that is returned by the single_to_int32_bits method.

◆ int64_bits_to_double()

static double xtd::bit_converter::int64_bits_to_double ( int64_t  value)
static

Converts the specified 64-bit signed integer to a double-precision floating point number.

Parameters
valueThe number to convert.
Returns
A double-precision floating point number whose value is equivalent to value.
Remarks
Typically, value is an integer that is returned by the double_to_int64_bits method.

◆ single_to_int32_bits()

static int32_t xtd::bit_converter::single_to_int32_bits ( float  value)
static

Converts the specified single-precision floating point number to a 32-bit signed integer.

Parameters
valueThe number to convert.
Returns
A 32-bit signed integer whose value is equivalent to value.
Remarks
The order of bits in the integer returned by the double_to_int64_bits method depends on whether the computer architecture is little-endian or big-endian.

◆ to_boolean()

static bool xtd::bit_converter::to_boolean ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a Boolean value converted from one byte_t at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
bool true if the byte_t at start_index in value is nonzero; otherwise, false.
Exceptions
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Examples
The following code example converts elements of byte_t std::vectors to Boolean values with the to_boolean method.

◆ to_char()

static char32_t xtd::bit_converter::to_char ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a char32_t converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A char32_t formed by four bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_char method converts the bytes from index start_index to start_index + 3 to an int32_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_double()

static double xtd::bit_converter::to_double ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a double-precision floating point number converted from eight bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A double precision floating point number formed by eight bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_double method converts the bytes from index start_index to start_index + 7 to a Double value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_int16()

static int16_t xtd::bit_converter::to_int16 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 16-bit signed integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 16-bit signed integer formed by two bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_int16 method converts the bytes from index start_index to start_index + 1 to an int16_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_int32()

static int32_t xtd::bit_converter::to_int32 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 32-bit signed integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 32-bit signed integer formed by four bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_int32 method converts the bytes from index start_index to start_index + 3 to an int32_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_int64()

static int64_t xtd::bit_converter::to_int64 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 64-bit signed integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 64-bit signed integer formed by eight bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_int64 method converts the bytes from index start_index to start_index + 7 to an int64_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_single()

static float xtd::bit_converter::to_single ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a single-precision floating point number converted from eight bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A single precision floating point number formed by eight bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_single method converts the bytes from index start_index to start_index + 3 to a Double value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_string() [1/3]

static xtd::ustring xtd::bit_converter::to_string ( const std::vector< byte_t > &  value)
static

Converts the numeric value of each element of a specified std::vector of bytes to its equivalent hexadecimal xtd::ustring representation.

Parameters
valueAn std::vector of bytes.
Returns
xtd::ustring A xtd::ustring of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A-00".
Exceptions
argument_null_exceptionvalue is null.
Remarks
All the elements of value are converted. The order of hexadecimal strings returned by the to_string method depends on whether the computer architecture is little-endian or big-endian.
Examples
The following code example converts byte_t std::vectors to xtd::ustring objects with the to_string method.

◆ to_string() [2/3]

static xtd::ustring xtd::bit_converter::to_string ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Converts the numeric value of each element of a specified sub std::vector of bytes to its equivalent hexadecimal xtd::ustring representation.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
xtd::ustring A xtd::ustring of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A-00".
Exceptions
argument_null_exceptionvalue is null.
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The elements from std::vector position start_index to the end of the std::vector are converted. The order of hexadecimal strings returned by the to_string method depends on whether the computer architecture is little-endian or big-endian.
Examples
The following code example converts byte_t std::vectors to xtd::ustring objects with the to_string method.

◆ to_string() [3/3]

static xtd::ustring xtd::bit_converter::to_string ( const std::vector< byte_t > &  value,
size_t  start_index,
size_t  length 
)
static

Converts the numeric value of each element of a specified sub std::vector of bytes to its equivalent hexadecimal xtd::ustring representation.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
lengthThe number of std::vector elements in value to convert.
Returns
xtd::ustring A xtd::ustring of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A-00".
Exceptions
argument_null_exceptionvalue is null.
argument_out_of_range_exceptionstart_index or length is less than zero. -or- start_index is greater than zero and is greater than or equal to the length of value.
argument_exceptionThe combination of start_index and length does not specify a position within value; that is, the start_index parameter is greater than the length of value minus the length parameter.
Remarks
AThe length elements from std::vector position start_index are converted. If length equals zero, the method returns xtd::ustring.Empty.
The order of hexadecimal strings returned by the to_string method depends on whether the computer architecture is little-endian or big-endian.
Examples
The following code example converts byte_t std::vectors to xtd::ustring objects with the to_string method.

◆ to_uint16()

static uint16_t xtd::bit_converter::to_uint16 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 16-bit unsigned integer formed by two bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_uint16 method converts the bytes from index start_index to start_index + 1 to an Uint16_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_uint32()

static uint32_t xtd::bit_converter::to_uint32 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 32-bit signed integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 32-bit signed integer formed by four bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_uint32 method converts the bytes from index start_index to start_index + 3 to an Uint32_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

◆ to_uint64()

static uint64_t xtd::bit_converter::to_uint64 ( const std::vector< byte_t > &  value,
size_t  start_index 
)
static

Returns a 64-bit unsigned integer converted from two bytes at a specified position in a byte_t std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A 64-bit unsigned integer formed by eight bytes beginning at start_index.
Exceptions
argument_exceptionstart_index equals the length of value minus 1.
argument_null_exceptionvalue is null
argument_out_of_range_exceptionstart_index is less than zero or greater than the length of value minus 1.
Remarks
The to_uint64 method converts the bytes from index start_index to start_index + 7 to an Uint64_t value. The order of bytes in the std::vector must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of the bit_converter class topic.

Member Data Documentation

◆ is_little_endian

const bool xtd::bit_converter::is_little_endian
static

Indicates the byte_t order ("endianness") in which data is stored in this computer architecture.

Returns
Returns true if the architecture is little-endian; false if it is big-endian.
Remarks
Different computer architectures store data using different byte_t orders. "Big-endian" means the most significant byte_t is on the left end of a word. "Little-endian" means the most significant byte_t is on the right end of a word.
Examples
The following code example illustrates the use of the is_little_endian field.

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