0

I have a string which represents a binary number: "1010", which represents a 10(ten) in decimal.

I need to write this string into a Stream but keeping the binary format. When normally you want to write a string, .Net will save it converting the current string into a byte array and then putting those bytes into the string, I do not want that, because the bytes that I want to contain my stream is that I have into the string "1010" for example.

How I do this???

David
  • 72,686
  • 18
  • 132
  • 173

1 Answers1

1

If "1010" is a string, you can still write it to a stream and preserve the format, provided the receiving end uses the proper encoding. Of course, you could use a StreamWriter and just write the string as a string.

UPDATE

Ok, so your comments seem to clarify your question a little. So it seems like you want to convert a base-2 string into a byte, so that you aren't storing multiple bytes to represent in text what you can represent in a single byte. Is that fair? If so, use Convert.ToByte(String, Int32), and specify 2 for the base. Then you have a byte corresponding to your string and you can write it out.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
  • Nop, because it will convert the string into bytes a write those bytes. I just dont wanna that. I want that my bit of array into my stream be the same that the string contain – anicolaspp Sep 07 '12 at 19:15
  • But it *will* be the same. Everything is basically bytes behind the scenes anyway, even your string as you see it. Anytime you write to or read from a stream, you're writing or reading bytes. You might have a higher-level API that abstracts the bytes away and lets you work with more sophisticated types, but it will always be bytes. And in your case, you're worrying about something that isn't actually a problem. Open a `FileStream` and write that string. Then open the file. There you go. – Brian Warshaw Sep 07 '12 at 19:18
  • I know that very well. I am worry because implementing a compress algorithm is not so easy and I need access to that kind of things, if I write the string into the stream it will increse its lenght, wrong!! that is the reason I need to write real bytes – anicolaspp Sep 07 '12 at 19:25
  • in way that is what I want, but not exacly. Ok, let me put it of this way: My string is a representation of data (binary of course), so I want to write that data into a stream. If I have string x = "1010" and I write it into a stream and after that I read a int from the stream a must get int a = 10 – anicolaspp Sep 07 '12 at 20:20
  • Right, but you can think of bytes and ints as integral data types, at least as far as this is concerned. A `byte` is an 8-bit integer, a `short` is a 16-bit integer, an `int` is a 32-bit integer, and a `long` is a 64-bit integer. If you write that byte out to a text file, regardless of encoding, you can read it back from the file, cast the `byte` to an `int`, and get what you're looking for. Come to the think of it, you can probably just assign an `int` variable to the value of that byte without casting, since it's a smaller type. – Brian Warshaw Sep 08 '12 at 14:41