c++ - 将两个整数值连接成一个 char 数组

标签 c++

我试图提取两个 (int32_t) 值,并将它们放在一个 char 数组中。

int32_t version = getVersion();

if (version < 0)
{
    return;
}
else
{
    //first part of number needs to be shifted right
    int32_t major = (((version) >>16) & 0xFFFF);
    int32_t minor = ((version) & 0xFFFF);

    // need to concatenate these two values, and place a "." between them

    setVersion(...);//requires a char array, should for example be "1.1"
}

任何人都可以就最好的方法给我任何建议吗?请不要使用 std::strings。 我更喜欢 char 数组。

提前致谢

最佳答案

您可以使用 strstream

char v[255] = {};
{
  std::strstream vs(v, 254);
  vs << major << '.' << minor;
}
setVersion(v);

关于c++ - 将两个整数值连接成一个 char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11274740/

相关文章:

c++ - 声明全局变量,在方法中初始化

C++ 将指针传递给多维数组

c++ iostream - 未创建输出文件

c++ - 如何将派生对象添加到 unique_ptr 的 vector 中

c++ - MeeGo 开发可行吗?

c++ - 字符串比较 vector C++

c++ - 您选择的 cpu 不支持 x86-64 指令集

c++ - accumulate 和 ostringstream 编译错误

c++ - C++中long double的精度是多少?

c++ - Visual Studio 2010 的 dword ptr [...] 问题