簡體   English   中英

C ++ | 將int轉換為byte [4],反之亦然

[英]C++ | Convert int to byte[4] and vice versa

我試圖將整數轉換為字節(也稱為無符號字符)數組,以在C ++中通過TCP流發送該數組,反之亦然。

我已經嘗試過許多關於stackoverflow和自己的想法的解決方案,但是似乎沒有什么真正適合我。

我的最后一個解決方案如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "tcpconnector.h"

typedef unsigned char byte;

using namespace std;

/*
char* byteToChar(byte* b, int length) {
    char c[length];
    for (int i = 0; i < length; i++) {
    c[i] = b[i] - 128;
    }
    return c;
}

byte* charToByte(char* c, int length) {
    byte b[length];
    for (int i = 0; i < length; i++) {
    b[i] = c[i] + 128;
    }
    return b;
}
*/

byte* intToByte(int n) {

    byte byte[4];

     byte[0] = n & 0x000000ff;
     byte[1] = n & 0x0000ff00 >> 8;
     byte[2] = n & 0x00ff0000 >> 16;
     byte[3] = n & 0xff000000 >> 24; 

     return byte;
}

int byteToInt(byte* byte) {

    int n = 0;

    n = n + (byte[0] & 0x000000ff);
    n = n + ((byte[1] & 0x000000ff) << 8);
    n = n + ((byte[2] & 0x000000ff) << 16);
    n = n + ((byte[3] & 0x000000ff) << 24);


    return n;
}

int main(int argc, char** argv)
{
    if (argc != 3) {
        printf("usage: %s <port> <ip>\n", argv[0]);
        exit(1);
    }

    int number = 42;
    byte* line = intToByte(number);

    cout << "Number: " << number << "\n";
    cout << "ArrayLength: " << sizeof line << "\n";
    cout << "Array: " << line << "\n";
    cout << "Array to Number: " << byteToInt(line) << "\n";

/*

    TCPConnector* connector = new TCPConnector();
    TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
    if (stream) {
        stream->send(byteToChar(line, 4), 4);
        delete stream;
    }

*/
    exit(0);
}

每當我執行此代碼時,無論我設置為“ int number”如何,都將得到結果“ 4202308”。

任何幫助,將不勝感激。

更新:

void intToByte(int n, byte* result) {

     result[0] = n & 0x000000ff;
     result[1] = n & 0x0000ff00 >> 8;
     result[2] = n & 0x00ff0000 >> 16;
     result[3] = n & 0xff000000 >> 24; 
}

來自main()的節選:

int number = 42;
byte line[4];
intToByte(number, line);

cout << "Number: " << number << "\n";
cout << "ArrayLength: " << sizeof line << "\n";
cout << "Array: " << line << "\n";
cout << "Array to Number: " << byteToInt(line) << "\n";

您的intToByte函數在其主體范圍內分配一個byte[4] ,然后返回一個指向它的指針。

因此,函數返回后立即丟棄該值,並且調用者收到的所有內容都是指向現在無效位置的指針-值超出范圍時銷毀,並且指針不會延長該生存期。

使用標准容器對象,例如std::arraystd::vector ,您的函數應將其返回給調用者,或者讓intToByte接受byte[4] / byte*作為參數並填寫。

為了完整起見,您還可以讓該函數使用new創建字節數組,但隨后您必須記住delete[]它,盡管在這種情況下似乎很容易做到,但通常在以下情況下是不好的做法您沒有充分的理由進行動態分配。

另外,語句x & y >> z將首先執行y >> z然后按位與x ,這當然不是您想要的。

第一點:byte* intToByte(int n)返回的緩沖區不安全。 您正在返回本地數組的基地址。 傳遞字節數組作為輸入或在堆中分配字節並返回地址

第二點:

考慮運算符優先級。

 byte byte[4];
 byte[0] = n & 0x000000ff;
 byte[1] = ( n & 0x0000ff00 ) >> 8;
 byte[2] = ( n & 0x00ff0000 ) >> 16;
 byte[3] = ( n & 0xff000000 ) >> 24; 

不幸的是,這不是您問題的確切答案,但可能會有所幫助。 我可以按照下面的代碼來做。

#include <stdio.h>
using namespace std;

unsigned char* intToByte(const int& N) {

    unsigned char* byte = new unsigned char[4];

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

    return byte;
}


int main()
{
    unsigned char * result = intToByte(255);
    printf("%x %x %x %x\n", (unsigned char)result[0], (unsigned char)result[1],
                            (unsigned char)result[2], (unsigned char)result[3]);

    delete result;
    return 0;
}

此函數將c ++中的所有標准類型轉換為字節向量。

template <typename T>
std::vector<byte> ToByte(T input) 
{
    byte* bytePointer = reinterpret_cast<byte*>(&input);
    return std::vector<byte>(bytePointer, bytePointer + sizeof(T));
}

暫無
暫無

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

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