anicolaspp
anicolaspp

Reputation: 3

Bit Conversion trying to write into a stream

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???

Upvotes: 0

Views: 265

Answers (1)

Brian Warshaw
Brian Warshaw

Reputation: 22974

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.

Upvotes: 1

Related Questions