二进制转换成十六进制

//如"我"字的gb2312编码: 十六进制表示为 ce,十进制表示为206.
        
//它的演算为: int b=206(11001110)(ce); b>>4=12(1100)(c); b & 0xF (11001110 & 00001111)=14(00001110)(e);
    Byte[] buffer=null;
                buffer
=System.Text.Encoding.GetEncoding("GB2312").GetBytes(ChinaStr1);
        
                
string str2=ToHexString(buffer) ;
static char[] hexDigits = {
                                      
'0''1''2''3''4''5''6''7',
                                      
'8''9''a''b''c''d''e''f'}
;
 
        
private static string ToHexString(byte[] bytes) 
        
{
            
char[] chars = new char[bytes.Length * 4];
            
for (int i = 0; i < bytes.Length; i++
            
{
                
int b = bytes[i];
                chars[i 
* 4 ]='\\';
                chars[i 
* 4 + 1]='\'';
                chars[i * 4 + 2= hexDigits[b >> 4];
                chars[i 
* 4 + 3= hexDigits[b & 0xF];
                
            }

            
return new string(chars);
        }
原文地址:https://www.cnblogs.com/furenjun/p/326500.html