String -> Binary -> TextBox -> AARGH!

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 21
Reputation: covertx is an unknown quantity at this point 
Solved Threads: 0
covertx covertx is offline Offline
Newbie Poster

String -> Binary -> TextBox -> AARGH!

 
0
  #1
Mar 25th, 2007
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?

  1. //Existing string from another text box "rev"
  2. MemoryStream ms_memStream = new MemoryStream();
  3. BinaryWriter br_binaryWriter = new BinaryWriter(ms_memStream);
  4. try
  5. {
  6. br_binaryWriter.Write(rev);
  7. }
  8. catch (Exception ex)
  9. {
  10. MessageBox.Show(ex.Message);
  11. }
Thanks for reading!
+[->,----------]<[+++++++++++.<]
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 173
Reputation: RwCC is an unknown quantity at this point 
Solved Threads: 4
RwCC's Avatar
RwCC RwCC is offline Offline
Junior Poster

Re: String -> Binary -> TextBox -> AARGH!

 
0
  #2
Mar 26th, 2007
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);
Sir David Healy - Northern Ireland Goal King
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 21
Reputation: covertx is an unknown quantity at this point 
Solved Threads: 0
covertx covertx is offline Offline
Newbie Poster

Re: String -> Binary -> TextBox -> AARGH!

 
0
  #3
Mar 26th, 2007
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!
+[->,----------]<[+++++++++++.<]
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: String -> Binary -> TextBox -> AARGH!

 
0
  #4
Apr 9th, 2007
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
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: String -> Binary -> TextBox -> AARGH!

 
0
  #5
Apr 9th, 2007
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
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC