#include <xtd/as>
#include <xtd/console>
#include <xtd/convert>
#include <any>
auto main() -> int {
console::write_line("Convert int to byte :");
console::write_line("---------------------");
auto value = 42;
console::write_line("static_cast<unsigned char>(value) = {}", static_cast<unsigned char>(value));
console::write_line("as<unsigned char>(value) = {}", as<unsigned char>(value));
console::write_line("convert::to_byte(value) = {}", convert::to_byte(value));
console::write_line();
console::write_line("Convert float to byte :");
console::write_line("-----------------------");
auto float_value = 42.5f;
console::write_line("static_cast<unsigned char>(float_value) = {}", static_cast<unsigned char>(float_value));
console::write_line("as<unsigned char>(float_value) = {}", as<unsigned char>(float_value));
console::write_line("convert::to_byte(float_value) = {}", convert::to_byte(float_value));
console::write_line();
console::write_line("Convert any to byte :");
console::write_line("---------------------");
auto any_value = std::make_any<unsigned char>(42);
console::write_line("any_cast<unsigned char>(any_value) = {}", any_cast<unsigned char>(any_value));
console::write_line("as<unsigned char>(any_value) = {}", as<unsigned char>(any_value));
console::write_line("convert::to_byte(any_value) = {}", convert::to_byte(any_value));
console::write_line();
console::write_line("Convert string to byte :");
console::write_line("------------------------");
auto string_value = "42";
console::write_line("parse<unsigned char>(string_value)) = {}", parse<unsigned char>(string_value));
console::write_line("as<unsigned char>(string_value) = {}", as<unsigned char>(string_value));
console::write_line("convert::to_byte(string_value) = {}", convert::to_byte(string_value));
console::write_line();
console::write_line("Convert hexa string to byte :");
console::write_line("-----------------------------");
auto string_hex_value = "0x2A";
console::write_line("parse<unsigned char>(string_hex_value, number_styles::hex_number) = {}", parse<unsigned char>(string_hex_value, number_styles::hex_number));
console::write_line("as<unsigned char>(string_hex_value, 16) = {}", as<unsigned char>(string_hex_value, 16));
console::write_line("convert::to_byte(string_hex_value, 16)) = {}", convert::to_byte(string_hex_value, 16));
console::write_line();
console::write_line("Convert int to byte with overflow :");
console::write_line("-----------------------------------");
auto overflow_value = 257;
console::write_line("static_cast<unsigned char>(overflow_value) = {}", static_cast<unsigned char>(overflow_value));
try {
console::write_line("as<unsigned char>(overflow_value) = {}", as<unsigned char>(overflow_value));
console::write_line("(ERROR) as : Overflow exception");
}
try {
console::write_line("convert::to_byte(overflow_value) = {}", convert::to_byte(overflow_value));
console::write_line("(ERROR) convert::to_byte : Overflow exception");
}
}
The exception that is thrown when an arithmetic, casting, or conversion operation in a checked contex...
Definition overflow_exception.h:19
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10