0

我试图提取两个 (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 数组。

提前致谢

4

2 回答 2

6

你可以雇佣strstream

char v[255] = {};
{
  std::strstream vs(v, 254);
  vs << major << '.' << minor;
}
setVersion(v);
于 2012-06-30T14:15:18.770 回答
0

这是另一种方法。

snprintf(charArray, sizeof(charArray), "%d.%d", major, minor);

// Please check the correctness for format specifier and verify the return 
// value too.

如果您在 Windows 平台上,请使用_snprintf_s.

于 2012-06-30T14:16:38.287 回答