0
votes

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?

3
how about a byte array? or a List<byte>? - Mitch Wheat
@MitchWheat the problem is i don't know the number of bytes i'll get back (in total).. and you can't just expand a byte array on the spot.. you'd have to keep making a new one... - BigBug
...which is no different to adding to a string (they're immutable) - Mitch Wheat
yea, which is another reason why i don't want to use it. - BigBug
Have you considered the BitConvertor class that is part of the framework? - Shaun Wilde

3 Answers

2
votes

Why don't you use a List<byte> ? It will grow dynamically as you add more bytes. Then you can decode it into a string.

1
votes

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

        char[] ac = { (char)(byte)97 };
        string s = new String(ac);
        Console.WriteLine(s);

A ready-to-compile sample program is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ByteTest
{
class Program
{
    static void Main(string[] args)
    {
        char[] ac = { (char)(byte)97 }; //converts it to a character
        string s = new String(ac); //converts it to a string
        Console.WriteLine(s); //writes 'a'
        Thread.Sleep(10000); //displays the 'a' for 10 seconds, then finishes executing
    }
}
}

For your specific situation, you could modify it to be the following

 string Message = "";
 ...
 char[] ac = { (char)valueInByte };
 string s = new String(as);
 Message += s;
0
votes

MemoryStream is another possible container,

http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

You can use its WriteByte method to accumulate the bytes.