xtd 0.2.0
Loading...
Searching...
No Matches
xtd::bit_converter Class Referencefinal
Inheritance diagram for xtd::bit_converter:
xtd::static_object

Definition

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

Header
#include <xtd/bit_converter>
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 xtd::byte 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 xtd::byte conversion From xtd::byte conversion
bool xtd::bit_converter::get_bytes(bool) xtd::bit_converter::to_boolean(const std::vector< xtd::byte >&, size_t)
char32 xtd::bit_converter::get_bytes(char32) xtd::bit_converter::to_char(const std::vector< xtd::byte >&, size_t)
double xtd::bit_converter::get_bytes(double) - or - double_to_int64_bits(double) xtd::bit_converter::to_double(const std::vector< xtd::byte >&, size_t) - or - int64_bits_to_double(int64)
int16 xtd::bit_converter::get_bytes(int16) xtd::bit_converter::to_int16(const std::vector< xtd::byte >&, size_t)
int32 xtd::bit_converter::get_bytes(int32) xtd::bit_converter::to_int32(const std::vector< xtd::byte >&, size_t)
int64 xtd::bit_converter::get_bytes(int64) xtd::bit_converter::to_int64(const std::vector< xtd::byte >&, size_t)
float xtd::bit_converter::get_bytes(float) - or - single_to_int32_bits(float) xtd::bit_converter::to_single(const std::vector< xtd::byte >&, size_t) - or - int32_bits_to_single(int32)
uint16. xtd::bit_converter::get_bytes(uint16) xtd::bit_converter::to_uint16(const std::vector< xtd::byte >&, size_t)
uint32 xtd::bit_converter::get_bytes(uint32) xtd::bit_converter::to_uint32(const std::vector< xtd::byte >&, size_t)
uint64 xtd::bit_converter::get_bytes(uint64) xtd::bit_converter::to_uint64(const std::vector< xtd::byte >&, size_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/bit_converter>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
namespace bit_converter_round_trips_example {
class program {
public:
// The main entry point for the application.
static auto main() {
auto value = -16;
auto bytes = bit_converter::get_bytes(value);
// Convert bytes back to int.
auto 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 unsigned int.
auto 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_(bit_converter_round_trips_example::program::main);
// This code produces the following output :
//
// -16 = -16: Round-trips
// -16 = 4294967280: Round-trip
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
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(xtd::byte[]) 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 xtd::byte std::vectors that result from passing the integer 1,234,567,890 (0x499602D2) to the get_bytes(int32) method. The bytes are listed in order from the xtd::byte at index 0 to the xtd::byte 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 xtd::byte 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 xtd::byte order (big-endian order). The following example provides an implementation for sending an integer value in network xtd::byte 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 xtd::byte 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/bit_converter>
#include <xtd/literals>
#include <xtd/console>
using namespace xtd;
auto main() -> int {
const auto formatter = "{0,25}{1,30}";
auto a_double = 0.1111111111111111111;
auto a_single = 0.1111111111111111111f;
auto a_long = 1111111111111111111_s64;
auto an_int = 1111111111;
auto a_short = 11111_u16;
auto a_char = '*';
auto 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::string to_string(const std::vector< xtd::byte > &value)
Converts the numeric value of each element of a specified std::vector of bytes to its equivalent hexa...
static std::vector< xtd::byte > get_bytes(bool value) noexcept
Returns the specified Boolean value as an std::vector of bytes.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...

Public Types

enum class  endian {
  little ,
  big
}
 Represents the xtd::byte order ("endianness") in which data is stored in this computer architecture. More...
 

Public Fields

static constexpr bool is_big_endian
 Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
 
static constexpr bool is_little_endian
 Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
 
static constexpr endian endianness
 Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
 

Public Static Methods

static int64 double_to_int64_bits (double value) noexcept
 Converts the specified double-precision floating point number to a 64-bit signed integer.
 
static std::vector< xtd::byteget_bytes (bool value) noexcept
 Returns the specified Boolean value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (char value) noexcept
 Returns the specified Char value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (char16 value) noexcept
 Returns the specified Char value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (char32 value) noexcept
 Returns the specified Char value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (wchar value) noexcept
 Returns the specified Char value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (double value) noexcept
 Returns the specified double value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (sbyte value) noexcept
 Returns the specified xtd::byte value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (int16 value) noexcept
 Returns the specified int16 value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (int32 value) noexcept
 Returns the specified int32 value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (int64 value) noexcept
 Returns the specified int64 value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (float value) noexcept
 Returns the specified single value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (xtd::byte value) noexcept
 Returns the specified xtd::byte value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (uint16 value) noexcept
 Returns the specified uint16 value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (uint32 value) noexcept
 Returns the specified uint32 value as an std::vector of bytes.
 
static std::vector< xtd::byteget_bytes (uint64 value) noexcept
 Returns the specified uint64 value as an std::vector of bytes.
 
static float int32_bits_to_single (int32 value) noexcept
 Converts the specified 32-bit signed integer to a single-precision floating point number.
 
static double int64_bits_to_double (int64 value) noexcept
 Converts the specified 64-bit signed integer to a double-precision floating point number.
 
static int32 single_to_int32_bits (float value) noexcept
 Converts the specified single-precision floating point number to a 32-bit signed integer.
 
static bool to_boolean (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a Boolean value converted from one xtd::byte at a specified position in a xtd::byte std::vector.
 
static char32 to_char (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a char32 converted from two bytes at a specified position in a xtd::byte std::vector.
 
static double to_double (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a double-precision floating point number converted from eight bytes at a specified position in a xtd::byte std::vector.
 
static int16 to_int16 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static int32 to_int32 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static int64 to_int64 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static float to_single (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a single-precision floating point number converted from eight bytes at a specified position in a xtd::byte std::vector.
 
static uint16 to_uint16 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static uint32 to_uint32 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static uint64 to_uint64 (const std::vector< xtd::byte > &value, size_t start_index)
 Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte std::vector.
 
static xtd::string to_string (const std::vector< xtd::byte > &value)
 Converts the numeric value of each element of a specified std::vector of bytes to its equivalent hexadecimal xtd::string representation.
 
static xtd::string to_string (const std::vector< xtd::byte > &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::string representation.
 
static xtd::string to_string (const std::vector< xtd::byte > &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::string representation.
 

Member Enumeration Documentation

◆ endian

enum class xtd::bit_converter::endian
strong

Represents the xtd::byte order ("endianness") in which data is stored in this computer architecture.

Remarks
Different computer architectures store data using different xtd::byte orders. "Big-endian" means the most significant xtd::byte is on the left end of a word. "Little-endian" means the most significant xtd::byte is on the right end of a word.
Enumerator
little 

Represnets "Little-endian" order.

big 

Represnets "Big-endian" order.

Member Function Documentation

◆ double_to_int64_bits()

static int64 xtd::bit_converter::double_to_int64_bits ( double  value)
staticnoexcept

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 values with the double_to_int64_bits method.

◆ get_bytes() [1/15]

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

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [2/15]

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

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [3/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( char16  value)
staticnoexcept

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [4/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( char32  value)
staticnoexcept

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [5/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( wchar  value)
staticnoexcept

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [6/15]

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

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [7/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( sbyte  value)
staticnoexcept

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

Parameters
valueA xtd::byte value.
Returns
An std::vector of bytes with length 1.

◆ get_bytes() [8/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( int16  value)
staticnoexcept

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

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

◆ get_bytes() [9/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( int32  value)
staticnoexcept

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

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

◆ get_bytes() [10/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( int64  value)
staticnoexcept

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

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

◆ get_bytes() [11/15]

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

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 xtd::byte std::vectors with the get_bytes method.

◆ get_bytes() [12/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( xtd::byte  value)
staticnoexcept

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

Parameters
valueA xtd::byte value.
Returns
An std::vector of bytes with length 1.

◆ get_bytes() [13/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( uint16  value)
staticnoexcept

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

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

◆ get_bytes() [14/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( uint32  value)
staticnoexcept

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

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

◆ get_bytes() [15/15]

static std::vector< xtd::byte > xtd::bit_converter::get_bytes ( uint64  value)
staticnoexcept

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

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

◆ int32_bits_to_single()

static float xtd::bit_converter::int32_bits_to_single ( int32  value)
staticnoexcept

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  value)
staticnoexcept

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 xtd::bit_converter::single_to_int32_bits ( float  value)
staticnoexcept

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< xtd::byte > &  value,
size_t  start_index 
)
static

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

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
bool true if the xtd::byte 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 xtd::byte std::vectors to Boolean values with the to_boolean method.

◆ to_char()

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

Returns a char32 converted from two bytes at a specified position in a xtd::byte std::vector.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
A char32 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 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< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a double-precision floating point number converted from eight bytes at a specified position in a xtd::byte 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 xtd::bit_converter::to_int16 ( const std::vector< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte 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 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 xtd::bit_converter::to_int32 ( const std::vector< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte 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 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 xtd::bit_converter::to_int64 ( const std::vector< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte 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 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< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a single-precision floating point number converted from eight bytes at a specified position in a xtd::byte 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_uint16()

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

Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte 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 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 xtd::bit_converter::to_uint32 ( const std::vector< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte 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 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 xtd::bit_converter::to_uint64 ( const std::vector< xtd::byte > &  value,
size_t  start_index 
)
static

Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte 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 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::string xtd::bit_converter::to_string ( const std::vector< xtd::byte > &  value)
static

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

Parameters
valueAn std::vector of bytes.
Returns
xtd::string A xtd::string 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 xtd::byte std::vectors to xtd::string objects with the to_string method.

◆ to_string() [2/3]

static xtd::string xtd::bit_converter::to_string ( const std::vector< xtd::byte > &  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::string representation.

Parameters
valueAn std::vector of bytes.
start_indexThe starting position within value.
Returns
xtd::string A xtd::string 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 xtd::byte std::vectors to xtd::string objects with the to_string method.

◆ to_string() [3/3]

static xtd::string xtd::bit_converter::to_string ( const std::vector< xtd::byte > &  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::string 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::string A xtd::string 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::string.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 xtd::byte std::vectors to xtd::string objects with the to_string method.

Member Data Documentation

◆ is_big_endian

constexpr bool xtd::bit_converter::is_big_endian
staticconstexpr

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

Returns
Returns true if the architecture is big-endian; false if it is little-endian.
Remarks
Different computer architectures store data using different xtd::byte orders. "Big-endian" means the most significant xtd::byte is on the left end of a word. "Little-endian" means the most significant xtd::byte is on the right end of a word.
Examples
The following code example illustrates the use of the is_big_endian field.
#include <xtd/bit_converter>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
namespace bit_converter_is_big_endian_example {
class program {
public:
// The main entry point for the application.
static auto main() {
console::write_line(
"This example of the bit_converter::is_big_endian field "
"generates \nthe following output when run on "
"x86-class computers.\n");
console::write_line("is_big_endian: {}",
bit_converter::is_big_endian);
}
};
}
startup_(bit_converter_is_big_endian_example::program::main);
/*
This example of the bit_converter::is_big_endian field generates
the following output when run on x86-class computers.
is_big_endian: false
*/

◆ is_little_endian

constexpr bool xtd::bit_converter::is_little_endian
staticconstexpr

Indicates the xtd::byte 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 xtd::byte orders. "Big-endian" means the most significant xtd::byte is on the left end of a word. "Little-endian" means the most significant xtd::byte is on the right end of a word.
Examples
The following code example illustrates the use of the is_little_endian field.
#include <xtd/bit_converter>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
namespace bit_converter_is_litte_endian_example {
class program {
public:
// The main entry point for the application.
static auto main() {
console::write_line(
"This example of the bit_converter::is_little_endian field "
"generates \nthe following output when run on "
"x86-class computers.\n");
console::write_line("is_little_endian: {}",
bit_converter::is_little_endian);
}
};
}
startup_(bit_converter_is_litte_endian_example::program::main);
/*
This example of the bit_converter::is_little_endian field generates
the following output when run on x86-class computers.
is_little_endian: true
*/

◆ endianness

constexpr endian xtd::bit_converter::endianness
staticconstexpr

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

Returns
Returns One of xtd::bit_converter::endian values.
Remarks
Different computer architectures store data using different xtd::byte orders. "Big-endian" means the most significant xtd::byte is on the left end of a word. "Little-endian" means the most significant xtd::byte is on the right end of a word.
Examples
The following code example illustrates the use of the endianness field.
#include <xtd/bit_converter>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
namespace bit_converter_endianness_example {
class program {
public:
// The main entry point for the application.
static auto main() {
console::write_line(
"This example of the bit_converter::endianness field "
"generates \nthe following output when run on "
"x86-class computers.\n");
console::write_line("endianness: {}",
bit_converter::endianness);
}
};
}
startup_(bit_converter_endianness_example::program::main);
/*
This example of the bit_converter::endianness field generates
the following output when run on x86-class computers.
endianness: little
*/

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