在 C++ 中正确返回 uint16_t 数组的方法

我有返回 uint 数组的 C# 代码,但我想在 C++ 中执行此操作。我看了其他帖子;他们使用 uint 指针数组,而我的数组不是。有谁知道如何正确返回 uint16_t 数组?


这是 C# 代码,运行良好


  public static UInt16[] GetIntArrayFromByteArray(byte[] byteArray)

        {

            if ((byteArray.Length % 2) == 1)

                Array.Resize(ref byteArray, byteArray.Length + 1);



            UInt16[] intArray = new UInt16[byteArray.Length / 2];



            for (int i = 0; i < byteArray.Length; i += 2)

                intArray[i / 2] = (UInt16)((byteArray[i] << 8) | byteArray[i + 1]);



            return intArray;

        }

这是创建语法错误的 C++ 代码


uint16_t[] GetIntArrayFromByteArray(byte[] byteArray)

{

    //if ((byteArray.Length % 2) == 1)

        //Array.Resize(ref byteArray, byteArray.Length + 1);



    uint16_t[] intArray = new uint16_t[10];



    for (int i = 0; i < 10; i += 2)

        intArray[i / 2] = (uint16_t)((byteArray[i] << 8) | byteArray[i + 1]);



    return intArray;

}


撒科打诨
浏览 194回答 1
1回答

喵喔喔

Type[]永远不要使用。用途std::vector:std::vector<uint16_t> GetIntArrayFromByteArray(std::vector<byte> byteArray){&nbsp; &nbsp; // If the number of bytes is not even, put a zero at the end&nbsp; &nbsp; if ((byteArray.size() % 2) == 1)&nbsp; &nbsp; &nbsp; &nbsp; byteArray.push_back(0);&nbsp; &nbsp; std::vector<uint16_t> intArray;&nbsp; &nbsp; for (int i = 0; i < byteArray.size(); i += 2)&nbsp; &nbsp; &nbsp; &nbsp; intArray.push_back((uint16_t)((byteArray[i] << 8) | byteArray[i + 1]));&nbsp; &nbsp; return intArray;}std::array<Type, Size>如果数组大小固定,您也可以使用。更优化的版本(感谢@Aconcagua)(演示)这是一个完整的代码,具有更优化的版本,不会复制或更改输入。如果您有很长的输入数组,这会更好。可以写得更短,但我想保持冗长并且对初学者友好。#include <iostream>#include <vector>using byte = unsigned char;std::vector<uint16_t> GetIntArrayFromByteArray(const std::vector<byte>& byteArray){&nbsp; &nbsp; const int inputSize = byteArray.size();&nbsp; &nbsp; const bool inputIsOddCount = inputSize % 2 != 0;&nbsp; &nbsp; const int finalSize = (int)(inputSize/2.0 + 0.5);&nbsp; &nbsp; // Ignore the last odd item in loop and handle it later&nbsp; &nbsp; const int loopLength = inputIsOddCount ? inputSize - 1 : inputSize;&nbsp; &nbsp; std::vector<uint16_t> intArray;&nbsp; &nbsp; // Reserve space for all items&nbsp; &nbsp; intArray.reserve(finalSize);&nbsp; &nbsp; for (int i = 0; i < loopLength; i += 2)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; intArray.push_back((uint16_t)((byteArray[i] << 8) | byteArray[i + 1]));&nbsp; &nbsp; }&nbsp; &nbsp; // If the input was odd-count, we still have one byte to add, along with a zero&nbsp; &nbsp; if(inputIsOddCount)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; // The zero in this expression is redundant but illustrative&nbsp; &nbsp; &nbsp; intArray.push_back((uint16_t)((byteArray[inputSize-1] << 8) | 0));&nbsp; &nbsp; }&nbsp; &nbsp; return intArray;}int main() {&nbsp; &nbsp; const std::vector<byte> numbers{2,0,0,0,1,0,0,1};&nbsp; &nbsp; const std::vector<uint16_t> result(GetIntArrayFromByteArray(numbers));&nbsp; &nbsp; for(uint16_t num: result) {&nbsp; &nbsp; &nbsp; &nbsp; std::cout << num << "\n";&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP