Click here to Skip to main content
11,708,116 members (74,996 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# binary
I've created a text file Encoded in ASCII. It Contains the Following Text;


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 3-Dec-11 21:40pm
DaveAuld at 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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Reading text is easy: MSDN[^]
byte[] data = File.ReadAllBytes(path);
Converting to binary is easy: MSDN[^]
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 | :laugh:
Pravinda Amarathunge at 4-Dec-11 4:24am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can also to it by using LINQ
var str = "abcd123";
           foreach (string letter in str.Select(c => Convert.ToString(c, 2)))
For more reference :-
How do you convert a string to ascii to binary in C#[^]

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

  Print Answers RSS
0 Maciej Los 230
1 Mika Wendelius 180
2 OriginalGriff 168
3 Sergey Alexandrovich Kryukov 145
4 Peter Leow 140
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100