如果不想自己自己寫,可以用c++提供的函式bitset完美轉2進制出來喔

include <bitset>

 

msdn的範例

// bitset_to_string.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   string s1;
   s1 =  b1.template to_string<char, 
   char_traits<char>, allocator<char> >( );
   cout << "The string returned from the bitset b1"
        << "\n by the member function to_string( ) is: "
        << s1 << "." << endl;
}

 

範例是主控台的程式碼,我要用在windows form視窗環境就改用下面那樣


if(radioButton1->Checked&&textBox7->Text!="") //10轉2
{
bitset<15> b1 ( int::Parse(textBox7->Text) );
string *s1=new string ;
*s1 = b1.template to_string<char,char_traits<char>, allocator<char> >( );
String^ str2 = gcnew String(s1->c_str());
textBox8->Text=str2;
delete str2;
delete s1;
}
, , ,
創作者介紹

宇若彎彎

周宇若 發表在 痞客邦 PIXNET 留言(0) 人氣()