#include <xtd/as>
#include <xtd/console>
#include <xtd/convert>
#include <any>
auto main() -> int {
console::write_line("Convert unsigned int to int :");
console::write_line("-----------------------------");
auto value = 42u;
console::write_line("static_cast<int>(value) = {}", static_cast<int>(value));
console::write_line("as<int>(value) = {}", as<int>(value));
console::write_line("convert::to_int32(value) = {}", convert::to_int32(value));
console::write_line();
console::write_line("Convert float to int :");
console::write_line("----------------------");
auto float_value = 42.5f;
console::write_line("static_cast<int>(float_value) = {}", static_cast<int>(float_value));
console::write_line("as<int>(float_value) = {}", as<int>(float_value));
console::write_line("convert::to_int32(float_value) = {}", convert::to_int32(float_value));
console::write_line();
console::write_line("Convert any to int :");
console::write_line("--------------------");
auto any_value = std::make_any<int>(42);
console::write_line("any_cast<int>(any_value) = {}", any_cast<int>(any_value));
console::write_line("as<int>(any_value) = {}", as<int>(any_value));
console::write_line("convert::to_int32(any_value) = {}", convert::to_int32(any_value));
console::write_line();
console::write_line("Convert string to int :");
console::write_line("-----------------------");
auto string_value = "42";
console::write_line("parse<int>(string_value)) = {}", parse<int>(string_value));
console::write_line("as<int>(string_value) = {}", as<int>(string_value));
console::write_line("convert::to_int32(string_value) = {}", convert::to_int32(string_value));
console::write_line();
console::write_line("Convert hexa string to int :");
console::write_line("----------------------------");
auto string_hex_value = "0x2A";
console::write_line("parse<int>(string_hex_value, number_styles::hex_number) = {}", parse<int>(string_hex_value, number_styles::hex_number));
console::write_line("as<int>(string_hex_value, 16) = {}", as<int>(string_hex_value, 16));
console::write_line("convert::to_int32(string_hex_value, 16)) = {}", convert::to_int32(string_hex_value, 16));
console::write_line();
console::write_line("Convert unsigned int to int with overflow :");
console::write_line("-------------------------------------------");
auto overflow_value = 2147483648u;
console::write_line("static_cast<int>(overflow_value) = {}", static_cast<int>(overflow_value));
try {
console::write_line("as<int>(overflow_value) = {}", as<int>(overflow_value));
console::write_line("(ERROR) as : Overflow exception");
}
try {
console::write_line("convert::to_int32(overflow_value) = {}", convert::to_int32(overflow_value));
console::write_line("(ERROR) convert::to_int32 : 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