xtd 0.2.0
bit_converter.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "core_export.hpp"
6#include "array.hpp"
7#include "enum.hpp"
8#include "read_only_span.hpp"
9#include "static.hpp"
10#include "types.hpp"
11
13namespace xtd {
53 private:
54 static constexpr uint32 __endian_value__ = 0x01020304;
55 static constexpr xtd::byte __endian_query__ = static_cast<const xtd::byte&>(__endian_value__);
56
57 public:
60 enum class endian {
62 little,
64 big,
65 };
66
68
76 static constexpr bool is_big_endian = __endian_query__ == 0x01u;
83 static constexpr bool is_little_endian = __endian_query__ == 0x04u;
90 static constexpr endian endianness = is_little_endian ? endian::little : endian::big;
92
94
103 static int64 double_to_int64_bits(double value) noexcept;
104
111 static xtd::array<xtd::byte> get_bytes(bool value) noexcept;
112
119 static xtd::array<xtd::byte> get_bytes(char value) noexcept;
120
127 static xtd::array<xtd::byte> get_bytes(char16 value) noexcept;
128
135 static xtd::array<xtd::byte> get_bytes(char32 value) noexcept;
136
143 static xtd::array<xtd::byte> get_bytes(wchar value) noexcept;
144
151 static xtd::array<xtd::byte> get_bytes(double value) noexcept;
152
156 static xtd::array<xtd::byte> get_bytes(sbyte value) noexcept;
157
164 static xtd::array<xtd::byte> get_bytes(int16 value) noexcept;
165
172 static xtd::array<xtd::byte> get_bytes(int32 value) noexcept;
173
180 static xtd::array<xtd::byte> get_bytes(int64 value) noexcept;
181
188 static xtd::array<xtd::byte> get_bytes(float value) noexcept;
189
194
201 static xtd::array<xtd::byte> get_bytes(uint16 value) noexcept;
202
209 static xtd::array<xtd::byte> get_bytes(uint32 value) noexcept;
210
217 static xtd::array<xtd::byte> get_bytes(uint64 value) noexcept;
218
220 static xtd::array<xtd::byte> get_bytes(slong value) noexcept;
221 static xtd::array<xtd::byte> get_bytes(xtd::ulong value) noexcept;
223
228 static float int32_bits_to_single(int32 value) noexcept;
229
234 static double int64_bits_to_double(int64 value) noexcept;
235
240 static int32 single_to_int32_bits(float value) noexcept;
241
251 static bool to_boolean(const xtd::array<xtd::byte>& value, xtd::size start_index);
261 static bool to_boolean(const read_only_span<xtd::byte>& value, xtd::size start_index);
262
271 static char32 to_char(const xtd::array<xtd::byte>& value, xtd::size start_index);
280 static char32 to_char(const read_only_span<xtd::byte>& value, xtd::size start_index);
281
290 static double to_double(const xtd::array<xtd::byte>& value, xtd::size start_index);
299 static double to_double(const read_only_span<xtd::byte>& value, xtd::size start_index);
300
309 static int16 to_int16(const xtd::array<xtd::byte>& value, xtd::size start_index);
318 static int16 to_int16(const read_only_span<xtd::byte>& value, xtd::size start_index);
319
328 static int32 to_int32(const xtd::array<xtd::byte>& value, xtd::size start_index);
337 static int32 to_int32(const read_only_span<xtd::byte>& value, xtd::size start_index);
338
347 static int64 to_int64(const xtd::array<xtd::byte>& value, xtd::size start_index);
356 static int64 to_int64(const read_only_span<xtd::byte>& value, xtd::size start_index);
357
366 static float to_single(const xtd::array<xtd::byte>& value, xtd::size start_index);
375 static float to_single(const read_only_span<xtd::byte>& value, xtd::size start_index);
376
385 static uint16 to_uint16(const xtd::array<xtd::byte>& value, xtd::size start_index);
394 static uint16 to_uint16(const read_only_span<xtd::byte>& value, xtd::size start_index);
395
404 static uint32 to_uint32(const xtd::array<xtd::byte>& value, xtd::size start_index);
413 static uint32 to_uint32(const read_only_span<xtd::byte>& value, xtd::size start_index);
414
423 static uint64 to_uint64(const xtd::array<xtd::byte>& value, xtd::size start_index);
432 static uint64 to_uint64(const read_only_span<xtd::byte>& value, xtd::size start_index);
433
452
463 static xtd::string to_string(const xtd::array<xtd::byte>& value, xtd::size start_index);
474 static xtd::string to_string(const read_only_span<xtd::byte>& value, xtd::size start_index);
475
489 static xtd::string to_string(const xtd::array<xtd::byte>& value, xtd::size start_index, xtd::size length);
503 static xtd::string to_string(const read_only_span<xtd::byte>& value, xtd::size start_index, xtd::size length);
505 };
506}
507
509template<> struct xtd::enum_register<xtd::bit_converter::endian> {
510 explicit operator auto() const noexcept {return xtd::enum_collection<xtd::bit_converter::endian> {{xtd::bit_converter::endian::little, "little"}, {xtd::bit_converter::endian::big, "big"}};}
511};
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
Represents text as a sequence of character units.
Definition basic_string.hpp:71
Converts base data types to an xtd::array of bytes, and an xtd::array of bytes to base data types.
Definition bit_converter.hpp:52
static int32 to_int32(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static uint64 to_uint64(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static int32 to_int32(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static uint16 to_uint16(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static float int32_bits_to_single(int32 value) noexcept
Converts the specified 32-bit signed integer to a single-precision floating point number.
static int64 to_int64(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static xtd::array< xtd::byte > get_bytes(wchar value) noexcept
Returns the specified Char value as an xtd::array of bytes.
static uint64 to_uint64(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static xtd::array< xtd::byte > get_bytes(uint16 value) noexcept
Returns the specified uint16 value as an xtd::array of bytes.
static int64 double_to_int64_bits(double value) noexcept
Converts the specified double-precision floating point number to a 64-bit signed integer.
static xtd::array< xtd::byte > get_bytes(int32 value) noexcept
Returns the specified int32 value as an xtd::array of bytes.
static xtd::string to_string(const read_only_span< xtd::byte > &value, xtd::size start_index)
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static int16 to_int16(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static xtd::string to_string(const read_only_span< xtd::byte > &value)
Converts the numeric value of each element of a specified xtd::array of bytes to its equivalent hexad...
static int16 to_int16(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static uint32 to_uint32(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static bool to_boolean(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a Boolean value converted from one xtd::byte at a specified position in a xtd::byte xtd::arra...
static xtd::string to_string(const xtd::array< xtd::byte > &value)
Converts the numeric value of each element of a specified xtd::array of bytes to its equivalent hexad...
static xtd::array< xtd::byte > get_bytes(float value) noexcept
Returns the specified single value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(sbyte value) noexcept
Returns the specified xtd::byte value as an xtd::array of bytes.
static double to_double(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a double-precision floating point number converted from eight bytes at a specified position i...
static xtd::array< xtd::byte > get_bytes(xtd::byte value) noexcept
Returns the specified xtd::byte value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(uint64 value) noexcept
Returns the specified uint64 value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(double value) noexcept
Returns the specified double value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(char32 value) noexcept
Returns the specified Char value as an xtd::array of bytes.
endian
Represents the xtd::byte order ("endianness") in which data is stored in this computer architecture.
Definition bit_converter.hpp:60
@ little
Represnets "Little-endian" order.
@ big
Represnets "Big-endian" order.
static xtd::array< xtd::byte > get_bytes(int16 value) noexcept
Returns the specified int16 value as an xtd::array of bytes.
static uint16 to_uint16(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static xtd::string to_string(const xtd::array< xtd::byte > &value, xtd::size start_index, xtd::size length)
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static int32 single_to_int32_bits(float value) noexcept
Converts the specified single-precision floating point number to a 32-bit signed integer.
static char32 to_char(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a char32 converted from two bytes at a specified position in a xtd::byte xtd::array.
static xtd::array< xtd::byte > get_bytes(char value) noexcept
Returns the specified Char value as an xtd::array of bytes.
static char32 to_char(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a char32 converted from two bytes at a specified position in a xtd::byte xtd::read_only_span.
static float to_single(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a single-precision floating point number converted from eight bytes at a specified position i...
static int64 to_int64(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static xtd::string to_string(const read_only_span< xtd::byte > &value, xtd::size start_index, xtd::size length)
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static double to_double(const xtd::array< xtd::byte > &value, xtd::size start_index)
Returns a double-precision floating point number converted from eight bytes at a specified position i...
static double int64_bits_to_double(int64 value) noexcept
Converts the specified 64-bit signed integer to a double-precision floating point number.
static uint32 to_uint32(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static xtd::string to_string(const xtd::array< xtd::byte > &value, xtd::size start_index)
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static bool to_boolean(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a Boolean value converted from one xtd::byte at a specified position in a xtd::byte xtd::read...
static xtd::array< xtd::byte > get_bytes(int64 value) noexcept
Returns the specified int64 value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(uint32 value) noexcept
Returns the specified uint32 value as an xtd::array of bytes.
static float to_single(const read_only_span< xtd::byte > &value, xtd::size start_index)
Returns a single-precision floating point number converted from eight bytes at a specified position i...
static xtd::array< xtd::byte > get_bytes(char16 value) noexcept
Returns the specified Char value as an xtd::array of bytes.
static xtd::array< xtd::byte > get_bytes(bool value) noexcept
Returns the specified Boolean value as an xtd::array of bytes.
Represents a non-owning view over a contiguous sequence of objects.
Definition read_only_span.hpp:52
Contains core_export_ keyword.
Contains enum_ and enum_ut_ keywords.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
#define core_export_
Define shared library export.
Definition core_export.hpp:13
int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:26
uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::vector< std::pair< enum_t, xtd::string > > enum_collection
Represents a pair of an enum_t value and a string of an enum of type enum_t.
Definition enum_collection.hpp:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::read_only_span class.
Contains xtd::static_object class.
Provides the registration struct for enumerations.
Definition enum_register.hpp:38
Contains xtd fundamental types.