|
-
Feb 26th, 2009, 06:47 PM
#1
Thread Starter
Fanatic Member
[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
-
Feb 26th, 2009, 07:58 PM
#2
Frenzied Member
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.
-
Feb 26th, 2009, 09:11 PM
#3
Thread Starter
Fanatic Member
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
-
Feb 26th, 2009, 09:13 PM
#4
Thread Starter
Fanatic Member
Re: [3.0/LINQ] Text to ascii binary
a=01000011
a=01100001
Its backwards
-
Feb 26th, 2009, 09:23 PM
#5
Frenzied Member
Re: [3.0/LINQ] Text to ascii binary
updated, my bad :P. Was appending on the wrong side.
-
Feb 26th, 2009, 10:22 PM
#6
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();
}
-
Feb 27th, 2009, 11:43 PM
#7
Thread Starter
Fanatic Member
Re: [3.0/LINQ] Text to ascii binary
 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
-
Feb 28th, 2009, 12:17 AM
#8
Thread Starter
Fanatic Member
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
-
Feb 28th, 2009, 08:31 AM
#9
Frenzied Member
Re: [3.0/LINQ] Text to ascii binary
 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;
}
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|