将数字的十六进制表示形式的字符串转换为实际数值

发布于 2024-09-16 13:30:44 字数 126 浏览 5 评论 0原文

我有一个像这样的字符串:

"00c4"

我需要将其转换为由文字表示的数值:

0x00c4

我该怎么做?

I have a string like this:

"00c4"

And I need to convert it to the numeric value that would be expressed by the literal:

0x00c4

How would I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ゃ懵逼小萝莉 2024-09-23 13:30:44

strtol 函数(或 strtoul 表示无符号长整型),来自 C 中的 stdlib.h 或 C++ 中的 cstdlib,允许您将字符串转换为特定基数的 long,因此像这样的事情应该做:

char *s = "00c4";
char *e;
long int i = strtol (s, &e, 16);
// Check that *e == '\0' assuming your string should ONLY
//    contain hex digits.
// Also check errno == 0.
// You can also just use NULL instead of &e if you're sure of the input.

The strtol function (or strtoul for unsigned long), from stdlib.h in C or cstdlib in C++, allows you to convert a string to a long in a specific base, so something like this should do:

char *s = "00c4";
char *e;
long int i = strtol (s, &e, 16);
// Check that *e == '\0' assuming your string should ONLY
//    contain hex digits.
// Also check errno == 0.
// You can also just use NULL instead of &e if you're sure of the input.
兔小萌 2024-09-23 13:30:44

您可以修改C++ 常见问题解答:

#include <iostream>
#include <sstream>
#include <stdexcept>

class bad_conversion : public std::runtime_error
{
public:
  bad_conversion(std::string const& s)
    : std::runtime_error(s)
  { }
};

template<typename T>
inline void convert_from_hex_string(std::string const& s, T& x,
  bool failIfLeftoverChars = true)
{
  std::istringstream i(s);
  char c;
  if (!(i >> std::hex >> x) || (failIfLeftoverChars && i.get(c)))
    throw ::bad_conversion(s);
}

int main(int argc, char* argv[])
{
  std::string blah = "00c4";
  int input;
  ::convert_from_hex_string(blah, input);
  std::cout << std::hex << input << "\n";
  return 0;
}

You can adapt the stringify sample found on the C++ FAQ:

#include <iostream>
#include <sstream>
#include <stdexcept>

class bad_conversion : public std::runtime_error
{
public:
  bad_conversion(std::string const& s)
    : std::runtime_error(s)
  { }
};

template<typename T>
inline void convert_from_hex_string(std::string const& s, T& x,
  bool failIfLeftoverChars = true)
{
  std::istringstream i(s);
  char c;
  if (!(i >> std::hex >> x) || (failIfLeftoverChars && i.get(c)))
    throw ::bad_conversion(s);
}

int main(int argc, char* argv[])
{
  std::string blah = "00c4";
  int input;
  ::convert_from_hex_string(blah, input);
  std::cout << std::hex << input << "\n";
  return 0;
}
你好,陌生人 2024-09-23 13:30:44
#include <stdio.h>

int main()
{
        const char *str = "0x00c4";
        int i = 0;
        sscanf(str, "%x", &i);
        printf("%d = 0x%x\n", i, i);
        return 0;
}
#include <stdio.h>

int main()
{
        const char *str = "0x00c4";
        int i = 0;
        sscanf(str, "%x", &i);
        printf("%d = 0x%x\n", i, i);
        return 0;
}
浪漫人生路 2024-09-23 13:30:44
std::string val ="00c4";
uint16_t out;
if( (std::istringstream(val)>>std::hex>>out).fail() )
{ /*error*/ }
std::string val ="00c4";
uint16_t out;
if( (std::istringstream(val)>>std::hex>>out).fail() )
{ /*error*/ }
小ぇ时光︴ 2024-09-23 13:30:44

不存在“实际十六进制值”这样的东西。一旦您进入本机数据类型,您就处于二进制状态。上面介绍了如何通过十六进制字符串到达​​那里。将其显示为十六进制输出,同上。但它不是“实际的十六进制值”。这只是二进制的。

There is no such thing as an 'actual hex value'. Once you get into the native datatypes you are in binary. Getting there from a hex string is covered above. Showing it as output in hex, ditto. But it isn't an 'actual hex value'. It's just binary.

七秒鱼° 2024-09-23 13:30:44
int val_from_hex(char *hex_string) {
    char *c = hex_string;
    int val = 0;
    while(*c) {
        val <<= 4;
        if(*c >= '0' && *c <= '9')
            val += *c - '0';
        else if(*c >= 'a' && *c <= 'f')
            val += *c - 'a' + 10;
        c++;
    }
    return val;
}
int val_from_hex(char *hex_string) {
    char *c = hex_string;
    int val = 0;
    while(*c) {
        val <<= 4;
        if(*c >= '0' && *c <= '9')
            val += *c - '0';
        else if(*c >= 'a' && *c <= 'f')
            val += *c - 'a' + 10;
        c++;
    }
    return val;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文