0

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?

5
  • 1
    how about a byte array? or a List<byte>? Commented Mar 18, 2012 at 6:12
  • @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... Commented Mar 18, 2012 at 6:14
  • 1
    ...which is no different to adding to a string (they're immutable) Commented Mar 18, 2012 at 6:15
  • yea, which is another reason why i don't want to use it. Commented Mar 18, 2012 at 6:18
  • Have you considered the BitConvertor class that is part of the framework? Commented Mar 18, 2012 at 10:04

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

2 Comments

hmm... i'm getting the value "138" back... the value in the original string is 2 though..i must be doing something wrong.. from the pattern of the bytes i get back, i can tell it's the right data... but.. i donno how 138 is 2...?
Check the original file with a hex editor. It could be that you are grabbing bytes at an offset. Also, make sure that the '2' character you are looking at is encoded in ascii, which is an 8-bit character encoding (actually 7 bits, as the sign bit is unused and renders as a '?' - if you get values of 128 and above unsigned/negative signed, it means you are grabbing non-valid characters)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.