if i’m getting 8 bits from a source and i put those 8 bits into a byte, how can i then store away this byte? each byte is part of a message (which was once a string) I don’t know how many bytes i’ll end up with – hence using a byte array is not really an option. can i store it in a string? I want to be able to re-assemble the string after i get all the bytes, how would i do this?
using the method below to make 8 bits into a byte..
public byte GetByte(BitArray array){
Byte byt = 0;
for (int i = 7; i >= 0; i--){
byt = (byte)((byt << 1) | (array[i] ? 1 : 0));
}
return byt;
}
i can call it by doing this…
byte valueInByte = GetByte(bitsOfMessage);
i was thinking i could do this…
bytesOfTheMessage += (valueInByte.ToString() + "+"); //bytesOfTheMessage is a string
but then… i have a bunch of byte values in a string… i need to extract and convert, but what do i convert to. The very first value that i get back is “138” which is supposed to be the number 2 – when converted back to it’s original form. i can tell that all the right values are there within the string as there is a pattern and it is consistent with the original string that i converted into bits.. any idea on what i should do?
Well, if you do want/need to put them in a string (though it may not be the most efficient way to store your data, if you really are just using it as storage), you can do something along these lines:
For example, this bit of code will print out the character ‘a’ (who’s ASCII code is 97 in decimal. Note that any value above 127 is displayed as a ‘?’)
A ready-to-compile sample program is as follows:
For your specific situation, you could modify it to be the following