2013-12-21 76 views
4

http://prntscr.com/2ctnozC++ WSAsend包的奇怪的十六進制轉儲

我鉤住WSAsend功能和傾倒的數據包。 ASCII轉儲工作,但十六進制轉儲有時顯示的東西,你可以看到在屏幕上(FFFFFFDD),任何想法爲什麼?

代碼:

int WINAPI myWSASend(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesSent, DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) 
{ 
    //Packet Log 
    if (bLogPacketS == TRUE) 
    { 
     for (unsigned int i = 0; i < lpBuffers->len; i = i + 8) 
     { 
      printf("%02X %02X %02X %02X %02X %02X %02X %02X\t\t%c %c %c %c %c %c %c %c\n", 
        (unsigned int)lpBuffers->buf[i], (unsigned int)lpBuffers->buf[i+1], (unsigned int)lpBuffers->buf[i+2], 
        (unsigned int)lpBuffers->buf[i+3], (unsigned int)lpBuffers->buf[i+4], (unsigned int)lpBuffers->buf[i+5], 
        (unsigned int)lpBuffers->buf[i+6], (unsigned int)lpBuffers->buf[i+7], 
        (drawable((unsigned int)lpBuffers->buf[i])) ? (unsigned int)lpBuffers->buf[i] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+1])) ? (unsigned int)lpBuffers->buf[i+1] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+2])) ? (unsigned int)lpBuffers->buf[i+2] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+3])) ? (unsigned int)lpBuffers->buf[i+3] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+4])) ? (unsigned int)lpBuffers->buf[i+4] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+5])) ? (unsigned int)lpBuffers->buf[i+5] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+6])) ? (unsigned int)lpBuffers->buf[i+6] : '.', 
        (drawable((unsigned int)lpBuffers->buf[i+7])) ? (unsigned int)lpBuffers->buf[i+7] : '.'); 
     } 
     printf("\n\n"); 
    } 
    return (oWSASend)(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpOverlapped, lpCompletionRoutine); 
} 

bool drawable(unsigned int value) 
{ 
    if (value > 32 && value < 127) 
     return true; 
    else 
     return false; 
} 
+6

是否有某種'年度printf'比賽正在進行? –

+0

如果是這樣,我可能會贏,我知道它不是最好的辦法,但我不能想到更好的方式 – LemoniscooL

+0

@MartinJames:這不是*那*壞... –

回答

3

你鑄造錯誤的類型。

您只想更改簽名,但您也將每個字節擴展爲四字節的單詞。在有符號到無符號轉換(以及由此產生的負值環繞)的情況下,這會導致極高的值。


每個lpBuffers->buf[i] 「元素」 是一個char,但你鑄造unsigned int。如果您的char已在您的系統上簽名,則0xDD不在該類型的範圍的頂部,因此它包裝到-35。然後將其轉換爲unsigned int,結果爲0xFFFFFFDD

printf說明符%02X不會截斷這個。

大概你想解釋所有的字節爲unsigned,以獲得完整的0x000xFF範圍。我個人會轉換爲unsigned char(而不是unsigned int),其中值0xDD221

在下面的代碼中,我還對環路條件進行了安全調整。

for (unsigned int i = 0; i < lpBuffers->len-8; i = i + 8) 
//           ^^ 
{ 
    printf(
     "%02X %02X %02X %02X %02X %02X %02X %02X" 
      "\t\t%c %c %c %c %c %c %c %c\n", 

     static_cast<unsigned char>(lpBuffers->buf[i]), 
     static_cast<unsigned char>(lpBuffers->buf[i+1]), 
     static_cast<unsigned char>(lpBuffers->buf[i+2]), 
     static_cast<unsigned char>(lpBuffers->buf[i+3]), 
     static_cast<unsigned char>(lpBuffers->buf[i+4]), 
     static_cast<unsigned char>(lpBuffers->buf[i+5]), 
     static_cast<unsigned char>(lpBuffers->buf[i+6]), 
     static_cast<unsigned char>(lpBuffers->buf[i+7]), 
     (drawable(lpBuffers->buf[i])) ? static_cast<unsigned char>(lpBuffers->buf[i]) : '.', 
     (drawable(lpBuffers->buf[i+1])) ? static_cast<unsigned char>(lpBuffers->buf[i+1]) : '.', 
     (drawable(lpBuffers->buf[i+2])) ? static_cast<unsigned char>(lpBuffers->buf[i+2]) : '.', 
     (drawable(lpBuffers->buf[i+3])) ? static_cast<unsigned char>(lpBuffers->buf[i+3]) : '.', 
     (drawable(lpBuffers->buf[i+4])) ? static_cast<unsigned char>(lpBuffers->buf[i+4]) : '.', 
     (drawable(lpBuffers->buf[i+5])) ? static_cast<unsigned char>(lpBuffers->buf[i+5]) : '.', 
     (drawable(lpBuffers->buf[i+6])) ? static_cast<unsigned char>(lpBuffers->buf[i+6]) : '.', 
     (drawable(lpBuffers->buf[i+7])) ? static_cast<unsigned char>(lpBuffers->buf[i+7]) : '.' 
    ); 
} 
+0

即使沒有將它們轉換爲unsigned int,問題也是一樣的,我不知道爲什麼我的數據流中有負值。通常不應該有任何 – LemoniscooL

+0

@ user3125183:再次閱讀。你應該轉換成'unsigned char'。對於已簽名的數據類型,負值對於無符號數據類型成爲高正值,並且關鍵是_vice versa_。 –

+0

嗯至少你的我的printf的「版本」看起來更好:D虐待它試試 – LemoniscooL