C++ C++;像这样将数组从char转换为unsigned int是否合适和安全?

C++ C++;像这样将数组从char转换为unsigned int是否合适和安全?,c++,C++,目标:正确快速地将数组从char转换为unsigned int 检查我的工作-请: ... // NOTE: // m_chFileBuffer is a member/variable from a class. // m_nFileSize is also a member/variable from a class. // iFile is declared locally as std::ifstream // Calculate the size of iFile and copy

目标:正确快速地将数组从char转换为unsigned int

检查我的工作-请:

...
// NOTE:
// m_chFileBuffer is a member/variable from a class.
// m_nFileSize is also a member/variable from a class.
// iFile is declared locally as std::ifstream

// Calculate the size of iFile and copy the calculated
// value to this->m_nFileSize
iFile.seekg( 0, std::ios::end );
this->m_nFileSize = iFile.tellg( );
iFile.seekg( 0, std::ios::beg );

// Declare this->m_chFileBuffer as a new char array
this->m_chFileBuffer = new char[ this->m_nFileSize ];

// Read iFile into this->m_chFileBuffer
iFile.read( this->m_chFileBuffer, this->m_nFileSize );

// Declare a new local variable
::UINT *nFileBuffer = new ::UINT[ this->m_nFileSize ];

// Convert this->m_chFileBuffer from char to unsigned int (::UINT)
// I might be doing this horribly wrong, but at least I tried and
// will end up learning from my mistakes!
for( ::UINT nIndex = 0; nIndex != this->m_nFileSize; nIndex ++ )
{
    nFileBuffer[ nIndex ] = static_cast< ::UINT >( this->m_chFileBuffer[ nIndex ] );

    // If defined DEBUG, print the value located at nIndex within nFileBuffer
    #ifdef DEBUG
    std::cout << nFileBuffer[ nIndex ] << ' ';
    #endif // DEBUG
}

// Do whatever with nFileBuffer
...

// Clean-up
delete [ ] nFileBuffer;
。。。
//注:
//m_chFileBuffer是类中的成员/变量。
//m_nFileSize也是类的成员/变量。
//iFile在本地声明为std::ifstream
//计算iFile的大小并复制计算出的
//此->m\u nFileSize的值
iFile.seekg(0,标准::ios::结束);
this->m_nFileSize=iFile.tellg();
i文件seekg(0,标准::ios::beg);
//将此->m_chFileBuffer声明为新的字符数组
this->m_chFileBuffer=新字符[this->m_nFileSize];
//将iFile读入此->m\u chFileBuffer
读取(这个->文件缓冲区,这个->文件大小);
//声明一个新的局部变量
::UINT*nFileBuffer=new::UINT[this->m_nFileSize];
//将此->m_chFileBuffer从char转换为unsigned int(::UINT)
//我可能做错了,但至少我试着
//最终会从我的错误中吸取教训!
对于(::UINT nIndex=0;nIndex!=this->m\u nFileSize;nIndex++)
{
nFileBuffer[nIndex]=static_cast<::UINT>(this->m_chFileBuffer[nIndex]);
//如果已定义调试,则打印位于nFileBuffer中nIndex处的值
#ifdef调试

对于这样一个简单的任务来说,代码太多了,您所需要的就是这个

std::vector <unsigned int> v;
std::copy (std::istream_iterator <char> (iFile), 
           std::istream_iterator <char> (), 
           std::back_inserter (v));
std::vector v;
std::copy(std::istream_迭代器(iFile),
std::istream_迭代器(),
标准:背部插入器(v);
甚至更短(感谢@111111):

std::vector v { std::istream_迭代器(iFile), std::istream_迭代器() };
对于这样一个简单的任务来说,代码太多了,您所需要的就是这个

std::vector <unsigned int> v;
std::copy (std::istream_iterator <char> (iFile), 
           std::istream_iterator <char> (), 
           std::back_inserter (v));
std::vector v;
std::copy(std::istream_迭代器(iFile),
std::istream_迭代器(),
标准:背部插入器(v);
甚至更短(感谢@111111):

std::vector v { std::istream_迭代器(iFile), std::istream_迭代器() };
矢量buf(文件大小);
/*将文件读取到&buf[0]*/
向量单位(buf.size());
复制(buf.begin(),buf.end(),uints.begin());
您的原始new/delete用法不是异常安全的。经验法则:只要您自己没有编写析构函数,就不要在代码中写入delete。此外,“char”可能是有符号的,不确定您在那里期望的是什么行为。

vector buf(文件大小);
/*将文件读取到&buf[0]*/
向量单位(buf.size());
复制(buf.begin(),buf.end(),uints.begin());


您的原始new/delete用法不是异常安全的。经验法则:只要您自己没有编写析构函数,就不要在代码中写入delete。还有,“char”可能是已签名的,不确定您想要的是什么行为。

不确定您想要的是什么,但是您显示的代码没有任何安全性,所有原始拥有的指针都可能泄漏。如何修复此问题???D:文件的内容是什么,以及
nFileBuffer
的所需内容是什么?@111111我正在尝试o开发一种新的文件加密算法。基本上,它加载文件并分析加载文件中每个字节的十进制值。这些字节从char转换为unsigned int(nFileBuffer)我不知道你到底在追求什么,但是你展示的代码没有什么安全性,所有那些原始的指针都容易泄漏。我如何解决这个问题???D:文件的内容是什么,以及
nFileBuffer
的期望内容是什么?@111111我正在尝试开发一种新的文件加密算法。基本上,它加载文件并分析加载文件中每个字节的十进制值。从char转换为unsigned int(nFileBuffer)或更好的方法是使用std::vectors迭代器构造函数。
std::vector v{std::istream_迭代器(iFile),std::istream_迭代器()}
@aleguna我真不敢相信这么简单!谢谢。我会在大约2分钟内接受这个答案。或者最好使用std::vectors迭代器构造函数。
std::vector v{std::istream_迭代器(iFile),std::istream_迭代器()}
@aleguna我真不敢相信这么简单!谢谢。我会在大约2分钟内接受这个答案。等等,如果我在类函数块中局部声明了一个数组,我可以在解构器中删除它?你确定吗?O。o@CLearner不知道你是从哪里得到的,但是:不。当然不是。我被你读到的东西弄糊涂了。我知道ow.谢谢你提供的信息。我不知道为什么你的帖子是-1,但是+1。@CLearner重点是:使用容器/智能指针类,让它们管理你的资源。因为这些对象的析构函数总是在你离开作用域时被调用,而不考虑方法。(返回,异常)等等,如果我在一个类函数的块中本地声明了一个数组,我可以在解构器中删除它?你确定吗?O。o@CLearner不知道你是从哪里得到的,但是:不。当然不是。我对你读的内容感到困惑。我知道。谢谢你提供的信息。不知道为什么你的帖子是-1,但我是+1。@CLearner重点是:使用container/智能指针类,并让它们管理您的资源。因为在您离开作用域时,无论使用何种方法,都会调用这些对象的析构函数。(返回,异常)
vector<char> buf(file_size);
/* read file to &buf[0] */
vector<unsigned int> uints(buf.size());
copy(buf.begin(), buf.end(), uints.begin());