C++ C++;使用无符号字符而不是typedef unsigned\uu int8
我正在使用一个我非常喜欢的类:C++ C++;使用无符号字符而不是typedef unsigned\uu int8,c++,class,typedef,encryption,C++,Class,Typedef,Encryption,我正在使用一个我非常喜欢的类: #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);
#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;
}
#pragma一次
#包括“StdAfx.h”
#包括“xorcipher.h”
#包括
使用名称空间std;
typedef无符号整数8字节;
向量键;
CXORcipher::CXORcipher()
{
m_键。推回(0x55);
m_键。推回(0xae);
m_键。推回(0x8c);
m_键。推回(0x14);
}
CXORcipher::~CXORcipher()
{
}
向量异或加密解密(const vector&uInput)
{
向量ret;
ret.reserve(uInput.size());
vector::const_iterator keyiiterator=m_Key.begin();
对于(vector::const_迭代器inputIterator=uInput.begin();inputIterator!=uInput.end();++inputIterator)
{
ret.push_back(*输入迭代器^*键迭代器);
//前进键迭代器,换行开始。
如果(++keyIterator==m_Key.end())
{
keyIterator=m_Key.begin();
}
}
返回ret;
}
但是,我想输入一个无符号字符的向量,而不是typedef。
我对C++还不是很坚定,我害怕把事情搞糟。
谁能告诉我我能做什么
谢谢大家! 我想当你传递一个向量的时候,它会马上出现,因为字节是通过定义为_int8的typedef传递的,在我看来,它与无符号字符是一样的 否则您可以做什么:
uint8\u t
,这是一个8位无符号整数,通常称为无符号字符
例如:
#include <cstdint>
typedef uint8_t BYTE;
#包括
typedef uint8_t字节;
\uuu int8
似乎是Microsoft特有的关键字。(它不是typedef
;如果它是unsigned\uu int8
,将是一个语法错误。)
定义字节
或字节
类型的合理方法是将无符号字符
定义为typedef。或者您可以直接使用无符号字符
,而不使用类型定义
;任何C或C++程序员都会将它识别为一个无符号类型,其大小正好为1字节。
如果您愿意假定字符位==8
,则可以使用中定义的uint8\u t
,这是非常常见但不通用的。(C和C++中的一个字节是一个代码< char < /C> >的大小,至少8位,但可以更多。)
我不知道unsigned char
和unsigned\u int8
是否为同一类型,但我怀疑它们是相同的。在任何情况下,使用\uuu int8
都会使您的代码不可移植。(如果你不需要将它移植到非微软平台上,这不一定是致命的缺陷,但是更多的人能够帮助你使用标准的C++而不是微软专用的C++)。
在对的评论中,您写道:
不幸的是,一个简单的替代品不适合我。这个
有问题的一行是:for(vector::const_iterator inputIterator=uInput.begin();inputIterator=
uInput.end();++inputierator)
我没有用于
无符号字符
请更新您的问题,添加显示问题和确切错误消息的代码。(不要删除现有代码,因为您已经有了引用它的答案。)我不明白:vector
已经是一个vector
。你能澄清你的问题吗?typedef
“弱”。如果您将typedef
A
作为B
使用,则可以互换使用A
和B
。不幸的是,简单的替换对我不起作用。有问题的一行是:对于(vector::const_iterator inputIterator=uInput.begin();inputIterator!=uInput.end();++inputIterator),我没有任何用于无符号字符的迭代器。错误消息是什么?您是否也将uInput更改为const vector&?