xtd 0.2.0
bit_converter.h
Go to the documentation of this file.
1 #pragma once
5 #include "core_export.h"
6 #include "enum.h"
7 #include "static.h"
8 #include "types.h"
9 #include <vector>
10 
12 namespace xtd {
49  class core_export_ bit_converter final static_ {
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 
381 template<> 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 };
Contains xtd::static_object class.
Contains xtd fundamental types.
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition: types.h:173
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:37
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition: types.h:228
Represnets "Big-endian" order.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
std::string to_string(const date_time &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: date_time.h:1063
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
Contains enum_ and enum_ut_ keywords.
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
Represnets "Little-endian" order.
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition: types.h:206
int_least16_t int16
Represents a 16-bit signed integer.
Definition: types.h:118
endian
Represents the xtd::byte order ("endianness") in which data is stored in this computer architecture...
Definition: bit_converter.h:57
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
char32_t char32
Represents a 32-bit unicode character.
Definition: types.h:85
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition: types.h:272
uint_least64_t uint64
Represents a 64-bit unsigned integer.
Definition: types.h:250
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
char16_t char16
Represents a 16-bit unicode character.
Definition: types.h:74
wchar_t wchar
Represents a wide character.
Definition: types.h:284
Provides the registration struct for enumerations.
Definition: enum_register.h:36