[英]How can I get a hex value in c++ to a proper string?
I am still working on a database for movies and I would like to show the user what he has input to the file. 我仍在为电影数据库工作,我想向用户展示他对文件的输入。
However when i use cout << lisafilm <<
it provides me with hex value. 但是,当我使用
cout << lisafilm <<
它为我提供了十六进制值。 Therefore, I need to conver hex to string. 因此,我需要将十六进制转换为字符串。
Snippet of trouble. 麻烦片段。
void sisend()
{
string nimi;
int aasta;
long int hinne;
string vaadatud;
ofstream lisafilm("andmebaas.txt", ios::app);
cout <<"Sisestage filmi nimi." << endl;
cin >> nimi;
cout << "Sisestage filmi aasta." << endl;
cin >> aasta;
cout << "Sisestage filmi hinne." << endl;
cin >> hinne;
cout << "Kas olete filmi juba vaadanud?" << endl;
cout << "Vastake 'Jah' voi 'Ei'" << endl;
cin >> vaadatud;
lisafilm<< nimi << " " << aasta << " " << hinne<< " " << vaadatud << endl;
lisafilm.close();
{
system("CLS");
int hex_str = lisafilm ;
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
main();
}
when i use
cout << lisafilm
it provides me with hex value当我使用
cout << lisafilm
它为我提供了十六进制的价值
This is because you are trying to output an ofstream
. 这是因为您试图输出一个
ofstream
。 When this happens, operator void*
gets called, producing an arbitrary hex sequence which is tied to your stream, but ultimately is very much useless. 发生这种情况时,将调用
operator void*
,产生与您的流相关的任意十六进制序列,但最终几乎没有用。
Try this: 尝试这个:
std::stringstream ss;
ss << std::hex << lisafilm;
const std::string s = ss.str();
lisafilm is a stream , not a string lisafilm是流 ,不是字符串
If you want to copy lisafilm to cout something like cout << lisafilm.rdbuf();
如果要复制lisafilm到cout中,例如
cout << lisafilm.rdbuf();
would do the trick (assuming that lisafilm is an ostream or istream and that lisafilm's position is the start of the file. 会达到目的(假设lisafilm是ostream或istream,而lisafilm的位置是文件的开头。
Your code is very poorly formatted, I don't think what you posted would compile. 您的代码格式很差,我认为您发布的内容无法编译。 If you clean it up stackoverflow may be able to help you more.
如果您清理它,stackoverflow也许可以为您提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.