请问,怎样将十进制大数字符串(例如“365214563156465415613132153”,可能比INT32数据类型还大)转化成二进制字符串?谢谢

解决方案 »

  1.   

                string s = "365214563156465415613132153";
                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);
      

  2.   

    楼上的答案错了吧,比如你输入这个
               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
      

  3.   

    我做了个可以对付 小一点数据的 你可以COPY来试一下(我已经初步测试过小值了,大值没敢试)using System;
    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;
            }
        }
      

  4.   

    我上面回复有BUG,要改下这个函数改了就对了:        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 (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();
                }
            }
      

  5.   

                Console.WriteLine(CastToBinaryFromInt((256+127+64+32+15+0).ToString()).BinaryData.ToArray<char>());
                Console.Read();//
    111001110
      

  6.   

    using System;
    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);
            }
        }
    }
      

  7.   

    我上面的划分成整数时出了点问题,改成下面的
    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);
            }
        }
    }
      

  8.   

    用位操作:
    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();
    }
      

  9.   

    哎呀 不好意思,那个修改依然完全是错的,这儿又改了一次,这次经验证没有发现BUG,结果符合预期。
    修改代码如下        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();
                }
            }