C++ C++;使用字符串函数循环
我正在涂鸦SHA-256的实现。我正在尝试编写一个程序,生成sha(0),sha(1)。。。但我不能。我天真地尝试了C++ C++;使用字符串函数循环,c++,string,for-loop,hash,C++,String,For Loop,Hash,我正在涂鸦SHA-256的实现。我正在尝试编写一个程序,生成sha(0),sha(1)。。。但我不能。我天真地尝试了 #include <iostream> #include "sha256.h" int main(int argc, char *argv[]){ for (int i=0; i < 4; i++) std::cout << sha256("i"); return 0; } #包括 #包括“sha256.h” intma
#include <iostream>
#include "sha256.h"
int main(int argc, char *argv[]){
for (int i=0; i < 4; i++)
std::cout << sha256("i");
return 0;
}
#包括
#包括“sha256.h”
intmain(intargc,char*argv[]){
对于(int i=0;i<4;i++)
std::cout您需要将数字i
转换为SHA接受的字符串i
。一个简单的选项是使用C++11函数
std::cout << sha256(std::to_string(i));
将sha256(“i”)
更改为sha256(i)
@下划线不正确,因为SHA需要一个字符串。谢谢!我想现在我要做的就是修复“to_字符串不是std的成员”glitch,hm.@ErikVesterlund如果你使用gcc,请确保使用-std=c++11
编译以启用c++11支持。如果你没有c++11编译器,请使用我发布的链接中的转换方法。我正在用代码块编写这些内容,它是否使用gcc?我应该在哪里编写?谢谢。我现在已经尝试了所有这些,但我还是找不到t to work.to_string相同的问题(甚至在选中编译器设置中的相关框后重新启动代码块)itoa和stringstream也不起作用。std::stringstream
或itoa
肯定会起作用。确保你\include
用于std::stringstream
和\include
用于itoa
。尽管这是非标准的,但还是要尽量避免itoa
。活的例子。
std::cout << sha256(std::to_string(i));
#include <iostream>
#include <sstream>
#include "sha256.h"
int main()
{
std::string result;
std::stringstream ss;
for (int i = 0; i < 4; i++)
{
ss << i;
ss >> result;
ss.clear(); // need to clear the eof flag so we can reuse it
std::cout << sha256(result) << std::endl;
}
}