I’m am pretty inexperienced with c++, but could use some help. I’m trying to write a small function that reads a line from an external .txt file and then writes the hexadecimal value to a short int.
Example:
Line read from external text file: 7c00
code:
short end_addr = 0x7c00
I already have the code to read the line from the external file, just need to know how I would save the character information (7c00) into the short end_addr as hexadecimal 0x7c00.
I will need to write protection for it I guess too so if there were too many characters (e.g 7c0010) or incorrect characters (e.g zyx4) then there would be an error, but really just need help with the conversion algorithm for now. Thanks for any help!
I would suggest to look into
boost::lexical_castfor simple interface and proper error handling.This code doesn’t handle errors, but you may add error handling by verifying that the whole line was read.
Explanation std::hex in C++ reference
So
std::hexchanges default numeric base to 16, andstd::decchanges it back to 10.