xtd 0.2.0
Loading...
Searching...
No Matches
bit_converter.h
Go to the documentation of this file.
1
4#pragma once
5#include "core_export.h"
6#include "enum.h"
7#include "static.h"
8#include "types.h"
9#include <vector>
10
12namespace xtd {
50 private:
51 static constexpr uint32 __endian_value__ = 0x01020304;
52 static constexpr xtd::byte __endian_query__ = static_cast<const xtd::byte&>(__endian_value__);
53
54 public:
57 enum class endian {
59 little,
61 big,
62 };
63
65
73 static constexpr bool is_big_endian = __endian_query__ == 0x01u;
80 static constexpr bool is_little_endian = __endian_query__ == 0x04u;
87 static constexpr endian endianness = is_little_endian ? endian::little : endian::big;
89
91
100 static int64 double_to_int64_bits(double value) noexcept;
101
108 static std::vector<xtd::byte> get_bytes(bool value) noexcept;
109
116 static std::vector<xtd::byte> get_bytes(char value) noexcept;
117
124 static std::vector<xtd::byte> get_bytes(char16 value) noexcept;
125
132 static std::vector<xtd::byte> get_bytes(char32 value) noexcept;
133
140 static std::vector<xtd::byte> get_bytes(wchar value) noexcept;
141
148 static std::vector<xtd::byte> get_bytes(double value) noexcept;
149
153 static std::vector<xtd::byte> get_bytes(sbyte value) noexcept;
154
161 static std::vector<xtd::byte> get_bytes(int16 value) noexcept;
162
169 static std::vector<xtd::byte> get_bytes(int32 value) noexcept;
170
177 static std::vector<xtd::byte> get_bytes(int64 value) noexcept;
178
185 static std::vector<xtd::byte> get_bytes(float value) noexcept;
186
190 static std::vector<xtd::byte> get_bytes(xtd::byte value) noexcept;
191
198 static std::vector<xtd::byte> get_bytes(uint16 value) noexcept;
199
206 static std::vector<xtd::byte> get_bytes(uint32 value) noexcept;
207
214 static std::vector<xtd::byte> get_bytes(uint64 value) noexcept;
215
217 static std::vector<xtd::byte> get_bytes(slong value) noexcept;
218 static std::vector<xtd::byte> get_bytes(xtd::ulong value) noexcept;
220
225 static float int32_bits_to_single(int32 value) noexcept;
226
231 static double int64_bits_to_double(int64 value) noexcept;
232
237 static int32 single_to_int32_bits(float value) noexcept;
238
248 static bool to_boolean(const std::vector<xtd::byte>& value, size_t start_index);
249
258 static char32 to_char(const std::vector<xtd::byte>& value, size_t start_index);
259
268 static double to_double(const std::vector<xtd::byte>& value, size_t start_index);
269
278 static int16 to_int16(const std::vector<xtd::byte>& value, size_t start_index);
279
288 static int32 to_int32(const std::vector<xtd::byte>& value, size_t start_index);
289
298 static int64 to_int64(const std::vector<xtd::byte>& value, size_t start_index);
299
308 static float to_single(const std::vector<xtd::byte>& value, size_t start_index);
309
318 static uint16 to_uint16(const std::vector<xtd::byte>& value, size_t start_index);
319
328 static uint32 to_uint32(const std::vector<xtd::byte>& value, size_t start_index);
329
338 static uint64 to_uint64(const std::vector<xtd::byte>& value, size_t start_index);
339
348 static xtd::ustring to_string(const std::vector<xtd::byte>& value);
349
360 static xtd::ustring to_string(const std::vector<xtd::byte>& value, size_t start_index);
361
375 static xtd::ustring to_string(const std::vector<xtd::byte>& value, size_t start_index, size_t length);
377 };
378}
379
381template<> struct xtd::enum_register<xtd::bit_converter::endian> {
382 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"}};}
383};
Converts base data types to an std::vector of bytes, and an std::vector of bytes to base data types.
Definition bit_converter.h:49
static float int32_bits_to_single(int32 value) noexcept
Converts the specified 32-bit signed integer to a single-precision floating point number.
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::...
static std::vector< xtd::byte > get_bytes(uint16 value) noexcept
Returns the specified uint16 value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(wchar value) noexcept
Returns the specified Char value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(int16 value) noexcept
Returns the specified int16 value as an std::vector of bytes.
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 i...
static std::vector< xtd::byte > get_bytes(uint32 value) noexcept
Returns the specified uint32 value as an std::vector 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 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::...
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...
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::vect...
static std::vector< xtd::byte > get_bytes(char value) noexcept
Returns the specified Char value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(xtd::byte value) noexcept
Returns the specified xtd::byte value as an std::vector of bytes.
static xtd::ustring 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 ...
static std::vector< xtd::byte > get_bytes(sbyte value) noexcept
Returns the specified xtd::byte value as an std::vector of bytes.
endian
Represents the xtd::byte order ("endianness") in which data is stored in this computer architecture.
Definition bit_converter.h:57
@ little
Represnets "Little-endian" order.
@ big
Represnets "Big-endian" order.
static xtd::ustring 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 int32 single_to_int32_bits(float value) noexcept
Converts the specified single-precision floating point number to a 32-bit signed integer.
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...
static std::vector< xtd::byte > get_bytes(int32 value) noexcept
Returns the specified int32 value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(bool value) noexcept
Returns the specified Boolean value as an std::vector of bytes.
static double int64_bits_to_double(int64 value) noexcept
Converts the specified 64-bit signed integer to a double-precision floating point number.
static xtd::ustring 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 ...
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::...
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::...
static std::vector< xtd::byte > get_bytes(char16 value) noexcept
Returns the specified Char value as an std::vector of bytes.
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 std::vector< xtd::byte > get_bytes(double value) noexcept
Returns the specified double value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(char32 value) noexcept
Returns the specified Char value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(int64 value) noexcept
Returns the specified int64 value as an std::vector of bytes.
static std::vector< xtd::byte > get_bytes(uint64 value) noexcept
Returns the specified uint64 value as an std::vector of bytes.
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 i...
static std::vector< xtd::byte > get_bytes(float value) noexcept
Returns the specified single value as an std::vector of bytes.
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
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.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
wchar_t wchar
Represents a wide character.
Definition types.h:286
int_least16_t int16
Represents a 16-bit signed integer.
Definition types.h:120
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition types.h:274
char16_t char16
Represents a 16-bit unicode character.
Definition types.h:76
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition types.h:175
int_least64_t int64
Represents a 64-bit signed integer.
Definition types.h:142
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition types.h:230
int_least32_t int32
Represents a 32-bit signed integer.
Definition types.h:131
char32_t char32
Represents a 32-bit unicode character.
Definition types.h:87
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition types.h:252
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition types.h:241
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition types.h:208
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition types.h:41
std::vector< std::pair< enum_t, xtd::ustring > > enum_collection
Represents a pair of an enum_t value and a string of an enum of type enum_t.
Definition enum_collection.h:19
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::static_object class.
Provides the registration struct for enumerations.
Definition enum_register.h:36
Contains xtd fundamental types.