簡體   English   中英

c#) 如何在不復制新數組的情況下引用 1mb 字節數組的內容

[英]c#) How to refer to the contents of 1mb byte array, without copy new array

如何在不復制新數組的情況下引用 1mb 字節數組的內容。

我搜索了一些 function,例如 [ArraySegment]。

但它只支持 int 類型作為偏移量。

我需要使用長類型作為偏移量。

我不確定我是否理解這個問題,但也許以下方法是您正在尋找的:

    public static void BaSetLong(byte[] byteArray, int longOffset, long value) {
        int start = longOffset * 8;
        byte[] array = BitConverter.GetBytes(value);
        for (int i = 0; i < 8; i++) byteArray[start + i] = array[i];
    }

    public static long BaGetLong(byte[] byteArray, int longOffset) {
        int start = longOffset * 8;
        return BitConverter.ToInt64(byteArray, start);
    }

這就是Span<byte>的發明目的,盡管它僅在 netstardard 2.1 中引入。

如果你不能使用它,那么你必須傳遞對數組、偏移量和長度的引用。 盡管您可以創建自己的Span類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM