0
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    int base10;
    long base2 = 0;
    base10 = Convert::ToInt32(txtBase10->Text);
    base2 = base10 / 128 * 10000000;
    base10 %= 128;

    base2 = base10 / 64 * 1000000;
    base10 %= 64;

    base2 = base10 / 32 * 100000;
    base10 %= 32;

    base2 = base10 / 16 * 10000;
    base10 %= 16;

    base2 = base10 / 8 * 1000;
    base10 %= 8;

    base2 = base10 / 4 * 100;
    base10 %= 4;

    base2 = base10 / 2 * 10;
    base10 %= 2;

    base2 = base10 / 1 * 1;
    base10 %= 1;
}
4

2 に答える 2

3

バイナリ形式の数値が何であるかを理解していません。整数を STRING 表現に変換する必要がある場合は、次のようにします。

String base2 = Convert.ToInt32(str,2).ToString();
String base8 = Convert.ToInt32(str,8).ToString();
String base10 = Convert.ToInt32(str,10).ToString();
String base16 = Convert.ToInt32(str,16).ToString();
于 2013-05-08T14:36:59.040 に答える
0

String ではなく long として使用する場合は、これを関数の本体として使用すると、パフォーマンスが向上します。

int base10 = Convert::ToInt32(txtBase10->Text);
long base2 = 0;

for (int mask = 0x80; mask != 0; mask >>= 1)
{
    base2 *= 10;
    if (base10 & mask) ++base2;
}

// base2 now has the number you want.

これは、0 から 255 (65 を含む) の間のいくつかの値を簡単にテストするイデオンです。

于 2013-05-08T14:48:57.257 に答える