-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToHexString.linq
53 lines (45 loc) · 1022 Bytes
/
ToHexString.linq
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
<Query Kind="Program" />
void Main()
{
byte[] x = new byte[] { 0, 1, 2, 99 };
StringExt.HexStr(x).Dump();
StringExt.HexStr(x, true).Dump();
x.ToHexString().Dump();
x.ToHexString(true).Dump();
}
//todo: Hex to Byte array
// PZahra
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/3928b8cb-3703-4672-8ccd-33718148d1e3/byte-array-to-hex-string?forum=csharpgeneral#06b6d378-9463-4071-b8cc-e4f1228303c5
static class StringExt
{
public static string ToHexString(this byte[] a, bool prefix = false)
{
return HexStr(a, prefix);
}
public static string HexStr(byte[] p, bool prefix = false)
{
char[] c=null;
byte b;
int xstart;
if(prefix)
{
c=new char[p.Length*2 + 2];
c[0]='0';
c[1]='x';
xstart = 2;
}
else
{
c=new char[p.Length*2];
xstart = 0;
}
for(int y=0, x=xstart; y<p.Length; ++y, ++x)
{
b=((byte)(p[y]>>4));
c[x]=(char)(b>9 ? b+0x37 : b+0x30);
b=((byte)(p[y]&0xF));
c[++x]=(char)(b>9 ? b+0x37 : b+0x30);
}
return new string(c);
}
}