Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can I create a byte from a binary string?
An example of what I mean is:
C#
public static byte CreateByte(string bits)
{
    //The Magic
}

A better example:
C#
CreateByte("10000000");
Posted
Updated 13-Dec-13 19:28pm
v4
Comments
BillWoodruff 14-Dec-13 2:29am    
A byte is an 8 bit unsigned integer: your string cannot be converted into a byte.

The .NET Convert.ToByte(string) function assumes the string parameter is the numeric representation of a byte: if you do:

byte b = Convert.ToByte("11");

The variable 'b now contains the bits: 00001011 ... i.e., equivalent to integer #11.

I think you want to convert a string containing the representation of the 8 bits in a byte, and I'll answer here based on that assumption.

Assuming that your data is in a form where each string of 8 characters contains only the characters '0' and '1' ... and that expresses the string representation of the 8 bits in a byte ...
C#
// use look-up rather than calling Math.Pow
List<int> Pow2 = new List<int> { 128, 64, 32, 16, 8, 4, 2, 1 };

// notice the return value is a nullable byte Type
private byte? StringToByte(string byteString)
{
    // test for correct bit length
    if (byteString.Length != 8) return null;

    // test for invalid bit values
    foreach(char c in byteString) { if (c != '0' && c != '1') return null;}

    int byteResult = 0;

    for (int i = 0; i < byteString.Length; i++)
    {
        byteResult +=
            (byteString[i] == '0')
            ? 0
            : Pow2[i];
    }

    return Convert.ToByte(byteResult);
}
Testing:
C#
byte? byte1 = StringToByte("00000000");
byte? byte2 = StringToByte("00000001");
byte? byte3 = StringToByte("01000001");
byte? byte4 = StringToByte("10000000");
byte? byte5 = StringToByte("11111111");

// test for length error
byte? byte6 = StringToByte("1111111111");
byte? byte7 = StringToByte("111");

// test for invalid bit error
byte? byte8 = StringToByte("1311111111");
byte? byte9 = StringToByte("1311Y11111");
The use of the nullable Byte return Type essentially defers checking for an error to the code calling the method: if you'd rather throw an error in the method, then change the return Type, and change the code.
 
Share this answer
 
 
Share this answer
 
How about:
if ( string = "zero" ) return 0;
if ( string = "one" ) return 1;
if ( string = "two" ) return 2;
if ( string = "three" ) return 3;
if ( string = "four" ) return 4;
if ( string = "five" ) return 5;
...
... left as exercise for the reader
...
if ( string = "two hundred and fifty five" ) return 255;


or if you specify what the input string bits ought to contain and how it should be interpreted as a byte, then better answers might be provided.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900