Repository navigation
Expand file tree
/
Copy pathHexEncoding.cs
More file actions
155 lines (139 loc) · 4.91 KB
/
Copy pathHexEncoding.cs
File metadata and controls
155 lines (139 loc) · 4.91 KB
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
//------------------------------------------------------------------
// Zune Meta Tag Editor
// Hex Encoding Class
//
// <copyright file="HexEncoding.cs" company="The Drunken Bakery">
// Copyright (c) 2009 The Drunken Bakery. All rights reserved.
// </copyright>
//
// Editor to update WMV meta tags for the Zune
// Encode HEX to byte array.
//
// Author: IRS
// $Revision: 1.1 $
//------------------------------------------------------------------
namespace DrunkenBakery.ZuneTag
{
using System;
using System.Globalization;
using System.Linq;
/// <summary>
/// Summary description for HexEncoding.
/// </summary>
public class HexEncoding
{
/// <summary>
/// Initializes a new instance of the <see cref="HexEncoding" /> class.
/// </summary>
public HexEncoding()
{
// TODO: Add constructor logic here
}
/// <summary>
/// Gets the byte count.
/// </summary>
/// <param name="hexString">The hex string.</param>
/// <returns></returns>
public static int GetByteCount(string hexString)
{
// Remove all none A-F, 0-9, characters
var numHexChars = hexString.Count(IsHexDigit);
// If odd number of characters, discard last character
if (numHexChars % 2 != 0)
{
numHexChars--;
}
return numHexChars / 2; // 2 characters per byte
}
/// <summary>
/// Creates a byte array from the hexadecimal string. Each two characters are combined
/// to create one byte. First two hexadecimal characters become first byte in returned array.
/// Non-hexadecimal characters are ignored.
/// </summary>
/// <param name="hexString">string to convert to byte array.</param>
/// <param name="discarded">number of characters in string ignored.</param>
/// <returns>byte array, in the same left-to-right order as the hexString.</returns>
public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
var newString = string.Empty;
// Remove all none A-F, 0-9, characters
foreach (var t in hexString)
{
if (IsHexDigit(t))
{
newString += t;
}
else
{
discarded++;
}
}
// If odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length - 1);
}
var byteLength = newString.Length / 2;
var bytes = new byte[byteLength];
var j = 0;
for (var i = 0; i < bytes.Length; i++)
{
var hex = new string(new[] { newString[j], newString[j + 1] });
bytes[i] = HexToByte(hex);
j += 2;
}
return bytes;
}
/// <summary>
/// Toes the string.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns></returns>
public static string ToString(byte[] bytes)
{
return bytes.Aggregate(string.Empty, (current, t) => current + t.ToString("X2"));
}
/// <summary>
/// Determines if given string is in proper hexadecimal string format.
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static bool InHexFormat(string hexString)
{
return hexString.All(IsHexDigit);
}
/// <summary>
/// Returns true is c is a hexadecimal digit (A-F, a-f, 0-9).
/// </summary>
/// <param name="c">Character to test.</param>
/// <returns>true if hex digit, false if not.</returns>
public static bool IsHexDigit(char c)
{
var numA = Convert.ToInt32('A');
var num1 = Convert.ToInt32('0');
c = char.ToUpper(c);
var numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < numA + 6)
{
return true;
}
return numChar >= num1 && numChar < num1 + 10;
}
/// <summary>
/// Converts 1 or 2 character string into equivalent byte value.
/// </summary>
/// <param name="hex">1 or 2 character string.</param>
/// <returns>byte.</returns>
private static byte HexToByte(string hex)
{
if (hex.Length > 2 || hex.Length <= 0)
{
throw new ArgumentException("hex must be 1 or 2 characters in length");
}
var newByte = byte.Parse(hex, NumberStyles.HexNumber);
return newByte;
}
}
}