BitConverter.GetBytes实现

前言

我在“浅谈 ZipInteger”一文中的 ZipInteger 结构中使用了 BitConverter 类的 GetBytes 方法。当时我是假设 GetBytes 方法根据 IsLittleEndian 的值不同而按照不同的顺序返回字节数组。但是 MSDN 有关 BitConverter 类的文档中没有对此作出明确的说明。请参见我在 MSDN 论坛的一个贴子“请问 BitConverter.GetBytes 方法以什么顺序返回字节数组”:

在 MSDN 文档的“BigInteger 构造函数 (Byte[])”中提到:

value 数组中的各个字节应该为 little-endian 顺序,从最低序位字节到最高序位字节。 
将数值转换为字节数组的大多数方法,例如 BigInteger.ToByteArray 和 BitConverter.GetBytes,以 little-endian 顺序返回字节数组。

按照我的理解,该 MSDN 文档认为 BitConverter.GetBytes 方法总是以 little-endian 顺序返回字节数组,而不管 BitConverter.IsLittleEndian 的值如何。
而在 BitConverter 类的文档中并没有明确指出 BitConverter.GetBytes 方法应该以什么顺序返回字节数组。
不知我的理解是否正确。

该贴子并没有得到靠谱的答复。

测试程序

那么,我们写个程序来测试一下吧。下面就是 BitConverterTester.cs:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using  System;
 
namespace  Skyiv.Tester
{
   static  class  BitConverterTester
   {
     static  void  Main()
     {
       Console.WriteLine( "      OS Version: "  + Environment.OSVersion);
       Console.WriteLine( "     CLR Version: "  + Environment.Version);
       Console.WriteLine( "  IsLittleEndian: "  + BitConverter.IsLittleEndian);
       long  n = 0x1234567890ABCDEF;
       double  d = 1;
       Console.WriteLine(n.ToString( "X" ) + ": "  + BitConverter.ToString(BitConverter.GetBytes(n)));
       Console.WriteLine(d.ToString( "F14" ) + ": "  + BitConverter.ToString(BitConverter.GetBytes(d)));
     }
   }
}

 

这个程序在 Windows Server 2003 操作系统的 .NET Framework 4 环境下编译和运行:

C:\CS\BitConverterTester> csc BitConverterTester.cs
Microsoft(R) Visual C# 2010 编译器 4.0.30319.1 版
版权所有(C) Microsoft Corporation。保留所有权利。

C:\CS\BitConverterTester> BitConverterTester
      OS Version: Microsoft Windows NT 5.2.3790 Service Pack 2
     CLR Version: 4.0.30319.1
  IsLittleEndian: True
1234567890ABCDEF: EF-CD-AB-90-78-56-34-12
1.00000000000000: 00-00-00-00-00-00-F0-3F

C:\CS\BitConverterTester>

 

在 Ubuntu 10.10 操作系统的 Mono 2.8.2 环境下编译和运行:

ben@ben-m4000t:~/work/BitConverterTester$ dmcs BitConverterTester.cs
ben@ben-m4000t:~/work/BitConverterTester$ mono28 BitConverterTester.exe
      OS Version: Unix 2.6.35.24
     CLR Version: 4.0.30319.1
  IsLittleEndian: True
1234567890ABCDEF: EF-CD-AB-90-78-56-34-12
1.00000000000000: 00-00-00-00-00-00-F0-3F
ben@ben-m4000t:~/work/BitConverterTester$ 

 

这两次运行的结果都在预料之中,BitConverter 类的 GetBytes 方法以 Little-Endian 顺序返回字节数组。但是,在这两次运行中,IsLittleEndian 的值都为 True,所以还是没有解决我们的问题。

查看 Microsoft .NET Framework 4 中相关的源程序代码

祭出 Reflector 这个神器:

在 mscorlib.dll 的 System 命名空间下找到 BitConverter 类:

如上图如示,IsLittleEndian 是 BitConverter 类的静态只读字段。

如上图所示,在 BitConverter 类的静态构造函数中,直接把 IsLittleEndian 这个静态只读字段的值赋值为 true。由于我没有 Microsoft 实现 BitConverter 类的 C# 源程序代码,不知道是 Microsoft 的 C# 源程序中就是直接这样写呢,还是实际上是有根据平台来判断的,但是 C# 编译器在具体平台上优化了这段代码。

如上图所示,GetBytes(Int64) 方法也非常简单,直接通过不安全的指针转换就得到了相应的字节数组。学过 C 语言中的朋友想必非常熟悉这种做法。这下清楚了,BitConverter 类的 GetBytes 方法是根据 IsLittleEndian 的值的不同按照不同顺序来返回字节数组的。MSDN 文档在“BigInteger 构造函数 (Byte[]) ”中的相关说法:“例如 BigInteger.ToByteArray 和 BitConverter.GetBytes,以 little-endian 顺序返回字节数组”是不正确的。

继续看下去:

上图中的 ToInt64 方法也明确地根据 IsLittleEndian 的值采取不同的动作。其实,既然在静态构造函数中明确地给 IsLittleEndian 赋值为 true,这里也可以省略对 IsLittleEndian 的判断,直接按照 IsLittleEndian 的值为 true 去做就行了,可以稍微节省点代码,提高点速度。

我们看到,GetBytes(Double) 方法也非常简单。

查看 Mono 2.8.2 中相关的源代码

我们可以到 http://ftp.novell.com/pub/mono/sources/mono/ 下载 Mono 的源代码,然后按以下方法找到 BitConverter 类的源程序代码:

ben@ben-m4000t:~$ cd src/mono-2.8.2
ben@ben-m4000t:~/src/mono-2.8.2$ find . -name BitConverter.cs
./mcs/class/corlib/System/BitConverter.cs
ben@ben-m4000t:~/src/mono-2.8.2$

下面就是 BitConverter.cs:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// System.BitConverter.cs
//
// Author:
//   Matt Kimball (matt@kimball.net)
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
 
using  System.Text;
 
namespace  System
{
     public
     static
     class  BitConverter
     {
         static  readonly  bool  SwappedWordsInDouble = DoubleWordsAreSwapped ();
         public  static  readonly  bool  IsLittleEndian = AmILittleEndian ();
 
         static  unsafe  bool  AmILittleEndian ()
         {
             // binary representations of 1.0:
             // big endian: 3f f0 00 00 00 00 00 00
             // little endian: 00 00 00 00 00 00 f0 3f
             // arm fpa little endian: 00 00 f0 3f 00 00 00 00
             double  d = 1.0;
             byte  *b = ( byte *)&d;
             return  (b [0] == 0);
         }
 
         static  unsafe  bool  DoubleWordsAreSwapped ()
         {
             // binary representations of 1.0:
             // big endian: 3f f0 00 00 00 00 00 00
             // little endian: 00 00 00 00 00 00 f0 3f
             // arm fpa little endian: 00 00 f0 3f 00 00 00 00
             double  d = 1.0;
             byte  *b = ( byte *)&d;
             return  b [2] == 0xf0;
         }
 
         public  static  long  DoubleToInt64Bits ( double  value)
         {
             return  ToInt64 (GetBytes (value), 0);
         }
 
         public  static  double  Int64BitsToDouble ( long  value)
         {
             return  ToDouble (GetBytes (value), 0);
         }
 
         internal  static  double  InternalInt64BitsToDouble ( long  value)
         {
             return  SwappableToDouble (GetBytes (value), 0);
         }
         
         unsafe  static  byte [] GetBytes ( byte  *ptr, int  count)
         {
             byte  [] ret = new  byte  [count];
 
             for  ( int  i = 0; i < count; i++) {
                 ret [i] = ptr [i];
             }
 
             return  ret;
         }
 
         unsafe  public  static  byte [] GetBytes ( bool  value)
         {
             return  GetBytes (( byte  *) &value, 1);
         }
 
         unsafe  public  static  byte [] GetBytes ( char  value)
         {
             return  GetBytes (( byte  *) &value, 2);
         }
 
         unsafe  public  static  byte [] GetBytes ( short  value)
         {
             return  GetBytes (( byte  *) &value, 2);
         }
 
         unsafe  public  static  byte [] GetBytes ( int  value)
         {
             return  GetBytes (( byte  *) &value, 4);
         }
 
         unsafe  public  static  byte [] GetBytes ( long  value)
         {
             return  GetBytes (( byte  *) &value, 8);
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  byte [] GetBytes ( ushort  value)
         {
             return  GetBytes (( byte  *) &value, 2);
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  byte [] GetBytes ( uint  value)
         {
             return  GetBytes (( byte  *) &value, 4);
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  byte [] GetBytes ( ulong  value)
         {
             return  GetBytes (( byte  *) &value, 8);
         }
 
         unsafe  public  static  byte [] GetBytes ( float  value)
         {
             return  GetBytes (( byte  *) &value, 4);
         }
 
         unsafe  public  static  byte [] GetBytes ( double  value)
         {
             if  (SwappedWordsInDouble) {
                 byte [] data = new  byte  [8];
                 byte  *p = ( byte *)&value;
                 data [0] = p [4];
                 data [1] = p [5];
                 data [2] = p [6];
                 data [3] = p [7];
                 data [4] = p [0];
                 data [5] = p [1];
                 data [6] = p [2];
                 data [7] = p [3];
                 return  data;
             } else  {
                 return  GetBytes (( byte  *) &value, 8);
             }
         }
 
         unsafe  static  void  PutBytes ( byte  *dst, byte [] src, int  start_index, int  count)
         {
             if  (src == null )
                 throw  new  ArgumentNullException ( "value" );
 
             if  (start_index < 0 || (start_index > src.Length - 1))
                 throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                     + " out of range. Must be non-negative and less than the"
                     + " size of the collection." );
 
             // avoid integer overflow (with large pos/neg start_index values)
             if  (src.Length - count < start_index)
                 throw  new  ArgumentException ( "Destination array is not long"
                     + " enough to copy all the items in the collection."
                     + " Check array index and length." );
 
             for  ( int  i = 0; i < count; i++)
                 dst[i] = src[i + start_index];
         }
 
         unsafe  public  static  bool  ToBoolean ( byte [] value, int  startIndex)
         {
             if  (value == null )
                 throw  new  ArgumentNullException ( "value" );
 
             if  (startIndex < 0 || (startIndex > value.Length - 1))
                 throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                     + " out of range. Must be non-negative and less than the"
                     + " size of the collection." );
 
             if  (value [startIndex] != 0)
                 return  true ;
             
             return  false ;
         }
 
         unsafe  public  static  char  ToChar ( byte [] value, int  startIndex)
         {
             char  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 2);
 
             return  ret;
         }
 
         unsafe  public  static  short  ToInt16 ( byte [] value, int  startIndex)
         {
             short  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 2);
 
             return  ret;
         }
 
         unsafe  public  static  int  ToInt32 ( byte [] value, int  startIndex)
         {
             int  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 4);
 
             return  ret;
         }
 
         unsafe  public  static  long  ToInt64 ( byte [] value, int  startIndex)
         {
             long  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 8);
 
             return  ret;
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  ushort  ToUInt16 ( byte [] value, int  startIndex)
         {
             ushort  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 2);
 
             return  ret;
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  uint  ToUInt32 ( byte [] value, int  startIndex)
         {
             uint  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 4);
 
             return  ret;
         }
 
         [CLSCompliant ( false )]
         unsafe  public  static  ulong  ToUInt64 ( byte [] value, int  startIndex)
         {
             ulong  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 8);
 
             return  ret;
         }
 
         unsafe  public  static  float  ToSingle ( byte [] value, int  startIndex)
         {
             float  ret;
 
             PutBytes (( byte  *) &ret, value, startIndex, 4);
 
             return  ret;
         }
 
         unsafe  public  static  double  ToDouble ( byte [] value, int  startIndex)
         {
             double  ret;
 
             if  (SwappedWordsInDouble) {
                 byte * p = ( byte *)&ret;
                 if  (value == null )
                     throw  new  ArgumentNullException ( "value" );
 
                 if  (startIndex < 0 || (startIndex > value.Length - 1))
                     throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                         + " out of range. Must be non-negative and less than the"
                         + " size of the collection." );
 
                 // avoid integer overflow (with large pos/neg start_index values)
                 if  (value.Length - 8 < startIndex)
                     throw  new  ArgumentException ( "Destination array is not long"
                         + " enough to copy all the items in the collection."
                         + " Check array index and length." );
 
                 p [0] = value [startIndex + 4];
                 p [1] = value [startIndex + 5];
                 p [2] = value [startIndex + 6];
                 p [3] = value [startIndex + 7];
                 p [4] = value [startIndex + 0];
                 p [5] = value [startIndex + 1];
                 p [6] = value [startIndex + 2];
                 p [7] = value [startIndex + 3];
 
                 return  ret;
             }
 
             PutBytes (( byte  *) &ret, value, startIndex, 8);
 
             return  ret;
         }
 
         unsafe  internal  static  double  SwappableToDouble ( byte [] value, int  startIndex)
         {
             double  ret;
 
             if  (SwappedWordsInDouble) {
                 byte * p = ( byte *)&ret;
                 if  (value == null )
                     throw  new  ArgumentNullException ( "value" );
 
                 if  (startIndex < 0 || (startIndex > value.Length - 1))
                     throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                         + " out of range. Must be non-negative and less than the"
                         + " size of the collection." );
 
                 // avoid integer overflow (with large pos/neg start_index values)
                 if  (value.Length - 8 < startIndex)
                     throw  new  ArgumentException ( "Destination array is not long"
                         + " enough to copy all the items in the collection."
                         + " Check array index and length." );
 
                 p [0] = value [startIndex + 4];
                 p [1] = value [startIndex + 5];
                 p [2] = value [startIndex + 6];
                 p [3] = value [startIndex + 7];
                 p [4] = value [startIndex + 0];
                 p [5] = value [startIndex + 1];
                 p [6] = value [startIndex + 2];
                 p [7] = value [startIndex + 3];
 
                 return  ret;
             } else  if  (!IsLittleEndian) {
                 byte * p = ( byte *)&ret;
                 if  (value == null )
                     throw  new  ArgumentNullException ( "value" );
 
                 if  (startIndex < 0 || (startIndex > value.Length - 1))
                     throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                         + " out of range. Must be non-negative and less than the"
                         + " size of the collection." );
 
                 // avoid integer overflow (with large pos/neg start_index values)
                 if  (value.Length - 8 < startIndex)
                     throw  new  ArgumentException ( "Destination array is not long"
                         + " enough to copy all the items in the collection."
                         + " Check array index and length." );
 
                 p [0] = value [startIndex + 7];
                 p [1] = value [startIndex + 6];
                 p [2] = value [startIndex + 5];
                 p [3] = value [startIndex + 4];
                 p [4] = value [startIndex + 3];
                 p [5] = value [startIndex + 2];
                 p [6] = value [startIndex + 1];
                 p [7] = value [startIndex + 0];
 
                 return  ret;
             }
 
             PutBytes (( byte  *) &ret, value, startIndex, 8);
 
             return  ret;
         }
         
         public  static  string  ToString ( byte [] value)
         {
             if  (value == null )
                 throw  new  ArgumentNullException ( "value" );
 
             return  ToString (value, 0, value.Length);
         }
 
         public  static  string  ToString ( byte [] value, int  startIndex)
         {
             if  (value == null )
                 throw  new  ArgumentNullException ( "value" );
 
             return  ToString (value, startIndex, value.Length - startIndex);
         }
 
         public  static  string  ToString ( byte [] value, int  startIndex, int  length)
         {
             if  (value == null )
                 throw  new  ArgumentNullException ( "byteArray" );
 
             // The 4th and last clause (start_index >= value.Length)
             // was added as a small fix to a very obscure bug.
             // It makes a small difference when start_index is
             // outside the range and length==0.
             if  (startIndex < 0 || startIndex >= value.Length) {
                 // special (but valid) case (e.g. new byte [0])
                 if  ((startIndex == 0) && (value.Length == 0))
                     return  String.Empty;
                 throw  new  ArgumentOutOfRangeException ( "startIndex" , "Index was"
                     + " out of range. Must be non-negative and less than the"
                     + " size of the collection." );
             }
 
             if  (length < 0)
                 throw  new  ArgumentOutOfRangeException ( "length" ,
                     "Value must be positive." );
 
             // note: re-ordered to avoid possible integer overflow
             if  (startIndex > value.Length - length)
                 throw  new  ArgumentException ( "startIndex + length > value.Length" );
 
             if  (length == 0)
                 return  string .Empty;
 
             StringBuilder builder = new  StringBuilder(length * 3 - 1);
             int  end = startIndex + length;
 
             for  ( int  i = startIndex; i < end; i++) {
                 if  (i > startIndex)
                     builder.Append( '-' );
                 
                 char  high = ( char )((value[i] >> 4) & 0x0f);
                 char  low = ( char )(value[i] & 0x0f);
 
                 if  (high < 10)
                     high += '0' ;
                 else  {
                     high -= ( char ) 10;
                     high += 'A' ;
                 }
 
                 if  (low < 10)
                     low += '0' ;
                 else  {
                     low -= ( char ) 10;
                     low += 'A' ;
                 }
                 builder.Append(high);
                 builder.Append(low);
             }
 
             return  builder.ToString ();
         }
     }
}

 

 

可以看出,在第 39 行通过第 41 到 50 行的 AmILittleEndian 方法给 IsLittleEndian 这个静态只读字段赋值。这个 AmILittleEndian 方法中的注释写得很明白,其实有三种不台的平台,除了 Big-Endian 和 Little-Endian 外,还有一种“arm fpa little endian”,在这里也归于 Little-Endian。但是在后面的方法中是要以不同的手段进行处理的。

另外,不象 Microsoft .NET Framework 4,Mono 中 BitConverter 类的各种重载的公有静态GetBytes 方法是统一调用第 78 到 87 行的私有静态 GetBytes 方法来实现其功能的。

从 Mono 中相关的源程序代码中也可以看出,BitConverter 类的 GetBytes 方法是根据 IsLittleEndian 的值的不同以不同的顺序返回字节数组的。

结论

总结一下,MSDN 文档在“BigInteger 构造函数 (Byte[]) ”中的相关说法:“例如 BigInteger.ToByteArray 和 BitConverter.GetBytes,以 little-endian 顺序返回字节数组”是不正确的。BitConverter 类的 GetBytes 方法是根据 IsLittleEndian 的值的不同以不同的顺序返回字节数组的。

分类:  .NET Framework
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值