Hey all, I'm trying what must be the very simplest of tasks but I can't work out how to get my binary string to a textbox! Any suggestions?

//Existing string from another text box "rev"
MemoryStream ms_memStream = new MemoryStream();
            BinaryWriter br_binaryWriter = new BinaryWriter(ms_memStream);
            try
            {
                br_binaryWriter.Write(rev);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Thanks for reading!

Dani AI

Generated

Short answer: get the bytes for the string, turn each byte into an 8‑bit text representation, and assign the final string to the target TextBox once (use a StringBuilder for speed). Avoid using BinaryWriter for this — BinaryWriter.Write(string) writes a length prefix plus bytes, so the stream data will not be the plain "0" and "1" characters you want (see the BinaryWriter docs).

A concise, robust approach:

  • Choose the encoding you want (ASCII for plain 7-bit ASCII, UTF8 for multibyte UTF-8 output, or iterate chars if you want per‑character Unicode code units).
  • Convert the input to bytes.
  • For each byte use Convert.ToString(b, 2).PadLeft(8, '0') to get an 8‑bit string.
  • Join those pieces and set the other TextBox.Text once.

Example code (assign once to the TextBox):

using System.Text;

string input = textBoxFrom.Text;
var bytes = Encoding.ASCII.GetBytes(input); // or Encoding.UTF8
var sb = new StringBuilder(bytes.Length * 9);
foreach (byte b in bytes)
{
    sb.Append(Convert.ToString(b, 2).PadLeft(8, '0')).Append(' ');
}
textBoxTo.Text = sb.ToString().TrimEnd();

Notes and troubleshooting:

  • ASCII encoding will replace non‑ASCII characters with '?'. Use Encoding.UTF8 if you want the actual UTF‑8 byte sequence instead.
  • If the goal is the Unicode code point per character (not bytes), iterate the string characters and convert (int)ch to binary and pad to 16 (or more) bits — be careful with surrogate pairs for code points > U+FFFF.
  • For large inputs, build the result with StringBuilder and toggle TextBox.Multiline = true and a monospace font for readability.
  • ’s suggestion to write into the writer addresses where to put text, but not the conversion. correctly pointed toward encodings; the Convert/Encoding approach above is simpler and safer for producing readable "0"/"1" text.

Reference: Convert.ToString and Encoding.GetBytes in .NET for details on the methods used.

Recommended Answers

All 4 Replies

Never really worked with a binary writer, but where exactly are you trying to put it into the textbox? Are you getting any errors or just not getting the right output?

From the looks of it you actually need to just tell your code to put it into the correct area. Try this:
br_binaryWriter.Write(textBox1.Text);

I'm trying to read an ASCII string from one text box and display the BINARY representation of that string as a string in another text box :)

Thanks for looking at my thread!

The binary Reader/Writer used for IO streams to files. i do not believe that they can be used in the manner you wish.

try an ASCIIEncoder as that has the option to get bytes/strings

wont let me edit so here is a double post. sorry about that:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace stringToBinary
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void m_ConvertToBin_Click(object sender , EventArgs e)
{
m_Converted.Text = "";
string _someString = m_String.Text;
if (_someString == string.Empty ||
_someString == null)
{
_someString = " ";
}
ASCIIEncoding _toBinary = new ASCIIEncoding();
byte[] _stringRepresentation = _toBinary.GetBytes(_someString);
foreach (byte _byte in _stringRepresentation)
{
double _preDecimal = 0 ,
_aftDecimal = 0 ,
_num = (double)_byte ,
_binConst = 2;
int _Binary = 0;
/*
* predecimal is the numbers before the decimal place so in 4.9 it would be the 4
* aftDecimal is the numbers after the decimal place; so in 4.9 it would be the 9
*/
//since we are working with a byte of data we have 8 binary digits
for (int i = 0 ; i < 8 ; ++i)
{
if (_num != 0)
{
if (i == 0)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary = (int)_aftDecimal * (int)_binConst;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 1)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 10;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 2)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 100;
//update the number we are working with
_num = (int)_preDecimal;
}
 
if (i == 3)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 1000;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 4)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 10000;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 5)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 100000;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 6)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 1000000;
//update the number we are working with
_num = (int)_preDecimal;
}
if (i == 7)
{
//get the before and after decimal places
_preDecimal = (double)System.Math.Floor((decimal)(_num / _binConst));
_aftDecimal = (_num / _binConst) % 1;
//add them to the binary number
_Binary += (int)(_aftDecimal * _binConst) * 10000000;
//update the number we are working with
_num = (int)_preDecimal;
}
}
}
m_Converted.Text += _Binary.ToString() + "--";
}
}
}
}

this will convert any string to its binary compliment

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.