FrmCoder: Hex coding.

This commit is contained in:
2018-03-14 21:34:28 +01:00
parent b8f941de2a
commit bdd857d489
8 changed files with 120 additions and 9 deletions

View File

@@ -0,0 +1,44 @@
using System;
using System.Text;
namespace VAR.Toolbox.Code
{
public class HexUtils
{
public static byte[] HexStringToBytes(string input)
{
int[] hexValues = new int[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
if (input.Length % 2 == 1)
{
throw new Exception("Unenven number of hex digits");
}
byte[] bytes = new byte[input.Length / 2];
int count = input.Length;
for (int x = 0, i = 0; i < count; i += 2, x += 1)
{
bytes[x] = (byte)(
hexValues[Char.ToUpper(input[i + 0]) - '0'] << 4 |
hexValues[Char.ToUpper(input[i + 1]) - '0']
);
}
return bytes;
}
public static string BytesToHexString(byte[] toEncodeAsBytes)
{
string HexAlphabet = "0123456789ABCDEF";
StringBuilder sbOutput = new StringBuilder();
int count = toEncodeAsBytes.Length;
for (int i = 0; i < count; i++)
{
byte b = toEncodeAsBytes[i];
sbOutput.Append(HexAlphabet[(b >> 4)]);
sbOutput.Append(HexAlphabet[(b & 0xF)]);
}
return sbOutput.ToString();
}
}
}

View File

@@ -3,8 +3,10 @@ using System.Text;
namespace VAR.Toolbox.Code
{
public class TextCoderBase64Ascii : ITextCoder
public class TextCoderBase64ToAscii : ITextCoder
{
public const string Name = "Base64ToAscii";
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key)

View File

@@ -3,8 +3,10 @@ using System.Text;
namespace VAR.Toolbox.Code
{
public class TextCoderBase64Utf8 : ITextCoder
public class TextCoderBase64ToUtf8 : ITextCoder
{
public const string Name = "Base64ToUtf8";
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key)

View File

@@ -7,20 +7,30 @@ namespace VAR.Toolbox.Code
public static string[] GetSupportedCoders()
{
return new string[] {
"Base64Ascii",
"Base64Utf8",
TextCoderBase64ToAscii.Name,
TextCoderBase64ToUtf8.Name,
TextCoderHexToUtf8.Name,
TextCoderHexToAscii.Name,
};
}
public static ITextCoder CreateFromName(string name)
{
if (name == "Base64Ascii")
if (name == TextCoderBase64ToAscii.Name)
{
return new TextCoderBase64Ascii();
return new TextCoderBase64ToAscii();
}
if (name == "Base64Utf8")
if (name == TextCoderBase64ToUtf8.Name)
{
return new TextCoderBase64Utf8();
return new TextCoderBase64ToUtf8();
}
if (name == TextCoderHexToUtf8.Name)
{
return new TextCoderHexToUtf8();
}
if (name == TextCoderHexToAscii.Name)
{
return new TextCoderHexToAscii();
}
throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name));
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Text;
namespace VAR.Toolbox.Code
{
class TextCoderHexToAscii : ITextCoder
{
public const string Name = "HexToAscii";
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key)
{
byte[] bytes = HexUtils.HexStringToBytes(input);
string returnValue = Encoding.ASCII.GetString(bytes);
return returnValue;
}
public string Encode(string input, string key)
{
byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(input);
return HexUtils.BytesToHexString(toEncodeAsBytes);
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Text;
namespace VAR.Toolbox.Code
{
class TextCoderHexToUtf8 : ITextCoder
{
public const string Name = "HexToUtf8";
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key)
{
byte[] bytes = HexUtils.HexStringToBytes(input);
string returnValue = Encoding.UTF8.GetString(bytes);
return returnValue;
}
public string Encode(string input, string key)
{
byte[] toEncodeAsBytes = Encoding.UTF8.GetBytes(input);
return HexUtils.BytesToHexString(toEncodeAsBytes);
}
}
}

View File

@@ -12,7 +12,6 @@ namespace VAR.Toolbox.UI
cboCode.Items.AddRange(TextCoderFactory.GetSupportedCoders());
cboCode.SelectedIndex = 1;
}
private ITextCoder _coder = null;

View File

@@ -53,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Code\HexUtils.cs" />
<Compile Include="Code\TextCoderBase64Utf8.cs" />
<Compile Include="Code\TextCoderBase64Ascii.cs" />
<Compile Include="Code\DirectShow\CameraControlProperty.cs" />
@@ -96,6 +97,8 @@
<Compile Include="Code\ProxyCmdExecutorThroughSQLServer.cs" />
<Compile Include="Code\Screenshooter.cs" />
<Compile Include="Code\TextCoderFactory.cs" />
<Compile Include="Code\TextCoderHexToAscii.cs" />
<Compile Include="Code\TextCoderHexToUtf8.cs" />
<Compile Include="Code\User32.cs" />
<Compile Include="Code\Webcam.cs" />
<Compile Include="Code\Win32API.cs" />