2013-10-11 55 views
-1

我使用我很喜歡的一類:C++使用unsigned char型的,而不是無符號的typedef __int8

#pragma once 
#include "StdAfx.h" 
#include "xorcipher.h" 
#include <vector> 

using namespace std; 

typedef unsigned __int8 BYTE; 

    vector<BYTE>m_Key; 

    CXORcipher::CXORcipher() 
    { 
     m_Key.push_back(0x55); 
     m_Key.push_back(0xae); 
     m_Key.push_back(0x8c); 
     m_Key.push_back(0x14); 
    } 
    CXORcipher::~CXORcipher() 
    { 
    } 

    vector<BYTE> xor_encryptdecrypt(const vector<BYTE>&uInput) 
    { 
     vector<BYTE> ret; 
     ret.reserve(uInput.size()); 

     vector<BYTE>::const_iterator keyIterator = m_Key.begin(); 

     for(vector<BYTE>::const_iterator inputIterator = uInput.begin(); inputIterator != uInput.end(); ++ inputIterator) 
     { 
      ret.push_back(*inputIterator^*keyIterator); 

      // advance the key iterator, wrapping to begin. 
      if(++ keyIterator == m_Key.end()) 
      { 
       keyIterator = m_Key.begin(); 
      } 
     } 
     return ret; 
    } 

不過,我想喂unsigned char型的,而不是類型定義的載體。我對C++還不是很堅定,而且我害怕搞砸了。

有人能告訴我我能做什麼嗎?

謝謝!

+1

我不明白:'vector '已經是'vector '了。你能澄清你的問題嗎? –

+0

'typedef'是「弱」。如果'將'typedef''設爲'B',則可以交替使用'A'和'B'。 – BoBTFish

回答

0

我認爲它會立即傳遞一個向量,因爲BYTE是通過typedef定義爲__int8,在我的opionion中它與unsigned char相同。

,否則你可以做什麼:

  1. BYTE的所有出現替換爲unsigned char(這是你的代碼是安全的)
  2. 創建CXORcipher類的模板化版本,它與不同的數據類型的工作原理。
+0

不幸的是,一個簡單的替換不適合我。有問題的行是這一個:for(vector :: const_iterator inputIterator = uInput.begin(); inputIterator!= uInput.end(); ++ inputIterator)我沒有任何迭代器的無符號字符。 – tmighty

+0

什麼是錯誤信息?你是否也改變了uInput const vector &? – Matthias247

1

您可以#include並使用uint8_t,它是一個8位無符號整數,通常被稱爲unsigned char

例子:

#include <cstdint> 
typedef uint8_t BYTE; 
1

__int8似乎是一個Microsoft特定的關鍵字。 (它不是一個typedef;如果它是unsigned __int8將是一個語法錯誤。)

合理的方式來定義一個或byte類型​​是作爲unsigned char一個typedef。或者您可以直接使用unsigned char而不需要typedef;任何C或C++程序員都會將其識別爲一個大小恰好爲1個字節的無符號類型。

您可以使用uint8_t,在<cstdint>定義,如果你願意承擔CHAR_BIT == 8,這是很常見的,但不具有普遍性。 (在C和C++ A「字節」是char的大小,這是至少 8位,但可以更多。)

不知unsigned charunsigned __int8是否是相同類型,但我懷疑他們是。無論如何,使用__int8會使您的代碼不可移植。 (如果您不需要將它移植到非Microsoft平臺上,這不一定是致命的缺陷,但更多的人可以使用標準C++來幫助您,而不是使用特定於Microsoft的C++。)

在對Matthias247's answer ,你寫道:

不幸的是,一個簡單的替換不適合我。 有問題的行是這一個:for(vector<unsigned char>::const_iterator inputIterator = uInput.begin(); inputIterator != uInput.end(); ++ inputIterator)我沒有任何迭代器爲 unsigned char

請更新您的問題,添加代碼,展示問題和確切的錯誤信息。 (不要刪除現有的代碼,因爲你已經有了指向它的答案。)

相關問題