forked from PKISolutions/Asn1Editor.WPF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexConverter.cs
45 lines (44 loc) · 1.97 KB
/
HexConverter.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
using System;
using System.Collections.Generic;
using System.Linq;
using SysadminsLV.Asn1Editor.API.ModelObjects;
using SysadminsLV.Asn1Parser;
namespace SysadminsLV.Asn1Editor.API.Utils {
static class HexUtility {
public static String GetHexString(IEnumerable<Byte> rawData, Asn1Lite node) {
Int32 startOffset = node.Offset + node.TagLength - node.PayloadLength;
Int32 endOffset = node.Offset + node.TagLength;
return AsnFormatter.BinaryToString(rawData.ToArray(), EncodingType.Hex, 0, startOffset, endOffset - startOffset);
}
public static String GetHexUString(Byte[] rawData) {
if (rawData == null || rawData.Length == 0) { return String.Empty; }
return AsnFormatter.BinaryToString(rawData).ToUpper();
}
public static String GetHexEditString(Byte[] rawData) {
if (rawData == null || rawData.Length == 0) { return String.Empty; }
return AsnFormatter.BinaryToString(rawData, EncodingType.Hex).ToUpper();
}
public static String BinaryToHex(Byte[] rawData) {
return AsnFormatter.BinaryToString(rawData, EncodingType.Hex, EncodingFormat.NOCR);
}
public static Byte[] HexToBinary(String hexString) {
return AsnFormatter.StringToBinary(hexString, EncodingType.Hex);
}
public static IEnumerable<Byte> AnyToBinary(String anyString) {
try {
return AsnFormatter.StringToBinary(anyString, EncodingType.HexAny);
} catch {
try {
return AsnFormatter.StringToBinary(anyString, EncodingType.StringAny);
} catch {
try {
return AsnFormatter.StringToBinary(anyString, EncodingType.Binary);
} catch {
Tools.MsgBox("Error", "The data is invalid");
return null;
}
}
}
}
}
}