Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a text file Encoded in ASCII. It Contains the Following Text;

abcd123


Binary value of the above text ;

01100001 01100010 01100011 01100100 0110001 0110010 0110011 


How Can I do this (getting binary value of a file to a text box) in C# ?
Posted
Comments
DaveAuld 4-Dec-11 3:43am    
Sounds like a homework question to me. There are various ways to achieve this, but i'm sure this must have been covered in you course notes?

Reading text is easy: MSDN[^]
C#
byte[] data = File.ReadAllBytes(path);

Converting to binary is easy: MSDN[^]
C#
string s = Convert.ToString(myByte, 2);

Put them together with a foreach loop and a StringBuilder and you have a program...

But, since this is your homework, I'm leaving the actual code to you! :laugh:
 
Share this answer
 
Comments
Captain Price 4-Dec-11 4:24am    
thanks
You can also to it by using LINQ
C#
var str = "abcd123";
           foreach (string letter in str.Select(c => Convert.ToString(c, 2)))
           {
               Console.WriteLine(letter);
           }
           Console.ReadLine();

For more reference :-
How do you convert a string to ascii to binary in C#[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900