将ulong表示为字节数组 [英] Representing a ulong as An Array of Bytes
问题描述
我需要在一个字节数组中存储一个uint。做老式的
风格作品:
byte [] b =新字节[8];
ulong ul = 1 ;
for(int j = 0; j< 8; j ++)
{
---显然我可以(并且确实)优化循环中的代码
---我只想保持我的例子简单
b [7-j] =(byte)((ul& bmask)> j * 8);
bmask = bmask<< 8;
}
它给了我我需要的东西:
b [0] = 0;
b [1] = 0;
(剪辑)
b [7] = 1;
我试图使用MemoryStream和BinaryWriter,看看它的执行速度有多快:
MemoryStream ms = new MemoryStream(16);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i = 1; i< 3; i ++)
{
br 。写(i);
}
byte [] b = ms.ToArray();
不幸的是顺序字节不是我想要的:
b [0] = 1;
b [1] = 0;
b [2] = 0;
(剪辑)
b [7] = 0;
有没有办法使用像MemoryStream这样的固定类和
BinaryWriter并获得我从第一个片段获得的结果?我要问的原因很简单:罐头类的表现可能比我自制的解决方案表现得更快。
I need to store a uint in an array of bytes. Doing it old fashioned
style works:
byte[] b=new byte[8];
ulong ul = 1;
for(int j=0; j<8; j++)
{
--- obviously I could (and did) optimize the code inside the loop
--- I just wanted to keep my example simple
b[7 - j] = (byte)((ul & bmask) >j*8);
bmask = bmask << 8;
}
It gives me exactly what I need:
b[0] = 0;
b[1] = 0;
(snip)
b[7] = 1;
I was trying to use MemoryStream and BinaryWriter and see how fast it
performs:
MemoryStream ms = new MemoryStream(16);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i=1; i<3; i++)
{
br.Write(i);
}
byte[] b = ms.ToArray();
Unfortunately the order of bytes is not what I want:
b[0] = 1;
b[1] = 0;
b[2] = 0;
(snip)
b[7] = 0;
Is there any way to use canned classes like MemoryStream and
BinaryWriter and get the results that I got from my first snippet? The
reason I am asking is simple: canned classes might perform faster than
my homemade solution.
推荐答案
尝试BitConverter类
http://msdn2.microsoft.com/en-us/library/d8z32tce.aspx 应该是您寻求的
重载(对于ulong)
干杯,
Greg Young
MVP - C# http://codebetter.com/blogs/gregyoung
< AK ******** **** @ hotmail.COMwrote in message
news:11 ********************** @ p79g2000cwp.googlegr psps.com ...
Try the BitConverter class
http://msdn2.microsoft.com/en-us/library/d8z32tce.aspx should be the
overload you seek (for a ulong)
Cheers,
Greg Young
MVP - C# http://codebetter.com/blogs/gregyoung
<AK************@hotmail.COMwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>我需要在一个字节数组中存储一个uint。做老式的
风格作品:
byte [] b =新字节[8];
ulong ul = 1 ;
for(int j = 0; j< 8; j ++)
{
---显然我可以(并且确实)优化循环中的代码
---我只想保持我的例子简单
b [7-j] =(byte)((ul& bmask)> j * 8);
bmask = bmask<< 8;
}
它给了我我需要的东西:
b [0] = 0;
b [1] = 0;
(剪辑)
b [7] = 1;
我试图使用MemoryStream和BinaryWriter,看看它的执行速度有多快:
MemoryStream ms = new MemoryStream(16);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i = 1; i< 3; i ++)
{
br 。写(i);
}
byte [] b = ms.ToArray();
不幸的是顺序字节不是我想要的:
b [0] = 1;
b [1] = 0;
b [2] = 0;
(剪辑)
b [7] = 0;
有没有办法使用像MemoryStream这样的固定类和
BinaryWriter并获得我从第一个片段获得的结果?我要问的原因很简单:罐头类的表现可能比我自制的解决方案表现得更快。
>I need to store a uint in an array of bytes. Doing it old fashioned
style works:
byte[] b=new byte[8];
ulong ul = 1;
for(int j=0; j<8; j++)
{
--- obviously I could (and did) optimize the code inside the loop
--- I just wanted to keep my example simple
b[7 - j] = (byte)((ul & bmask) >j*8);
bmask = bmask << 8;
}
It gives me exactly what I need:
b[0] = 0;
b[1] = 0;
(snip)
b[7] = 1;
I was trying to use MemoryStream and BinaryWriter and see how fast it
performs:
MemoryStream ms = new MemoryStream(16);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i=1; i<3; i++)
{
br.Write(i);
}
byte[] b = ms.ToArray();
Unfortunately the order of bytes is not what I want:
b[0] = 1;
b[1] = 0;
b[2] = 0;
(snip)
b[7] = 0;
Is there any way to use canned classes like MemoryStream and
BinaryWriter and get the results that I got from my first snippet? The
reason I am asking is simple: canned classes might perform faster than
my homemade solution.
谢谢格雷格。我正在比较不同的方式将大量的
无符号长整数发送到SQL Server 2000.解决方案需要非常好的b / b
performant。在其他替代方案中,我向数据库发送了一个
图像,如下所示:
SqlCommand a = new SqlCommand(" ImageTest",conn);
a.CommandType = System.Data.CommandType.StoredProcedure;
a.Parameters.Add(new SqlParameter(" @ image",
System.Data.SqlDbType.Image,2147483647));
- b是一个字节数组
a.Parameters [0] .Value = b;
我的理解是BitConverter会为每个unsigned long创建一个新的字节数组
,我认为这对垃圾来说很难
收藏家。我的理解是,在我的本地PC上进行短暂测试时,我很可能不会注意到这一点,但最终在制作中,b $ b会导致速度减慢。所以我估计在我的情况下,手动交换字节更好
这样只有2个新对象,因为
与10001相反:
MemoryStream ms = new MemoryStream(80000);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i = 1; i< 10001 ; i ++)
{
br.Write(i);
}
byte [] b = ms .ToArray();
byte [] b1 =新字节[80000];
---手动交换字节
for(k = 0; k <80000; k + = 8)
{
for(j = 0; j <8; j ++)
{
b1 [k + 7-j] = b [k + j];
}
}
如果我错了请纠正我。
Thank you Greg. I am comparing different ways to send a large array of
unsigned longs to SQL Server 2000. The solution needs to be very
performant. Among other alternatives, I am sending to the database an
image, as follows:
SqlCommand a = new SqlCommand("ImageTest", conn);
a.CommandType = System.Data.CommandType.StoredProcedure;
a.Parameters.Add(new SqlParameter("@image",
System.Data.SqlDbType.Image,2147483647));
-- b is an array of bytes
a.Parameters[0].Value = b;
My understanding is that BitConverter will create a new array of bytes
for every unsigned long, and I think that would be hard on the garbage
collector. My understanding is that most likely I will not notice that
in a short test on my local PC, but eventually in the production that
would cause a slowdown. So I estimate that in my situation it is better
to swap then bytes manually so that there are only 2 new objects as
opposed to 10001:
MemoryStream ms = new MemoryStream(80000);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i=1; i<10001; i++)
{
br.Write(i);
}
byte[] b = ms.ToArray();
byte[] b1 = new byte[80000];
--- swap the bytes manually
for(k=0; k<80000; k+=8)
{
for(j=0; j<8; j++)
{
b1[k+7-j] = b[k+j];
}
}
Please correct me if I am wrong.
或者你可以使用BinaryWriter和内存流..二进制
作家实际上以比你更有效的方式做到这一点..
反射器'的写入方法来源。
public virtual void写(长值)
{
this._buffer [0] =(byte)value;
this._buffer [1] =(byte)(value> 8);
this._buffer [2] =(byte)(value> 0x10);
this._buffer [3] =(byte)(value> 0x18);
this._buffer [4] =(byte)(value> 0x20);
this._buffer [5] =(byte)(value> 40);
this._buffer [6] =(byte)(value> 0x30);
this._buffer [7] =(byte)(value> 0x38);
this.OutStream.Write(this._buffer,0,8);
}
干杯,
Greg Young
MVP - C#
http://codebetter.com / blogs / gregyoung
< AK ************ @ hotmail.COMwrote in message
news :11 ********************** @ 75g2000cwc.googlegro ups.com ...
Or you could just use a BinaryWriter and a memory stream .. the binary
writer actually does this in a more effiicient way than you do..
reflector''ed source of write method.
public virtual void Write(long value)
{
this._buffer[0] = (byte) value;
this._buffer[1] = (byte) (value >8);
this._buffer[2] = (byte) (value >0x10);
this._buffer[3] = (byte) (value >0x18);
this._buffer[4] = (byte) (value >0x20);
this._buffer[5] = (byte) (value >40);
this._buffer[6] = (byte) (value >0x30);
this._buffer[7] = (byte) (value >0x38);
this.OutStream.Write(this._buffer, 0, 8);
}
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
<AK************@hotmail.COMwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
谢谢Greg。我正在比较不同的方式将大量的
无符号长整数发送到SQL Server 2000.解决方案需要非常好的b / b
performant。在其他替代方案中,我向数据库发送了一个
图像,如下所示:
SqlCommand a = new SqlCommand(" ImageTest",conn);
a.CommandType = System.Data.CommandType.StoredProcedure;
a.Parameters.Add(new SqlParameter(" @ image",
System.Data.SqlDbType.Image,2147483647));
- b是一个字节数组
a.Parameters [0] .Value = b;
我的理解是BitConverter会为每个unsigned long创建一个新的字节数组
,我认为这对垃圾来说很难
收藏家。我的理解是,在我的本地PC上进行短暂测试时,我很可能不会注意到这一点,但最终在制作中,b $ b会导致速度减慢。所以我估计在我的情况下,手动交换字节更好
这样只有2个新对象,因为
与10001相反:
MemoryStream ms = new MemoryStream(80000);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i = 1; i< 10001 ; i ++)
{
br.Write(i);
}
byte [] b = ms .ToArray();
byte [] b1 =新字节[80000];
---手动交换字节
for(k = 0; k <80000; k + = 8)
{
for(j = 0; j <8; j ++)
{
b1 [k + 7-j] = b [k + j];
}
}
如果我错了,请纠正我。
Thank you Greg. I am comparing different ways to send a large array of
unsigned longs to SQL Server 2000. The solution needs to be very
performant. Among other alternatives, I am sending to the database an
image, as follows:
SqlCommand a = new SqlCommand("ImageTest", conn);
a.CommandType = System.Data.CommandType.StoredProcedure;
a.Parameters.Add(new SqlParameter("@image",
System.Data.SqlDbType.Image,2147483647));
-- b is an array of bytes
a.Parameters[0].Value = b;
My understanding is that BitConverter will create a new array of bytes
for every unsigned long, and I think that would be hard on the garbage
collector. My understanding is that most likely I will not notice that
in a short test on my local PC, but eventually in the production that
would cause a slowdown. So I estimate that in my situation it is better
to swap then bytes manually so that there are only 2 new objects as
opposed to 10001:
MemoryStream ms = new MemoryStream(80000);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i=1; i<10001; i++)
{
br.Write(i);
}
byte[] b = ms.ToArray();
byte[] b1 = new byte[80000];
--- swap the bytes manually
for(k=0; k<80000; k+=8)
{
for(j=0; j<8; j++)
{
b1[k+7-j] = b[k+j];
}
}
Please correct me if I am wrong.
这篇关于将ulong表示为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!