Hi i was able to convert a ASCII
string to binary using a binarywriter
.. as 10101011
. im required back to convert Binary
---> ASCII
string .. any idea how to do it ?
Asked
Active
Viewed 4.2k times
9

Sudantha
- 15,684
- 43
- 105
- 161
-
1System.Text.Encoding.ASCII.GetString(data) ? – Chris Baxter May 15 '11 at 13:45
-
3What's wrong with Convert.ToBase64String(data)? Why are you shoving the bytes into a stream, and reading those bytes back again from that stream? – R. Martinho Fernandes May 15 '11 at 13:46
-
`ToBase64String` gives me a Base64 value not a Text ! – Sudantha May 15 '11 at 13:48
-
`System.Text.ASCIIEncoding.ASCII.GetString` giving me back the binary in string not Text Representation :-S – Sudantha May 15 '11 at 13:50
-
1**What do you want to do?** Just say what do you have, and what do you want. If you're using ToBase64String, **of course the result will be in base-64**. – R. Martinho Fernandes May 15 '11 at 13:50
-
1I you're trying to get banned from asking questions, you're a great job. Stop asking the same thing again and again! – Shadow The GPT Wizard May 15 '11 at 13:51
-
@Martinho I have a Binary String `'101010'` -> Need `'ABC'` representation of it ! – Sudantha May 15 '11 at 13:53
-
1@Sudantha how exactly `101010` is `ABC`? If you'll describe the algorithm maybe someone can help you implement it. – Shadow The GPT Wizard May 15 '11 at 13:54
-
@Sudantha - Do you have a binary string "01001101" or a byte array? Your question says it is already a byte array; thus GetString... if you need to convert the string representation of binary in to a byte array then that is a different issue. – Chris Baxter May 15 '11 at 13:55
-
@Shadow - `010000010100001001000011` --> `ABC` - NOW HAPPY ? – Sudantha May 15 '11 at 13:55
-
@Sudantha: see, wasn't that hard, was it? – R. Martinho Fernandes May 15 '11 at 13:56
-
@Coder - Thats the exact point ! i have a `String` .. how to take it exactly to a `byte[]` ? – Sudantha May 15 '11 at 13:57
-
http://www.dreamincode.net/code/snippet2206.htm algo i used to create the binary string – Sudantha May 15 '11 at 13:58
-
Calgary beat me to it, see your original question for the almost same code just with some error handling. – Shadow The GPT Wizard May 15 '11 at 14:13
-
From your multiple re-tries a this, your first sentence is wrong. A binarywriter does not produce a _string_ of `0` and `1`s – H H May 15 '11 at 14:19
5 Answers
23
This should do the trick... or at least get you started...
public Byte[] GetBytesFromBinaryString(String binary)
{
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
return list.ToArray();
}
Once the binary string has been converted to a byte array, finish off with
Encoding.ASCII.GetString(data);
So...
var data = GetBytesFromBinaryString("010000010100001001000011");
var text = Encoding.ASCII.GetString(data);

Chris Baxter
- 16,083
- 9
- 51
- 72
-
Thanks a Lot Coder.. Exactly what i needed and working awesomely ! .. i was trying whole day with a blackout and finally !! thanks a lot ! :D – Sudantha May 15 '11 at 14:11
-
@Sudantha: I have merged your two identical questions; you might want to re-accept this answer (it was accepted on the other question). – balpha May 15 '11 at 14:56
-
7
If you have ASCII charters only you could use Encoding.ASCII.GetBytes
and Encoding.ASCII.GetString
.
var text = "Test";
var bytes = Encoding.ASCII.GetBytes(text);
var newText = Encoding.ASCII.GetString(bytes);

Alex Aza
- 76,499
- 26
- 155
- 134
3
Here is complete code for your answer
FileStream iFile = new FileStream(@"c:\test\binary.dat",
FileMode.Open);
long lengthInBytes = iFile.Length;
BinaryReader bin = new BinaryReader(aFile);
byte[] byteArray = bin.ReadBytes((int)lengthInBytes);
System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;
string str = encEncoder.GetString(byteArray);

Amit
- 21,570
- 27
- 74
- 94
1
Take this as a simple example:
public void ByteToString()
{
Byte[] arrByte = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 };
string x = Convert.ToBase64String(arrByte);
}
This linked answer has interesting details about this kind of conversion:

Community
- 1
- 1

Leniel Maccaferri
- 100,159
- 46
- 371
- 480
-
HI leniel im having a string how to convert it to `byte[]` i used `Encoding.ASCHII` .... but it gives a junk ~ – Sudantha May 15 '11 at 04:21
0
Sometimes instead of using the built in tools it's better to use "custom" code.. try this function:
public string BinaryToString(string binary)
{
if (string.IsNullOrEmpty(binary))
throw new ArgumentNullException("binary");
if ((binary.Length % 8) != 0)
throw new ArgumentException("Binary string invalid (must divide by 8)", "binary");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < binary.Length; i += 8)
{
string section = binary.Substring(i, 8);
int ascii = 0;
try
{
ascii = Convert.ToInt32(section, 2);
}
catch
{
throw new ArgumentException("Binary string contains invalid section: " + section, "binary");
}
builder.Append((char)ascii);
}
return builder.ToString();
}
Tested with 010000010100001001000011
it returned ABC
using the "raw" ASCII values.

Shadow The GPT Wizard
- 66,030
- 26
- 140
- 208