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!

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

This article has been dead for over six months. Start a new discussion instead.