从二进制字符串创建一个字节 [英] Creating a byte from a binary string

查看:78
本文介绍了从二进制字符串创建一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从二进制字符串创建一个字节?

我的意思是:

  public   static   byte  CreateByte(字符串位)
{
// 魔术
}



一个更好的例子:

 CreateByte(  10000000); 

解决方案

假设您的数据采用的形式是每个8个字符的字符串仅包含字符0和1...并且表示字节中8个的字符串表示形式。 。

  //  使用查找而不是调用Math.Pow  
List< int> Pow2 = new 列表< int> { 128 64 32 ,< span class =code-digit> 16 , 8 4 2 1 };

// 注意返回值是可以为空的字节类型
private byte ? StringToByte( string byteString)
{
// 测试正确的位长
if (byteString.Length!= 8 return null ;

// 测试无效位值
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);
}

测试:

  byte ? byte1 = StringToByte(  00000000); 
byte ? byte2 = StringToByte( 00000001);
byte ? byte3 = StringToByte( 01000001);
byte ? byte4 = StringToByte( 10000000);
byte ? byte5 = StringToByte( 11111111);

// 测试长度错误
字节? byte6 = StringToByte( 1111111111);
byte ? byte7 = StringToByte( 111);

// 测试无效位错误
字节? byte8 = StringToByte( 1311111111);
byte ? byte9 = StringToByte( 1311Y11111);

使用可空字节返回类型本质上是将错误检查延迟到调用方法的代码:如果您更愿意在方法中抛出错误,则更改返回类型,然后更改代码。


visit这里..



convert-a-binary-string-representation-to-a-byte-array [ ^ ]


怎么样:

 if(string =0)返回0; 
if(string =one)返回1;
if(string =two)返回2;
if(string =three)返回3;
if(string =four)返回4;
if(string =five)返回5;
...
...留给读者练习
...
如果(字符串=二百五十五)返回255;





或者如果指定输入字符串位应包含的内容以及如何将其解释为字节,则可能会提供更好的答案。


How can I create a byte from a binary string?
An example of what I mean is:

public static byte CreateByte(string bits)
{
    //The Magic
}


A better example:

CreateByte("10000000");

解决方案

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 ...

// 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:

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.


visit here..

convert-a-binary-string-representation-to-a-byte-array[^]


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.


这篇关于从二进制字符串创建一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆