【问题标题】:How to convert const char* to uint16_t octal (C/C++) [closed]如何将 const char* 转换为 uint16_t 八进制(C/C++)[关闭]
【发布时间】:2021-05-05 04:54:23
【问题描述】:

我需要将前导零的值转换为八进制。因为 12(八进制 14)与 012(八进制 10)不同

我将调用的函数需要一个 uint16_t 类型的变量,并且要转换的值是动态接收的,可以存储在 String 或 const char* 中。

String node = "012"; // received dynamically

const char* node = '012'; // received dynamically

我需要的是: uint16_t n = node;

功能:

begin(uint8_t _channel, uint16_t _node_address )

begin(90, n);

【问题讨论】:

  • 你可以直接打电话给strtolstrtoul吗?
  • 使用std::stoi "...前缀(0)表示八进制..." en.cppreference.com/w/cpp/string/basic_string/stol
  • c 还是 c++? const char* node = '012' 在两者中均无效。 String 是什么?是std::stringchar * 的别名还是别的什么?

标签: c++ c octal leading-zero


【解决方案1】:
unsigned strToInt(const char *str, int base)
{
    const char digits[] = "01234567890ABCDEF";
    unsigned result = 0;

    while(*str)
    {
        result *= base;
        result += strchr(digits, *str++) - digits;
    }
    return result;
}

【讨论】:

  • 对我来说工作得很好。
猜你喜欢
  • 2015-04-02
  • 2013-12-31
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 2019-11-11
  • 2020-08-04
  • 1970-01-01
相关资源
最近更新 更多