簡體   English   中英

C ++ int到byte數組

[英]C++ int to byte array

我在我的java代碼中有這個方法,它返回給定int的字節數組:

private static byte[] intToBytes(int paramInt)
{
     byte[] arrayOfByte = new byte[4];
     ByteBuffer localByteBuffer = ByteBuffer.allocate(4);
     localByteBuffer.putInt(paramInt);
     for (int i = 0; i < 4; i++)
         arrayOfByte[(3 - i)] = localByteBuffer.array()[i];
     return arrayOfByte;
}

有人可以給我提示如何將該方法轉換為C ++?

你不需要一個完整的功能; 一個簡單的演員就足夠了:

int x;
static_cast<char*>(static_cast<void*>(&x));

C ++中的任何對象都可以重新解釋為字節數組。 如果要將字節的副本實際復制到單獨的數組中,可以使用std::copy

int x;
char bytes[sizeof x];
std::copy(static_cast<const char*>(static_cast<const void*>(&x)),
          static_cast<const char*>(static_cast<const void*>(&x)) + sizeof x,
          bytes);

這些方法都沒有考慮字節順序,但由於您可以將int重新解釋為字節數組,因此自行執行任何必要的修改都是微不足道的。

使用std::vector<unsigned char>

#include <vector>
using namespace std;

vector<unsigned char> intToBytes(int paramInt)
{
     vector<unsigned char> arrayOfByte(4);
     for (int i = 0; i < 4; i++)
         arrayOfByte[3 - i] = (paramInt >> (i * 8));
     return arrayOfByte;
}

我使用的另一個有用的方法是工會:

union byteint
{
    byte b[sizeof int];
    int i;
};
byteint bi;
bi.i = 1337;
for(int i = 0; i<4;i++)
    destination[i] = bi.b[i];

這將使字節數組和整數“重疊”(共享相同的內存)。 這可以用各種類型完成,只要字節數組與類型相同(否則其中一個字段不會受到另一個字段的影響)。 當你必須在整數操作和字節操作/復制之間切換時,將它們作為一個對象也很方便。

Int到byte,反之亦然。

unsigned char bytes[4];
unsigned long n = 1024;

bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = n & 0xFF;

printf("%x %x %x %x\n", bytes[0], bytes[1], bytes[2], bytes[3]);


int num = 0;
for(int i = 0; i < 4; i++)
 {
 num <<= 8;
 num |= bytes[i];
 }


printf("number %d",num);

您可以通過anding和shift操作獲得單個字節:

byte1 =  nint & 0x000000ff
byte2 = (nint & 0x0000ff00) >> 8
byte3 = (nint & 0x00ff0000) >> 16
byte4 = (nint & 0xff000000) >> 24
std::vector<unsigned char> intToBytes(int value)
{
    std::vector<unsigned char> result;
    result.push_back(value >> 24);
    result.push_back(value >> 16);
    result.push_back(value >>  8);
    result.push_back(value      );
    return result;
}

int(或任何其他數據類型)已經作為字節存儲在內存中。 那么為什么不直接復制內存呢?

memcpy(arrayOfByte, &x, sizeof x);

一個簡單優雅的襯墊,也可以與任何其他數據類型一起使用。



如果需要反轉字節,可以使用std :: reverse

memcpy(arrayOfByte, &x, sizeof x);
std::reverse(arrayOfByte, arrayOfByte + sizeof x);

或者更好的是,只需將字節反向復制即可

BYTE* p = (BYTE*) &x;
std::reverse_copy(p, p + sizeof x, arrayOfByte);

如果您根本不想復制數據,只需要使用其字節表示

BYTE* bytes = (BYTE*) &x;
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));

:d

我知道這個問題已有答案,但我會解決這個問題。 我正在使用模板函數和整數約束。

這是我的解決方案:

#include <type_traits>
#include <vector>

template <typename T,
          typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
std::vector<uint8_t> splitValueToBytes(T const& value)
{
    std::vector<uint8_t> bytes;

    for (size_t i = 0; i < sizeof(value); i++)
    {
        uint8_t byte = value >> (i * 8);
        bytes.insert(bytes.begin(), byte);
    }

    return bytes;
}

我希望我的幫助

template <typename t_int>
std::array<uint8_t, sizeof (t_int)> int2array(t_int p_value) {
    static const uint8_t _size_of (static_cast<uint8_t>(sizeof (t_int)));
    typedef std::array<uint8_t, _size_of> buffer;
    static const std::array<uint8_t, 8> _shifters = {8*0, 8*1, 8*2, 8*3, 8*4, 8*5, 8*6, 8*7};

    buffer _res;
    for (uint8_t _i=0; _i < _size_of; ++_i) {
        _res[_i] = static_cast<uint8_t>((p_value >> _shifters[_i]));
    }
    return _res;
}

暫無
暫無

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

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