-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexadecimalStream.cs
95 lines (77 loc) · 2.27 KB
/
HexadecimalStream.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Arduino
{
public class HexadecimalStream : Stream
{
Stream stream;
long offset;
public HexadecimalStream(Stream f) {
stream = f;
offset = 0;
}
public override bool CanRead {
get { return true; }
}
public override bool CanWrite {
get { return false; }
}
public override bool CanSeek {
get { return true; }
}
public override void Flush() {
// do nothing
}
public override long Seek(long offset, SeekOrigin origin) {
throw new NotImplementedException();
}
public override long Position {
get {
return offset;
}
set {
Seek(value, SeekOrigin.Begin);
}
}
public override long Length {
get { return stream.Length / 2; }
}
public override void SetLength(long value) {
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count) {
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count) {
for (int i = 0; i < count; i++) {
int b = ReadByte();
if (b < 0) return i;
buffer[offset + i] = (byte)b;
}
return count;
}
int convertHexChar(byte b) {
if (b >= '0' && b <= '9') {
return b - '0';
}
if (b >= 'a' && b <= 'f') {
return b - 'a' + 10;
}
if (b >= 'A' && b <= 'F') {
return b - 'A' + 10;
}
throw new ArgumentException("hex character must be between 0-9a-fA-F");
}
public override int ReadByte() {
int b1 = stream.ReadByte();
int b2 = stream.ReadByte();
if (b1 < 0) return b1;
if (b2 < 0) return b2;
offset++;
return (convertHexChar((byte) b1) << 4) | convertHexChar((byte) b2);
}
}
}