当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ Hex String转Signed Integer用法及代码示例

本文讨论在 C++ 中将十六进制字符串转换为有符号整数。有5种方法在 C++ 中执行此操作:

  1. 使用stoi()函数。
  2. 使用stoul()函数。
  3. 使用sscanf()函数。
  4. 使用字符串流方法。
  5. 使用升压:lexical_cast。

1.使用stoi函数

斯托伊 是一个函数string头文件。该函数将字符串作为参数并返回转换后的整数。函数语法如下:

用法:

int stoi (char *str, size_t* idx, int base);

这里,

str: The input string that is to be converted
size: Pointer to the object whose value is set by the function
base: It specifies the radix to determine the value type of input string

由于要求是将十六进制字符串转换为整数,因此基数为 16。

例子:

C++


// C++ program to implement
// stoi() function to convert
// hex string to signed integer
#include <iostream>
#include <string>
using namespace std;
// Driver code
int main()
{
  // The Hex string that is to 
  // be converted
  char *str = "4AF1";
   
  // Calling the function stoi 
  // Storing the return value in 
  // an unsigned integer
  signed number = stoi(str, 0, 16);
   
  // Displaying the unsigned integer 
  // equivalent to the hex 
  cout << "hex: " << str << 
          "\tinteger: " << number;
  return 0;
}
输出
hex: 4AF1    integer: 19185

说明:首先包含相关的头文件。然后定义一个包含十六进制 4AF1 作为值的字符串。该字符串(字符数组)与 0 和 16 作为参数一起传递给 stoi 函数。其中0为大小的默认值,16为十六进制转换。函数的返回值存储在有符号整数中。随后将显示该十六进制字符串及其等价的有符号整数。

2.使用soul函数

使用bits/stdc++.h 头文件中的stoul 函数可以产生相同的效果。

用法:

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);

这里,

str: String object with the representation of an integral number.
idx: Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
base: Numerical base (radix) determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence.Notice that by default this argument is 10, not 0. 

例子:

C++


// C++ program to implement stoul() 
// function to convert hex string 
// to a signed integer
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
   
// Driver code
int main()
{
  // Hexadecimal string
  string str = "FF";
   
  // Converting the string to 
  // unsigned integer
  unsigned num = stoul(str, 0, 16);
  
  cout << str << "is equals to " << num;
  return 0;
}
输出
FFis equals to 255

解释:首先,字符串包含值 FF (255)。然后这个字符串作为参数传递给 stoul 函数,以及 0(表示空指针)和 16(表示十六进制转换)。输出存储在无符号整数中。该变量显示在最后。

3.使用sscanf函数

使用默认 C++ 发行版中的 sscanf 函数可以产生相同的效果。

用法:

int sscanf(const char *str, const char *format, ...)

这里,

str: This is the C string that the function processes as its source to retrieve the data.
format: This is the C string that contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers

例子:

C++


// C++ program to implement the 
// sscanf() function to convert 
// a hex string to a signed integer
#include <iostream>
using namespace std;
// Driver code
int main()
{
  // Hexadecimal String
  char char_string[] = "4F";
   
  // Initializing the unsigned 
  // int to 0 value
  unsigned result = 0;
   
  // Calling the function and storing 
  // the resultant value in the address 
  // of result variable
  sscanf(char_string, "%X", &result);
   
  cout << result;
  return 0;
}
输出
79

说明: 字符串已初始化,包含值 4F (int = 79)。无符号 int 数据类型的另一个变量初始化为值 0。调用 sscanf 函数,并将字符串以及十六进制格式说明符 “%X” 和存储结果值的整数作为参数传递。最后显示有符号整数的值。

4.使用stringstream方法

stringstream 方法使用流对字符串对象进行变形,使其看起来像是从流中读取数据。该库主要用于解析存储在字符串内的数据。

例子:

C++


#include <bits/stdc++.h>
using namespace std;
int main()
{
    unsigned x;
   
      // Hexadecimal String
    string str = "FF";
    // Used for segregation of characters
    istringstream iss(str);
    // Converting to integer and storing the result in x
    iss >> hex >> x;
    cout << x;
}

输出:

255

解释:

与前面的示例类似,无符号整数变量和包含十六进制代码的字符串存储在单独的变量中。然后该字符串作为参数传递给国际空间站函数。该函数将常规字符串转换为流。然后将基字段格式设置为十六进制,导致十六进制字符串转换为整数,并将结果存储在变量中x。这是通过使用六角操纵器来完成的。最后显示无符号值。

5. 使用升压:lexical_cast

boost:lexical_cast 可以将十六进制字符串转换为有符号整数。它在幕后使用 stringstream。

例子:

C++


#include "boost/lexical_cast.hpp"
#include <bits/stdc++.h>
using namespace std;
using boost::lexical_cast;
int main() {
  
  string s1 = "0xFF";
   
  unsigned int x = lexical_cast<unsigned int>(s1); 
  cout << x << endl;
   
}

输出:

255

解释:该函数的工作和结构与中使用的非常相似字符串流方法。这里唯一的区别是0x必须添加到十六进制字符串之前,以使 lexical_cast 函数确认存在十六进制 String 。之后,该字符串作为参数传递给lexical_cast 函数,并存储其结果。最后,显示与十六进制代码关联的值。



相关用法


注:本文由纯净天空筛选整理自VasuDev4大神的英文原创作品 Convert Hex String to Signed Integer in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。