亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 C++ 中正確返回 uint16_t 數組的方法

在 C++ 中正確返回 uint16_t 數組的方法

C#
撒科打諢 2023-09-16 15:07:31
我有返回 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;}
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

Type[]永遠不要使用。用途std::vector:


std::vector<uint16_t> GetIntArrayFromByteArray(std::vector<byte> byteArray)

{

    // If the number of bytes is not even, put a zero at the end

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

        byteArray.push_back(0);



    std::vector<uint16_t> intArray;


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

        intArray.push_back((uint16_t)((byteArray[i] << 8) | byteArray[i + 1]));


    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)

{

    const int inputSize = byteArray.size();

    const bool inputIsOddCount = inputSize % 2 != 0;

    const int finalSize = (int)(inputSize/2.0 + 0.5);

    // Ignore the last odd item in loop and handle it later

    const int loopLength = inputIsOddCount ? inputSize - 1 : inputSize;


    std::vector<uint16_t> intArray;

    // Reserve space for all items

    intArray.reserve(finalSize);

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

    {

      intArray.push_back((uint16_t)((byteArray[i] << 8) | byteArray[i + 1]));

    }


    // If the input was odd-count, we still have one byte to add, along with a zero

    if(inputIsOddCount) 

    {

      // The zero in this expression is redundant but illustrative

      intArray.push_back((uint16_t)((byteArray[inputSize-1] << 8) | 0));

    }

    return intArray;

}


int main() {

    const std::vector<byte> numbers{2,0,0,0,1,0,0,1};

    const std::vector<uint16_t> result(GetIntArrayFromByteArray(numbers));


    for(uint16_t num: result) {

        std::cout << num << "\n";

    }


    return 0;

}


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 318 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號