请教大虾们,如何将下面的128位转换为16字节的byte数组?急用
33832964bf7a3b7c0f3ae3925066160e
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
33832964bf7a3b7c0f3ae3925066160e
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
如果是可以用Guid来表示就是了.
Guid g = new Guid("33832964bf7a3b7c0f3ae3925066160e");
byte[] bytes = g.ToByteArray();
byte[] bytes=guid.ToByteArray();
Console.WriteLine(BitConverter.ToString(bytes));
c20db9e33a672d1a85e04f1be966a0a1406669e6e9ddcb6ea70c7a8e0cc3ddaf");
byte[] bytes = g.ToByteArray();
这样能够是长度为16的字节数组吗?!
得到:
64-29-83-33-7A-BF-7C-3B-0F-3A-E3-92-50-66-16-0E测试的结果,估计有点问题...
前面4个字节位置相反,第5个字节和第6个字节位置也相反...楼主,你有四排字符串,你要得到四个数组还是1个数组?
把输出结果写出来先
以上方法不可用。
private string getbytes(string stmp)
{
string sBytes="";
for(int i=0;i<stmp.Length;i++)
{
sBytes+=IntToBytes(stmp[i]);
}
return sBytes;
} private string IntToBytes(char t)
{
switch(t)
{
case '0':
{
return "0000";
}
case '1':
{
return "0001";
}
case '2':
{
return "0010";
}
case '3':
{
return "0011";
}
case '4':
{
return "0100";
}
case '5':
{
return "0101";
}
case '6':
{
return "0110";
}
case '7':
{
return "0111";
}
case '8':
{
return "1000";
}
case '9':
{
return "1001";
}
case 'a':
{
return "1010";
}
case 'b':
{
return "1011";
}
case 'c':
{
return "1100";
}
case 'd':
{
return "1101";
}
case 'e':
{
return "1110";
}
case 'f':
{
return "1111";
}
}
}
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
这几个要一个个的转,
如果要用一个Byte数组,那么你这个数组要定义大一些,足以用来存放这几个值的字节形式.比如:byte[] bytes = new byte[32 * 4];
byte[] bs = null;
int startIndex = 0;
Guid g = new Guid("33832964bf7a3b7c0f3ae3925066160e");
bs = g.ToByteArray();
bs.CopyTo(bytes, 0);
startIndex =bs.Length;
g = new Guid("3f66864d511f7dc43a550900bd795d02");
bs = g.ToByteArray();
bs.CopyTo(bytes, startIndex);
startIndex += bs.Length;g = new Guid("c20db9e33a672d1a85e04f1be966a0a1");
bs = g.ToByteArray();
bs.CopyTo(bytes, startIndex);
startIndex += bs.Length;g = new Guid("406669e6e9ddcb6ea70c7a8e0cc3ddaf");
bs = g.ToByteArray();
bs.CopyTo(bytes, startIndex);
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
--------------------------------
得到的结果是?运算的规则是?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx共有16 * 4 个字节
除非这个数组类型是4个字节比如int32
楼主先说清楚你的需求
例﹕
private byte[] IntToBytes(char t)
{
switch(t)
{
case '0':
{
return new byte[]{(byte)0,(byte)0,(byte)0,(byte)0};
}
....
}
}
怎样将33832964bf7a3b7c0f3ae3925066160e
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf)
是你数错了?还是想压缩啊?
===========================開始的需求沒有說清楚~~
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class KEY
{
public byte[] bt = new byte[16];
} class Program
{
static void Main(string[] args)
{
KEY[] bta = new KEY[4];
Guid g = new Guid("33832964bf7a3b7c0f3ae3925066160e");
bta[0] = new KEY();
bta[0].bt = g.ToByteArray();
bta[1] = new KEY();
g = new Guid("3f66864d511f7dc43a550900bd795d02");
bta[1].bt = g.ToByteArray();
bta[2] = new KEY();
g = new Guid("c20db9e33a672d1a85e04f1be966a0a1");
bta[2].bt = g.ToByteArray();
bta[3] = new KEY();
g = new Guid("406669e6e9ddcb6ea70c7a8e0cc3ddaf");
bta[3].bt = g.ToByteArray(); //---
}
}
}
這是不可能的﹐除非你的128個字節是通過算法擴展出來的。
比如说把:string k = "1234567890123456";
byte []key = new byte[16];
key = System.Text.Encoding.Default.GetBytes(k);
现在就是 变量k 是上面的那个128位长的字符,怎样才能变为长度为16的byte数组?!
public static byte[] ConvertHexToBytes(string value)
{
int len = value.Length / 2;
byte[] ret = new byte[len];
for (int i = 0; i < len; i++)
ret[i]=(byte)(Convert.ToInt32(value.Substring(i * 2, 2), 16));
return ret;
}