C++Convert Hexadecimal String to Decimal Integer
To convert a hexadecimal string (of type string
) into an integer in C++, you can use the stoi()
function with a base of 16. Here's an example:
#include
#include
int main() {
std::string hexStr = "1A3F"; // Hexadecimal string
int decimalValue = std::stoi(hexStr, nullptr, 16); // Convert hex to int
std::cout << "Decimal value: " << decimalValue>
In this code:
- The string hexStr contains a hexadecimal number.
- The function std::stoi(hexStr, nullptr, 16) converts the hexadecimal string to a decimal integer using base 16.
- The result is stored in decimalValue and printed to the console.
337B
文件大小:
评论区