xtd 0.2.0
Loading...
Searching...
No Matches
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;
92
94
103 [[nodiscard]] static auto double_to_int64_bits(double value) noexcept -> xtd::int64;
110 [[nodiscard]] static auto get_bytes(bool value) noexcept -> xtd::array<xtd::byte>;
117 [[nodiscard]] static auto get_bytes(char value) noexcept -> xtd::array<xtd::byte>;
124 [[nodiscard]] static auto get_bytes(char8 value) noexcept -> xtd::array<xtd::byte>;
131 [[nodiscard]] static auto get_bytes(char16 value) noexcept -> xtd::array<xtd::byte>;
138 [[nodiscard]] static auto get_bytes(char32 value) noexcept -> xtd::array<xtd::byte>;
145 [[nodiscard]] static auto get_bytes(wchar value) noexcept -> xtd::array<xtd::byte>;
152 [[nodiscard]] static auto get_bytes(double value) noexcept -> xtd::array<xtd::byte>;
156 [[nodiscard]] static auto get_bytes(sbyte value) noexcept -> xtd::array<xtd::byte>;
163 [[nodiscard]] static auto get_bytes(int16 value) noexcept -> xtd::array<xtd::byte>;
170 [[nodiscard]] static auto get_bytes(int32 value) noexcept -> xtd::array<xtd::byte>;
177 [[nodiscard]] static auto get_bytes(int64 value) noexcept -> xtd::array<xtd::byte>;
184 [[nodiscard]] static auto get_bytes(float value) noexcept -> xtd::array<xtd::byte>;
188 [[nodiscard]] static auto get_bytes(xtd::byte value) noexcept -> xtd::array<xtd::byte>;
195 [[nodiscard]] static auto get_bytes(uint16 value) noexcept -> xtd::array<xtd::byte>;
202 [[nodiscard]] static auto get_bytes(uint32 value) noexcept -> xtd::array<xtd::byte>;
209 [[nodiscard]] static auto get_bytes(uint64 value) noexcept -> xtd::array<xtd::byte>;
210
212 [[nodiscard]] static auto get_bytes(slong value) noexcept -> xtd::array<xtd::byte>;
213 [[nodiscard]] static auto get_bytes(xtd::ulong value) noexcept -> xtd::array<xtd::byte>;
215
220 [[nodiscard]] static auto int32_bits_to_single(int32 value) noexcept -> xtd::single;
221
226 [[nodiscard]] static auto int64_bits_to_double(int64 value) noexcept -> double;
227
232 [[nodiscard]] static auto single_to_int32_bits(float value) noexcept -> xtd::int32;
233
243 [[nodiscard]] static auto to_boolean(const xtd::array<xtd::byte>& value, xtd::size start_index) -> bool;
253 [[nodiscard]] static auto to_boolean(const read_only_span<xtd::byte>& value, xtd::size start_index) -> bool;
254
263 [[nodiscard]] static auto to_char(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::char32;
272 [[nodiscard]] static auto to_char(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::char32;
273
282 [[nodiscard]] static auto to_double(const xtd::array<xtd::byte>& value, xtd::size start_index) -> double;
291 [[nodiscard]] static auto to_double(const read_only_span<xtd::byte>& value, xtd::size start_index) -> double;
292
301 [[nodiscard]] static auto to_int16(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::int16;
310 [[nodiscard]] static auto to_int16(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::int16;
311
320 [[nodiscard]] static auto to_int32(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::int32;
329 [[nodiscard]] static auto to_int32(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::int32;
330
339 [[nodiscard]] static auto to_int64(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::int64;
348 [[nodiscard]] static auto to_int64(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::int64;
349
358 [[nodiscard]] static auto to_single(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::single;
367 [[nodiscard]] static auto to_single(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::single;
368
377 [[nodiscard]] static auto to_uint16(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::uint16;
386 [[nodiscard]] static auto to_uint16(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::uint16;
387
396 [[nodiscard]] static auto to_uint32(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::uint32;
405 [[nodiscard]] static auto to_uint32(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::uint32;
406
415 [[nodiscard]] static auto to_uint64(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::uint64;
424 [[nodiscard]] static auto to_uint64(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::uint64;
425
434 [[nodiscard]] static auto to_size(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::size;
443 [[nodiscard]] static auto to_size(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::size;
444
453 [[nodiscard]] static auto to_string(const xtd::array<xtd::byte>& value) -> xtd::string;
462 [[nodiscard]] static auto to_string(const read_only_span<xtd::byte>& value) -> xtd::string;
473 [[nodiscard]] static auto to_string(const xtd::array<xtd::byte>& value, xtd::size start_index) -> xtd::string;
484 [[nodiscard]] static auto to_string(const read_only_span<xtd::byte>& value, xtd::size start_index) -> xtd::string;
498 [[nodiscard]] static auto to_string(const xtd::array<xtd::byte>& value, xtd::size start_index, xtd::size length) -> xtd::string;
512 [[nodiscard]] static auto to_string(const read_only_span<xtd::byte>& value, xtd::size start_index, xtd::size length) -> xtd::string;
514 };
515}
516
518template<> struct xtd::enum_register<xtd::bit_converter::endian> {
519 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"}};}
520};
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
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 auto get_bytes(double value) noexcept -> xtd::array< xtd::byte >
Returns the specified double value as an xtd::array of bytes.
static auto get_bytes(char32 value) noexcept -> xtd::array< xtd::byte >
Returns the specified xtd::char32 value as an xtd::array of bytes.
static auto to_single(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::single
Returns a single-precision floating point number converted from eight bytes at a specified position i...
static auto get_bytes(bool value) noexcept -> xtd::array< xtd::byte >
Returns the specified Boolean value as an xtd::array of bytes.
static constexpr bool is_little_endian
Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
Definition bit_converter.hpp:83
static auto get_bytes(uint32 value) noexcept -> xtd::array< xtd::byte >
Returns the specified uint32 value as an xtd::array of bytes.
static auto to_uint32(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::uint32
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto to_double(const read_only_span< xtd::byte > &value, xtd::size start_index) -> double
Returns a double-precision floating point number converted from eight bytes at a specified position i...
static auto get_bytes(uint64 value) noexcept -> xtd::array< xtd::byte >
Returns the specified uint64 value as an xtd::array of bytes.
static auto to_double(const xtd::array< xtd::byte > &value, xtd::size start_index) -> double
Returns a double-precision floating point number converted from eight bytes at a specified position i...
static auto to_char(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::char32
Returns a char32 converted from two bytes at a specified position in a xtd::byte xtd::read_only_span.
static auto to_int32(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::int32
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto to_string(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::string
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static auto to_string(const xtd::array< xtd::byte > &value) -> xtd::string
Converts the numeric value of each element of a specified xtd::array of bytes to its equivalent hexad...
static auto to_int64(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::int64
Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto to_int32(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::int32
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto to_string(const read_only_span< xtd::byte > &value, xtd::size start_index, xtd::size length) -> xtd::string
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static constexpr bool is_big_endian
Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
Definition bit_converter.hpp:76
static auto to_uint64(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::uint64
Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static auto to_string(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::string
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static auto get_bytes(char8 value) noexcept -> xtd::array< xtd::byte >
Returns the specified xtd::char8 value as an xtd::array of bytes.
static constexpr endian endianness
Indicates the xtd::byte order ("endianness") in which data is stored in this computer architecture.
Definition bit_converter.hpp:90
static auto get_bytes(char value) noexcept -> xtd::array< xtd::byte >
Returns the specified char value as an xtd::array of bytes.
static auto to_boolean(const xtd::array< xtd::byte > &value, xtd::size start_index) -> bool
Returns a Boolean value converted from one xtd::byte at a specified position in a xtd::byte xtd::arra...
static auto to_uint32(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::uint32
Returns a 32-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
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.
Definition bit_converter.hpp:62
@ big
Represnets "Big-endian" order.
Definition bit_converter.hpp:64
static auto get_bytes(float value) noexcept -> xtd::array< xtd::byte >
Returns the specified single value as an xtd::array of bytes.
static auto to_int64(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::int64
Returns a 64-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto get_bytes(xtd::byte value) noexcept -> xtd::array< xtd::byte >
Returns the specified xtd::byte value as an xtd::array of bytes.
static auto get_bytes(int16 value) noexcept -> xtd::array< xtd::byte >
Returns the specified int16 value as an xtd::array of bytes.
static auto get_bytes(int32 value) noexcept -> xtd::array< xtd::byte >
Returns the specified int32 value as an xtd::array of bytes.
static auto to_int16(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::int16
Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto get_bytes(wchar value) noexcept -> xtd::array< xtd::byte >
Returns the specified wchar value as an xtd::array of bytes.
static auto to_size(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::size
Returns a sxtd::ize converted from two bytes at a specified position in a xtd::byte xtd::read_only_sp...
static auto to_uint64(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::uint64
Returns a 64-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static auto to_string(const xtd::array< xtd::byte > &value, xtd::size start_index, xtd::size length) -> xtd::string
Converts the numeric value of each element of a specified sub xtd::array of bytes to its equivalent h...
static auto get_bytes(char16 value) noexcept -> xtd::array< xtd::byte >
Returns the specified xtd::char16 value as an xtd::array of bytes.
static auto get_bytes(int64 value) noexcept -> xtd::array< xtd::byte >
Returns the specified int64 value as an xtd::array of bytes.
static auto double_to_int64_bits(double value) noexcept -> xtd::int64
Converts the specified double-precision floating point number to a 64-bit signed integer.
static auto to_single(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::single
Returns a single-precision floating point number converted from eight bytes at a specified position i...
static auto to_size(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::size
Returns a xtd::size converted from two bytes at a specified position in a xtd::byte xtd::array.
static auto int32_bits_to_single(int32 value) noexcept -> xtd::single
Converts the specified 32-bit signed integer to a single-precision floating point number.
static auto to_uint16(const read_only_span< xtd::byte > &value, xtd::size start_index) -> xtd::uint16
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
static auto int64_bits_to_double(int64 value) noexcept -> double
Converts the specified 64-bit signed integer to a double-precision floating point number.
static auto to_boolean(const read_only_span< xtd::byte > &value, xtd::size start_index) -> bool
Returns a Boolean value converted from one xtd::byte at a specified position in a xtd::byte xtd::read...
static auto to_char(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::char32
Returns a char32 converted from two bytes at a specified position in a xtd::byte xtd::array.
static auto to_int16(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::int16
Returns a 16-bit signed integer converted from two bytes at a specified position in a xtd::byte xtd::...
static auto get_bytes(sbyte value) noexcept -> xtd::array< xtd::byte >
Returns the specified xtd::byte value as an xtd::array of bytes.
static auto to_string(const read_only_span< xtd::byte > &value) -> xtd::string
Converts the numeric value of each element of a specified xtd::array of bytes to its equivalent hexad...
static auto get_bytes(uint16 value) noexcept -> xtd::array< xtd::byte >
Returns the specified uint16 value as an xtd::array of bytes.
static auto single_to_int32_bits(float value) noexcept -> xtd::int32
Converts the specified single-precision floating point number to a 32-bit signed integer.
static auto to_uint16(const xtd::array< xtd::byte > &value, xtd::size start_index) -> xtd::uint16
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a xtd::byte xtd...
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:38
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::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
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
std::uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
std::int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
std::uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
float single
Represents a single-precision floating-point number.
Definition single.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
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:24
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
constexpr read_only_span()
Creates an empty xtd::read_only_span whose xtd::read_only_span::data is null and xtd::read_only_span:...
Definition read_only_span.hpp:88
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.