简体   繁体   中英

Addition of an integer with an hex string

What should I do to add an integer to an hex string.

Say my hex string is:

11'h000

And I want to add integer 7 to it. Output it should give should be

11'h007

If given 11'h00e, Adding integer 1 to it should give me 11'h00f.

Are there any predefined functions in c++? I could have write my switch-case statements to get it but looking for a compact way.

The best way? Don't confuse formatting of a number with a number.

Use

int x = std::stoi(s/*a hexadecimal string*/, nullptr, 16 /*hexadecimal*/);
x++; /*all your arithmetic operations here*/
std::cout/*or a suitable stream*/ << std::hex << x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM