您的位置:程序门 -> .net技术 -> c#



急,位到字节的转换


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


急,位到字节的转换[已结贴,结贴人:foxpan]
发表于:2007-03-13 14:23:24 楼主
请教大虾们,如何将下面的128位转换为16字节的byte数组?急用
33832964bf7a3b7c0f3ae3925066160e
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
发表于:2007-03-13 14:31:331楼 得分:0
顶一下
发表于:2007-03-13 14:32:242楼 得分:0
记下,一会儿帮你解决
发表于:2007-03-13 14:33:313楼 得分:0
你这些数字是来自guid吗?
如果是可以用guid来表示就是了.
发表于:2007-03-13 14:34:404楼 得分:0
guid   是一个   128   位整数(16   字节),可用于所有需要唯一标识符的计算机和网络。此标识符重复的可能性非常小。

用法如下:

guid   g   =   new   guid( "33832964bf7a3b7c0f3ae3925066160e ");
发表于:2007-03-13 14:35:475楼 得分:10
要表示成字节数组就简单了:
guid   g   =   new   guid( "33832964bf7a3b7c0f3ae3925066160e ");
byte[]   bytes   =   g.tobytearray();
发表于:2007-03-13 14:37:356楼 得分:0
hbxtlhx太油菜了
发表于:2007-03-13 14:39:017楼 得分:0
问题是要把所有的都转化为一个16位字节数组啊!
发表于:2007-03-13 14:39:168楼 得分:0
guid   guid=guid.newguid();
byte[]   bytes=guid.tobytearray();
console.writeline(bitconverter.tostring(bytes));
发表于:2007-03-13 14:40:239楼 得分:0
guid   g   =   new   guid( "33832964bf7a3b7c0f3ae3925066160e3f66864d511f7dc43a550900bd795d02
                                    c20db9e33a672d1a85e04f1be966a0a1406669e6e9ddcb6ea70c7a8e0cc3ddaf ");
byte[]   bytes   =   g.tobytearray();
这样能够是长度为16的字节数组吗?!
发表于:2007-03-13 14:46:0610楼 得分:0
33832964bf7a3b7c0f3ae3925066160e
得到:
64-29-83-33-7a-bf-7c-3b-0f-3a-e3-92-50-66-16-0e

测试的结果,估计有点问题...
前面4个字节位置相反,第5个字节和第6个字节位置也相反...

楼主,你有四排字符串,你要得到四个数组还是1个数组?
把输出结果写出来先
发表于:2007-03-13 14:48:0011楼 得分:0
guid的参数32位的数字。
以上方法不可用。
发表于:2007-03-13 14:49:4412楼 得分:40
自已做個簡單的函數就可以了啊﹗
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 ";
}
}
}
发表于:2007-03-13 14:50:5013楼 得分:0
4个字符串得到一个长度为16的数组
发表于:2007-03-13 14:51:1814楼 得分:20
33832964bf7a3b7c0f3ae3925066160e
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);
发表于:2007-03-13 14:52:5815楼 得分:0
2个2个一扫   最原始的方法
发表于:2007-03-13 14:54:5916楼 得分:20
4个字符串得到一个长度为16的数组

33832964bf7a3b7c0f3ae3925066160e
3f66864d511f7dc43a550900bd795d02
c20db9e33a672d1a85e04f1be966a0a1
406669e6e9ddcb6ea70c7a8e0cc3ddaf
--------------------------------
得到的结果是?运算的规则是?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

共有16   *   4   个字节
除非这个数组类型是4个字节比如int32
楼主先说清楚你的需求
发表于:2007-03-13 15:00:5517楼 得分:0
上面的是3des的密钥,但是3des密钥是长度为16或24的byte数组,给定的是128位(1个字节=8位)所以以上的应该能转化为长度为16的数组!
发表于:2007-03-13 15:02:5518楼 得分:0
上面得到的是字符型表示的數組﹔將它轉換成byte數組就可以了,或者你在inttobytes()函數中可以直接返回一個byte數組﹐在getbytes中將它們放在一個長數組中就可以了.
例﹕
private   byte[]   inttobytes(char   t)
{
switch(t)
{
case   '0 ':
{
return   new   byte[]{(byte)0,(byte)0,(byte)0,(byte)0};
}
....
}
}
发表于:2007-03-13 15:03:5119楼 得分:0
byte   []key   =   new   byte[16]
怎样将33832964bf7a3b7c0f3ae3925066160e
              3f66864d511f7dc43a550900bd795d02
              c20db9e33a672d1a85e04f1be966a0a1
              406669e6e9ddcb6ea70c7a8e0cc3ddaf)
发表于:2007-03-13 15:04:3620楼 得分:0
变为byte存在key中
发表于:2007-03-13 15:07:0821楼 得分:0
楼主的128位16进制的数转成byte数组的长度是64字节..
是你数错了?还是想压缩啊?
发表于:2007-03-13 15:07:5122楼 得分:0
上面的是3des的密钥,但是3des密钥是长度为16或24的byte数组,给定的是128位(1个字节=8位)所以以上的应该能转化为长度为16的数组!
===========================

開始的需求沒有說清楚~~
发表于:2007-03-13 15:11:5523楼 得分:10
using   system;
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();

                        //---
                }
        }
}
发表于:2007-03-13 15:12:1824楼 得分:0
128個字節怎么能放得進長度為16的byte[]中啊﹖
這是不可能的﹐除非你的128個字節是通過算法擴展出來的。
发表于:2007-03-13 15:15:5525楼 得分:0
sqfeiyu(流星雨)   大虾,那有什么方式解决吗?!
发表于:2007-03-13 15:18:0926楼 得分:0
可以把上面的看成128位吗!?
发表于:2007-03-13 15:23:1227楼 得分:0
按照你的输入
输出的应该是什么?给结果参考一下阿.
发表于:2007-03-13 15:27:5728楼 得分:0
hbicexp(飘逝如风)   要是有输出也好啊,但我没有,可以举个例子!
比如说把:string   k   =   "1234567890123456 ";
byte   []key   =   new   byte[16];
key   =   system.text.encoding.default.getbytes(k);
现在就是   变量k   是上面的那个128位长的字符,怎样才能变为长度为16的byte数组?!
发表于:2007-03-13 15:29:1429楼 得分:0
只能舍弃掉一部分了.
装不下阿.
发表于:2007-03-13 16:04:1830楼 得分:0
不可能直接轉換的﹐如果是3des的密钥的話,應該有一定的轉換算法﹐所以你最好去找找它的加密算法﹐否則是沒有辦法的。
发表于:2007-03-13 16:40:4231楼 得分:0
学习一下!!!
发表于:2007-03-13 22:05:2132楼 得分:0
参考了
发表于:2007-03-13 22:55:0333楼 得分:0
我查了一下,估计是不能转换的,结帖吧!谢谢大家!^_^
发表于:2007-07-10 16:57:0334楼 得分:0
将16进制字符串转化为字节数组
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;
}


快速检索

最新资讯
热门点击