Results 1 to 9 of 9

Thread: [3.0/LINQ] Text to ascii binary

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    [3.0/LINQ] Text to ascii binary

    Im trying convert a text string to ascii binary

    such as A =01000001
    test =01110100011001010111001101110100

    here is an example
    http://www.roubaixinteractive.com/Pl...ry_To_Text.asp


    I understand that im goign to have to do a for each loop for each char C in string x

    but im not sure how to get the binary ascii char out of it


    i know this is probably pretty simple but im just drawing a blank

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [3.0/LINQ] Text to ascii binary

    Code:
    string Int2Bin(int c)
            {
                string ret = "";
                while (c > 0)
                {
                    ret = (((c & 1) != 0) ? "1" : "0") + ret;
                    c >>= 1;
                }
                return ret.PadLeft(8,'0');
            }
            string Str2Bin(string str)
            {
                string ret = "";
                if (str.Length > 0)
                {
                    ret = Int2Bin(str[0]);
                    for (int i = 1; i < str.Length; i++)
                    {
                        ret += " " + Int2Bin(str[i]);
                    }
                }
                return ret;
            }
    Last edited by high6; Feb 26th, 2009 at 09:23 PM.

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] Text to ascii binary

    this is pretty close but something is off im looking into it now

    "Robert"
    website =010100100110111101100010011001010111001001110100
    Program=001001010111101100100011010100110010011100010111

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] Text to ascii binary

    a=01000011
    a=01100001

    Its backwards

  5. #5
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [3.0/LINQ] Text to ascii binary

    updated, my bad :P. Was appending on the wrong side.

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [3.0/LINQ] Text to ascii binary

    Even though High's code probably works, I think this is more readable:

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(ConvertToBin("test"));
    
            }
    
            string ConvertToBin(string s)
            {
                StringBuilder sb = new StringBuilder();
                Byte[] bytes= System.Text.Encoding.ASCII.GetBytes(s);
                foreach (byte b in bytes )
                    sb.Append(Convert.ToString(b,2).PadLeft(8,'0'));
                return sb.ToString();
            }

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] Text to ascii binary

    Quote Originally Posted by high6
    Code:
    string Int2Bin(int c)
            {
                string ret = "";
                while (c > 0)
                {
                    ret = (((c & 1) != 0) ? "1" : "0") + ret;  <------Here
                    c >>= 1;
                }
                return ret.PadLeft(8,'0');
            }
            string Str2Bin(string str)
            {
                string ret = "";
                if (str.Length > 0)
                {
                    ret = Int2Bin(str[0]);
                    for (int i = 1; i < str.Length; i++)
                    {
                        ret += " " + Int2Bin(str[i]);
                    }
                }
                return ret;
            }

    I figured it out too. I am intrigued by this line ( marked HERE)

    ive never seen that type of syntax could you explain it a little better

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] Text to ascii binary

    sorry to be going the long way around this


    but I want to convert the binary string to a Dec number

    I tried this code
    return Convert.ToInt64(sb.ToString(), 2);

    and it works till i get up into the words that are 8 or 9 letters long
    then i get a stack overflow.

    i dont see a convert.tolong what should i use

  9. #9
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [3.0/LINQ] Text to ascii binary

    Quote Originally Posted by Crash893
    I figured it out too. I am intrigued by this line ( marked HERE)

    ive never seen that type of syntax could you explain it a little better
    Code:
    ret = (((c & 1) != 0) ? "1" : "0") + ret;
    It is called a condition operator.

    It is basically like doing.

    Code:
    if ((c & 1) != 0)
    {
        ret = "1" + ret;
    }
    else
    {
        ret = "0" + ret;
    }
    Quote Originally Posted by Crash893
    sorry to be going the long way around this


    but I want to convert the binary string to a Dec number

    I tried this code
    return Convert.ToInt64(sb.ToString(), 2);

    and it works till i get up into the words that are 8 or 9 letters long
    then i get a stack overflow.

    i dont see a convert.tolong what should i use
    Long/Int64 are the same, they both can only store 8 bytes(64 bits). So you cannot store a string longer then 8 bytes in an Int64.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width