二进制与1/0字符串的转换

在帮助里找了好久也没找到相应的函数,不知道是不是我找的方向不对所以没有找到.
自己写了个,没有做数据验证^_^
None.gif      public   class  BitOperator
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将字节转换成二进制字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="valByte">字节</param>
ExpandedSubBlockEnd.gif        
/// <returns>二进制字符串</returns>

InBlock.gif        public string BitConvert(byte valByte)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string valString = "00000000";
InBlock.gif            
int valInt = System.Convert.ToInt32(valByte);
InBlock.gif            
InBlock.gif            
if(valInt != 0
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
int[] bitArray = new int[]dot.gif{0,0,0,0,0,0,0,0};
InBlock.gif                valString 
= "";
InBlock.gif                
for(int i = 7; i >= 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bitArray[i] 
= valInt%2;
InBlock.gif                    valInt 
/= 2;
InBlock.gif                    
if(valInt == 1
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        bitArray[i 
- 1= 1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
for(int i = 0; i < bitArray.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    valString 
+= bitArray[i].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return valString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将二进制字符串转换成字节
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="valString">二进制字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>字节</returns>

InBlock.gif        public byte BitConvert(string valString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte valByte = System.Convert.ToByte(0);
InBlock.gif
InBlock.gif            
if(valString != "00000000")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int valInt = 0;
InBlock.gif                
for(int i = 7,j = 0; i >= 0; i--,j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(valString[i] != '0')
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        valInt 
+=  System.Convert.ToInt32(Math.Pow(2,j));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                valByte 
=System.Convert.ToByte(valInt);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return valByte;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/jxhwei/archive/2006/04/30/389226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值