将 typedef short 转换为矢量(字符)
Convert typedef short to vector (char)
我是C++的新人。有谁知道如何将 typedef short 转换为 vector(char( ?将解压缩转换为矢量(字符(。
opusencoder encodedatag;
opus_int16 *decompress = encodedatag.encodedata(...);
假设收到的短裤位于主机的字节序中,并且短裤实际上是 16 位(uint16_t会更好(,并且字符预计是短裤的大端表示,将短裤转换为 char 可以像这样完成:
std::pair<uint8_t, uint8_t> convert_uint16_to_uint8s(uint16_t in)
{
std::pair<uint8_t, uint8_t> out;
out.first = in >> 8;
out.second = in & 0xff;
return out;
}
如果您知道解压缩指向的数组长度,则可以迭代数组并转换每个短字符和push_back字符。
std::vector<uint8_t> output;
for (int i = 0; i < decompress_length; ++i)
{
std::pair<uint8_t, uint8_t> chars = convert_uint16_to_uint8s(decompress[i];
output.push_back(chars.first);
output.push_back(chars.second);
}
(代码作为示例,未编译或测试(
我假设你有一个short
,你想把它表示为vector<char>
中的char
序列。
如果是这样的话,那就是我的建议,用一个简单的测试:)
#include <iostream>
#include <vector>
#include <algorithm>
using opus_int16 = short;
std::vector<char> toVector(opus_int16 arg)
{
std::vector<char> converted;
opus_int16 remainder(0);
bool isNegative((arg < 0) ? true : false);
if (isNegative)
arg = -arg;
while (arg != 0)
{
remainder = arg % 10;
arg /= 10;
converted.push_back(remainder + 48);
}
if (isNegative)
converted.push_back('-');
std::reverse(converted.begin(), converted.end());
return converted;
}
int main(int argc, char **argv)
{
opus_int16 example = -156;
std::vector<char> test(toVector(example));
for (auto i : test)
std::cout << i;
std::cin.get();
return 0;
}
相关文章:
- C++:TypeDef使用元组
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 将 typedef short 转换为矢量(字符)
- 为什么我不能像正常一样初始化 typedef 结构字符串或字符?
- 无已知转换 在带有 std::vector 的无符号字符的 typedef 数组之间