-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigEndianBitConverter.cs
70 lines (66 loc) · 2.66 KB
/
BigEndianBitConverter.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
using System;
using System.Collections.Generic;
using System.Text;
namespace RESTfulFish
{
/// <summary>
/// Implementation of EndianBitConverter which converts to/from big-endian
/// byte arrays.
/// </summary>
public sealed class BigEndianBitConverter : EndianBitConverter
{
/// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
/// <remarks>
/// Different computer architectures store data using different byte orders. "Big-endian"
/// means the most significant byte is on the left end of a word. "Little-endian" means the
/// most significant byte is on the right end of a word.
/// </remarks>
/// <returns>true if this converter is little-endian, false otherwise.</returns>
public sealed override bool IsLittleEndian()
{
return false;
}
/// <summary>
/// Indicates the byte order ("endianess") in which data is converted using this class.
/// </summary>
public sealed override Endianness Endianness
{
get { return Endianness.BigEndian; }
}
/// <summary>
/// Copies the specified number of bytes from value to buffer, starting at index.
/// </summary>
/// <param name="value">The value to copy</param>
/// <param name="bytes">The number of bytes to copy</param>
/// <param name="buffer">The buffer to copy the bytes into</param>
/// <param name="index">The index to start at</param>
protected override void CopyBytesImpl(long value, int bytes, byte[] buffer, int index)
{
int endOffset = index + bytes - 1;
for (int i = 0; i < bytes; i++)
{
buffer[endOffset - i] = unchecked((byte)(value & 0xff));
value = value >> 8;
}
}
/// <summary>
/// Returns a value built from the specified number of bytes from the given buffer,
/// starting at index.
/// </summary>
/// <param name="buffer">The data in byte array format</param>
/// <param name="startIndex">The first index to use</param>
/// <param name="bytesToConvert">The number of bytes to use</param>
/// <returns>The value built from the given bytes</returns>
protected override long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
{
long ret = 0;
for (int i = 0; i < bytesToConvert; i++)
{
ret = unchecked((ret << 8) | buffer[startIndex + i]);
}
return ret;
}
}
}