2016-11-03 443 views
0

我試圖將整數轉換爲byte(aka unsigned char)數組,以通過C++中的TCP Stream發送數組,反之亦然。C++ |將int轉換爲byte [4],反之亦然

我已經嘗試了很多解決方案在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); 
} 

每次我執行這個代碼,我得到的結果「4202308」不管我設置爲「整型數字」。

任何幫助,將不勝感激。

UPDATE:從

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; 
} 

摘錄主():

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"; 
+0

您確定您的機器的排序嗎? –

+0

很難判斷代碼中缺少的註釋何時不顯示結果數組應該存在的位置 - 可以將其地址/參考作爲參數提供給它,也可以動態分配它。考慮改變函數的名稱 - 至少是「... Bytes ...()'。 – greybeard

+0

在這種情況下,這應該是沒有問題的,因爲我在同一臺機器上來回移動相同的字節。但只要我通過TCP流傳輸數據,這可能會有問題。 – eheller

回答

2

intToByte功能是其主體的範圍內分配byte[4],然後返回一個指向它的指針。

因此,只要函數返回並且所有調用者接收到的值都是指向當前無效位置的指針,值就會被丟棄 - 當值超出範圍並且指針不延長該生存期時,值將被銷燬。

要麼使用一個標準集裝箱的物體,如std::arraystd::vector,你的函數應該返回給調用者或替代,已經intToByte接受byte[4]/byte*作爲參數,並填寫好。

爲了完整...你也可以使用new來創建字節數組,但是你必須記住它到delete[]它,雖然在這種情況下似乎很容易做到,但當你沒有充足的理由做動態分配。

此外,該聲明x & y >> z將首先執行y >> z然後位與與x的結果,這當然不是你想要的。

+0

非常感謝!這是有道理的,我現在記得你必須通過參數傳遞參考。這也可以解釋爲什麼sizeof行總是返回8而不是4 – eheller

+0

我認爲你的意思是*通過引用傳遞參數* - 雖然這不適用於此,因爲一切都是按值傳遞的,因爲雖然指針可以被認爲是一個參考,但你真的只是傳遞指針的值。儘管如此,你也可以接受'byte(&)[4]'而不是'byte *'來解決問題。儘管如果'std :: array'是一個選項,就使用它。 – Olipro

+0

不幸的是,將簽名更改爲void void intToByte(int n,byte * result)並填充數組並沒有真正的幫助。也許我的轉換邏輯中仍然存在問題。我會嘗試使用std :: array,但我想知道爲什麼傳遞一個字節*作爲參數不應該工作。 – eheller

0

不幸的是,這不是一個確切的答案給你的問題,但可能會有所幫助。 YOu可以像下面的代碼那樣做,我認爲。

#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; 
} 
+0

是否解決了這個問題,但恰巧我所說的是Bad Thing(TM)所要做的。雖然我很想知道爲什麼你認爲傳遞'int'而不是'int'更好。 – Olipro

+0

它是const int&not int& –

+0

是的,爲了簡潔起見,我省略了它,因爲這與我的問題無關,我對你的const正確性毫無疑問。 – Olipro

1

第一點: 你是從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; 
+0

謝謝!使用結果數組作爲參數後,丟失的括號是問題。我以某種方式沒有想到這一點 – eheller

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)); 
} 
相關問題