- Home /
Writing to files in bytes.
If I have a string that looks like your standard byte, for example: "01001100", can I write this to a file and have it use just one byte? As opposed to the 8 it would normally take up if I use the normal StreamWriter method or something similar.
Do you have a string or a stream?
If I have a string that looks like your standard byte, for example: "01001100"
I've never heard of anyone reading the 1's and 0's in a string into bits of data, but I'm not saying it couldn't be done. $$anonymous$$ost people would just Serialize()
the data or something similar to that.
Hmm I think my problem is that I don't really know anything about serializing, streams of how data is really stored... Could you point my in the direction of some good documents that can explain how it all works?
Here's the .Net page on serialization. It's a fairly thorough explanation that might help you some if you have a decent understanding of technical terms.
Answer by msknapp · Sep 24, 2011 at 03:50 PM
something like this might work:
byte value = 0;
int index = 0;
char[] cs = byteString.toCharArray();
for (int i = 0;i<cs.Length(),i++) {
if (cs[i]=='1') {
value += (1<< (8-i));
}
}
// now save that byte.
Your answer
Follow this Question
Related Questions
Unity not properly flushing file output? 0 Answers
Problems writing to existing xml 1 Answer
Saving data in to files (Android) 1 Answer
Reading - Writing data to - from file? 2 Answers
Writing to a File Without Deleting What's Already There 2 Answers