我想在套接字上發送文件,並且需要在前四個字節中傳遞其長度。在ruby中將整數轉換爲字符串
這是我想在C++做的:
struct
{
int lenght; //four bytes
char msg[40];
}dataBuf;
write(fd, &databuf, sizeof(dataBuf))
如何推動一個整數到一個插座,所以它接收它作爲一個整數,在另一端,而不是作爲一個ASCII值?
我不想像「\x04\X03
」那樣對其進行硬編碼,我試圖用pack(L*)
來完成。這隻適用於數組,我沒有辦法將我的四字節整數分成四個字節的數組。
這是你想要做的嗎? http://stackoverflow.com/questions/941856/write-binary-file-in-ruby – HeatfanJohn 2012-07-24 21:00:36