2017-03-24 1 views
0

私はC + +で初めての人です。誰もtypedefをベクトル(char)に変換する方法を知っていますか?解凍をベクトル(char)に変換します。typedefをベクタに変換する

opusencoder encodedatag; 
opus_int16 *decompress = encodedatag.encodedata(...); 
+1

opusencoder'、あなたが期待 'opus_int16'、してください詳細の入力と出力のものだ'何ですか – jpo38

答えて

0

私はあなたが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; 
} 
0

受け取ったショートパンツは、ホストのエンディアンに短いが、実際には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; 
} 

あなたは、配列の長さを知っている場合、あなたは繰り返すことができ、解凍によって指さ配列の上で各shortとpush_backを変換する各char。

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); 
} 

(コードの例として、コンパイルやテストしていません)

関連する問題