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;
}
问问题
169 次
2 回答
3
你不明白什么是二进制形式的数字。如果您需要将任何整数转换为它的字符串表示,您可以执行以下操作:
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
If you want it as a long and not a String, you can use this as the body of your function, it will perform better:
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.
Here's an ideone with a brief testing of some values between 0 and 255 (including 65).
于 2013-05-08T14:48:57.257 回答