byte[] data = new byte[4];
uint aa = 20;
System.BitConverter.GetBytes(aa);

解决方案 »

  1.   

    byte[] data = new byte[4];
    uint aa = 20;
    data=System.BitConverter.GetBytes(aa);
      

  2.   

    先定义一个byte数组,然后使用BitConverter.GetBytes(//你需要转换的十六进制数);这样就搞定了
      

  3.   

    除了 System.BitConverter.GetBytes(aa); 方法还有别的没有?
      

  4.   

    下面一段是我不久前写的16进制和10进制互换的代码,可能比较长,有兴趣的话可以看下;
    可能看不清楚或明白,那发信给我,我给你发完整版的:[email protected]
                               string dec_str=null;
    string hex_str=null;
    long dec;
    string result=null;
    char[] ch=null;
    if(this.textBox1.Text!="")
    {
    dec_str=this.textBox1.Text.Trim();
    dec=Int64.Parse(dec_str);
    }
    if(this.textBox2.Text!="")
    {
    hex_str=this.textBox2.Text.Trim();
    ch=hex_str.ToCharArray();
    long temp=1;
    long mid_temp=0;
    int i=ch.Length;
    int b=0;
    int a=0;
    int c=0;
    for(int j=0;j<i;j++)
    {
    b=i-j-1;
    if(((ch[j]>='a')&&(ch[j]<='f'))||((ch[j]>='A')&&(ch[j]<='F')))
    {
    a=0;
    if(ch[j]=='a'||ch[j]=='A')
    a=10;
    else if(ch[j]=='b'||ch[j]=='B')
    a=11;
    else if(ch[j]=='c'||ch[j]=='C')
    a=12;
    else if(ch[j]=='d' ||ch[j]=='D')
    a=13;
    else if(ch[j]=='e'||ch[j]=='E')
    a=14;
    else if(ch[j]=='f'||ch[j]=='F')
    a=15;
    if(b!=0)
    {
    for(int k=0;k<b;k++)
    {
    temp*=16;
    }
    }
    else
    temp*=1;
    temp*=a;
    mid_temp+=temp;
    temp=1;
    }
    else if((ch[j]>='0'&&ch[j]<='9'))
    {
    c=Int32.Parse(ch[j].ToString());
    if(b!=0)
    {
    for(int k=0;k<b;k++)
    {
    temp*=16;
    }
    }
    else
    temp*=1;
    temp*=c;
    mid_temp+=temp;
    temp=1;
    }
    }
    result=mid_temp.ToString();
    this.textBox1.Text=result;
    }
    else if(this.textBox1.Text!="")
    {
    int temp0=0;
    int temp1=0;
    int temp2=1;
    int temp3=0;
    int temp4=0;
    long val=Int64.Parse(this.textBox1.Text.Trim());
    char[] chr=new char[100];
    char[] covert=new char[100];
    char ch1='1';
    char ch0='0';
    char temp5='0';
    char[] result1=new char[20];
    int count=0;
    //int qushu=2;
    if(val>=1)
    {
    for(int i=0;val!=1;i++)
    {
    if(val%2!=0)
    chr[i]=ch1;
    else
    chr[i]=ch0;
    val/=2;
    temp4=i;
    }
    temp4++;
    chr[temp4]=ch1;
    if((temp4+1)%4!=0)
    {
    temp3=4-(temp4+1)%4;
    for(int j=1;j<=temp3;j++)
    {
    temp4++;
    chr[temp4]=ch0;
    }
    }
    for(int j=0,k=temp4;k>=0;j++,k--)
    {
    covert[j]=chr[k];
    }
    /*for(int k=0;k<=temp4;k++)
    {
    result=covert[k].ToString();
    this.textBox2.AppendText(result);
    }*/
    for(int i=0;i<=temp4-3;i+=4)
    {
    temp0=0;
    for(int j=0;j<4;j++)
    {
    temp1=Int32.Parse(covert[i+j].ToString());
    if(temp1!=0)
    {
    for(int m=1;m<=3-j;m++)
    {
    temp2*=2;
    }
    }
    temp1*=temp2;
    temp0+=temp1;
    temp2=1;
    }
    if((temp0>=10)&&(temp0<=15))
    {
    switch(temp0)
    {
    case 10:
    {
    result1[count]='a';
    count++;
    break;
    }
    case 11:
    {
    result1[count]='b';
    count++;
    break;
    }
    case 12:
    {
    result1[count]='c';
    count++;
    break;
    }
    case 13:
    {
    result1[count]='d';
    count++;
    break;
    }
    case 14:
    {
    result1[count]='e';
    count++;
    break;
    }
    case 15:
    {
    result1[count]='f';
    count++;
    break;
    }
    }
    }
    else
    {
    switch(temp0)
    {
    case 0:
    {
    result1[count]='0';
    count++;
    break;
    }
    case 1:
    {
    result1[count]='1';
    count++;
    break;
    }
    case 2:
    {
    result1[count]='2';
    count++;
    break;
    }
    case 3:
    {
    result1[count]='3';
    count++;
    break;
    }
    case 4:
    {
    result1[count]='4';
    count++;
    break;
    }
    case 5:
    {
    result1[count]='5';
    count++;
    break;
    }
    case 6:
    {
    result1[count]='6';
    count++;
    break;
    }
    case 7:
    {
    result1[count]='7';
    count++;
    break;
    }
    case 8:
    {
    result1[count]='8';
    count++;
    break;
    }
    case 9:
    {
    result1[count]='9';
    count++;
    break;
    }
    }
    }
    }
    }
    for(int i=0;i<count;i++)
    {
    this.textBox2.AppendText(result1[i].ToString());
    }
    }