请问,怎样将十进制大数字符串(例如“365214563156465415613132153”,可能比INT32数据类型还大)转化成二进制字符串?谢谢
解决方案 »
- WEB程序更改服务器IP
- TCP/IP编程中怎么把字节流传输改为比特流传输?
- XML,看不懂
- 获取数据量大时 怎么优化?请前辈们指教!
- 如何在后台获取 __doPostBack('Button1','2222'); 中2222这个字符窜
- 重写抽象类方法提示错误: 并非所有的代码路径都返回值
- 如何在CS页面里面实现ASPX页面里面的<BODY ONLOAD="INIT(10)">
- vs.net2003 正则表达式验证是否有bug?(不能限定最大长度)
- 程序启动欢迎页面的问题??/
- 怎样在vs2003.net 中显示子窗口?初学者,谢谢!
- 再问一个正则表达的问题,昨天已问过了。但还没解决
- GridView如何改变列顺序
if (s.Length % 2==1 ){s="0"+s;}
string b = "";
for (i = 0; i < s.Length / 2; i++)
{
b += Convert.ToString(Convert.ToInt32(s.Substring(2 * i, 2)), 2);
}
MessageBox.Show(b);
string s = "123";
if (s.Length % 2==1 ){s="0"+s;}
string b = "";
for (i = 0; i < s.Length / 2; i++)
{
b += Convert.ToString(Convert.ToInt32(s.Substring(2 * i, 2)), 2);
}
MessageBox.Show(b);
出来的答案是b=110111 而实际上(123)Dec = (1111011)Bin
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 转化二进制 {
class Program {
static void Main(string[] args) {
Console.WriteLine(CastToBinaryFromInt((32+16+1).ToString()).BinaryData.ToArray<char>());
Console.Read();
}
public static bs CastToBinaryFromInt(string intValue) {
long v = default(long);
try { v = long.Parse(intValue); } catch {
throw new ArgumentOutOfRangeException();
}
bs[] mr = new bs[intValue.Length];//mr=middleResult
for (int i = 0 ; i < intValue.Length ; i++) {
switch (intValue[i]) {
case '0':
mr[i].Add(false);
break;
case '1':
mr[i].Add(true);
break;
case '2':
mr[i].Add(false);
mr[i].Add(true);
break;
case '3':
mr[i].Add(true);
mr[i].Add(true);
break;
case '4':
mr[i].Add(false);
mr[i].Add(false);
mr[i].Add(true);
break;
case '5':
mr[i].Add(true);
mr[i].Add(false);
mr[i].Add(true);
break;
case '6':
mr[i].Add(false);
mr[i].Add(true);
mr[i].Add(true);
break;
case '7':
mr[i].Add(true);
mr[i].Add(true);
mr[i].Add(true);
break;
case '8':
mr[i].Add(false);
mr[i].Add(false);
mr[i].Add(false);
mr[i].Add(true);
break;
case '9':
mr[i].Add(true);
mr[i].Add(false);
mr[i].Add(false);
mr[i].Add(true);
break;
default:
break;
}
var b = mr[i].Clone();
bs mr2 = default(bs);
List<bs> mr2l = new List<bs>();
List<bs> nos = new List<bs>();
for (int j2 = 0 ; j2 < b.Length ; j2++) {
if (b[j2]) {
bs no = new bs();
no.Add(true);
no = no >> j2;
nos.Add(no);
}
}
for (int j = 0 ; j < intValue.Length - 1 - i ; j++) {
for (int j2 = 0 ; j2 < nos.Count ; j2++) {
mr2l.Add(nos[j2] >> 1);
mr2l.Add(nos[j2] >> 3);
}
bs[] bbs = new bs[mr2l.Count];
mr2l.CopyTo(bbs);
mr2l.Clear();
nos.Clear();
nos.AddRange(bbs);
}
foreach (var item in nos) {
mr2 += item;
}
mr[i] = mr2.Clone();
}
bs result = default(bs);
foreach (var item in mr) {
result += item;
}
return result;
}
}
/// <summary>
/// binary data store
/// </summary>
struct bs {
class node {
public node next;
public int i;
public bool data;
public node(int i , bool b) {
this.i = i;
this.data = b;
}
}
node current , ;
public int Length;
public void Add(bool b) {
this.current = this.;
node n = new node(this.Length , b);
n.next = current;
current = n;
= current;
this.Length++;
}
public void AddToHead(bool b) {
node n = new node(this.Length , b);
n.next = current;
current = n;
= current;
this.Length++;
}
public IEnumerable<char> BinaryData {
get {
this.current = this.;
while (this.current != null) {
if (this.current.data) {
yield return '1';
} else {
yield return '0';
}
this.current = this.current.next;
}
}
}
public bool this[int i] {
get {
this.current = this.;
while (this.current != null) {
if (i == this.current.i) {
return this.current.data;
}
this.current = this.current.next;
}
throw new ArgumentOutOfRangeException("i必须在0到Length范围之中");
}
set {
this.current = this.;
while (this.current != null) {
if (i == this.current.i) {
this.current.data = value;
}
this.current = this.current.next;
}
}
}
public void Append() {
this.current = this.;
while (this.current.next != null) {
this.current.i++;
this.current = this.current.next;
}
this.current.i++;
this.current.next = new node(0 , false);
this.Length++;
} //
public static bs operator +(bs c , bs d) {
try {
bs r = new bs();
r = c.Length > d.Length ? c.Clone() : d.Clone();
int len = c.Length > d.Length ? d.Length : c.Length;
for (int i = 0 ; i < len ; i++) {
bool m1 , m2;
m1 = c[i]; m2 = d[i];
r[i] = m1 ^ m2;
int j = i;
while (m1 && m2 && j < r.Length) {
if (r.Length == c.Length) {
m1 = r[++j]; m2 = true;
} else if (r.Length == d.Length) {
m2 = r[++j]; m1 = true;
}
r[j] = m1 ^ m2;
if (j+1 == r.Length) {
r.Add(true); break;
}
}
}
return r;
} catch {
throw new ArgumentOutOfRangeException();
}
}
public static bs operator >>(bs n , int position) {
try {
bs b = n.Clone();
for (int i = 0 ; i < position ; i++) {
b.Append();
}
return b;
} catch {
throw new ArgumentOutOfRangeException();
}
} internal bs Clone() {
bs b = new bs() { Length = this.Length };
if (this. != null) {
this.current = this.;
b.current = b. = new node(this.current.i , this.current.data);
while (this.current.next != null) {
this.current = this.current.next;
var n = new node(this.current.i , this.current.data);
b.current = b.current.next = n;
}
}
return b;
}
}
try {
bs r = new bs();
r = c.Length >= d.Length ? c.Clone() : d.Clone();
int len = c.Length > d.Length ? d.Length : c.Length;
for (int i = 0 ; i < len ; i++) {
bool m1 , m2;
m1 = c[i]; m2 = d[i];
r[i] = m1 ^ m2;
int j = i;
while (m1 && m2 && j < r.Length) {
if (j + 1 < r.Length) {
if (r.Length == c.Length) {
m1 = r[++j]; m2 = true;
} else if (r.Length == d.Length) {
m2 = r[++j]; m1 = true;
}
r[j] = m1 ^ m2;
} else
r.Add(true); break;
}
}
return r;
} catch {
throw new ArgumentOutOfRangeException();
}
}
Console.Read();//
111001110
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string number = "11";//"365214563156465415613132153";
int i = 0,j;
const int next=1000000000;
List<int> splitnumber = new List<int>(); //将字符串转换成一块块长度不超过9的整数
StringBuilder newNumber=new StringBuilder(),tempNumber=new StringBuilder();
while (i < number.Length)
{
splitnumber.Add(int.Parse(number.Substring(i, 9-number.Length+i<0?9:number.Length-i)));
i+=9;
}
int count=splitnumber.Count;
while (count != 0) //将所给的数除2,余数加到stringBuilder里
{
for (i = 0; ; ++i)
{
j = splitnumber[i] & 1;
splitnumber[i] >>= 1;
if (i == count - 1)
{
tempNumber.Append(j);
break;
}
else
{
splitnumber[i + 1] += next * j;
}
}
if (splitnumber[0]==0)
{
splitnumber.RemoveAt(0);
--count;
}
}
//得到二进制数
for (i=tempNumber.Length-1;i>-1;--i)
{
newNumber.Append(tempNumber[i]);
}
Console.WriteLine("二进制数为:{0}", newNumber);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string number = (Math.Pow(2,31)).ToString();
int i = number.Length%9,j;
const int next=1000000000;
List<int> splitnumber = new List<int>(); //将字符串转换成一块块长度不超过9的整数
StringBuilder newNumber=new StringBuilder(),tempNumber=new StringBuilder();
if (i != 0)
{
splitnumber.Add(int.Parse(number.Substring(0,i)));
}
while (i < number.Length)
{
splitnumber.Add(int.Parse(number.Substring(i,9)));
i+=9;
}
int count=splitnumber.Count;
while (count != 0) //将所给的数除2,余数加到stringBuilder里
{
for (i = 0; ; ++i)
{
j = splitnumber[i] & 1;
splitnumber[i] >>= 1;
if (i == count - 1)
{
tempNumber.Append(j);
break;
}
else
{
splitnumber[i + 1] += next * j;
}
}
if (splitnumber[0]==0)
{
splitnumber.RemoveAt(0);
--count;
}
}
//得到二进制数
for (i=tempNumber.Length-1;i>-1;--i)
{
newNumber.Append(tempNumber[i]);
}
Console.WriteLine("二进制数为:{0}", newNumber);
}
}
}
private static string ConvertToBinValue(long iValue)
{
if (iValue == 0)
{
return "0";
}
StringBuilder builder = new StringBuilder();
int Mask = 0x1;
while(iValue > 0)
{
builder.Insert(0, iValue & Mask);
iValue = iValue >> 1;
}
return builder.ToString();
}
修改代码如下 public static bs operator +(bs c , bs d) {
try {
bs r = new bs();
r = c.Length >= d.Length ? c.Clone() : d.Clone();
bs m = r.Length == c.Length ? d : c;
int len = c.Length > d.Length ? d.Length : c.Length;
for (int i = 0 ; i < len ; i++) {
bool m1 , m2;
m1 = r[i]; m2 = m[i];
r[i] = r[i] ^ m[i];
int j = i;
while (m1 && m2 && j < r.Length) {
if (j == r.Length - 1) { r.Add(true); break; }
if (r[++j]) { r[j] = false; } else { r[j] = true; break; }
}
}
return r;
} catch {
throw new ArgumentOutOfRangeException();
}
}