string - How do I convert hex numbers to a character in c++? -
i'm trying convert hex number character in c++. looked can't find answer works me.
here code:
char mod_tostring(int state, int index, int size) { int stringaddress = lua_tolstring(state, index, 0); const char* const base = (const char* const)stringaddress; return base[0]; };
base[0] return hex number like: 0000005b
if go here http://string-functions.com/hex-string.aspx , put 0000005b input outputs character "[". how output [?
to print number character, can either assign char
variable or cast char
type:
unsigned int value = 0x5b; char c = static_cast<char>(value); cout << "the character of 0x5b '" << c << "` , '" << static_cast<char>(value) << "'\n";
you use snprintf
:
char text_buffer[128]; unsigned int value = 0x5b; snprintf(&text_buffer[0], sizeof(text_buffer), "%c\n", value); puts(text_buffer);
example program:
#include <iostream> #include <cstdlib> int main() { unsigned int value = 0x5b; char c = static_cast<char>(value); std::cout << "the character of 0x5b '" << c << "` , '" << static_cast<char>(value) << "'\n"; std::cout << "\n" << "paused. press enter continue.\n"; std::cin.ignore(1000000, '\n'); return exit_success; }
output:
$ ./main.exe character of 0x5b '[` , '[' paused. press enter continue.
Comments
Post a Comment