xtd 0.2.0
bit_converter_round_trips.cpp

Shows how to use bit_converter class.

#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
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 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 xtd::array< xtd::byte > get_bytes(bool value) noexcept
Returns the specified Boolean value as an xtd::array of bytes.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8