2011-03-17 11 views
0
string convert_binary_to_hex(string binary_value) 
{ 
    bitset<8> set(binary_value);  
    cout << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return 
    return ""; 
} 

私は以前にCをしていません。 = DC++:何かがコンソールに送信されています:代わりに、それを変数に保存するにはどうしたらいいですか?

EDIT: ユーザーが提案しostringstream:、私の輸入も

main.cpp:20: error: aggregate ‘std::ostringstream result’ has incomplete type and cannot be defined 

#include <stdio.h> 
#include <iostream> 
#include <string> 
#include <bitset> 


using namespace std; 

答えて

3

書き込み、それをすることが、今、このエラーを与える

ostringstream result; 
bitset<8> set(binary_value);  
result << hex << set.to_ulong() << endl; 
return result.str(); 

が、 std::ostringstreamのインスタンス:

string convert_binary_to_hex(string binary_value) 
{ 
    bitset<8> set(binary_value);  
    ostringstream oss; 
    oss << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return 
    return oss.str(); 
} 
+0

どういう意味ですか? – NullVoxPopuli

+0

のような... cstの代わりにostringstream varName? – NullVoxPopuli

+0

@DerNalia私は答えを広げました。 – NPE

0

この回答を参照してください:

C++ equivalent of sprintf?

をそしてout変数を使用しますが、ここでcoutを使用しているように、代わりの

std::cout << out.str() << '\n'; 

は、あなただけの

return out.str(); 
を行うだろう
関連する問題