Text conversion improvements

This commit is contained in:
Jaex 2016-01-02 15:24:14 +02:00
parent 27e8828ce7
commit d8d379a04f
3 changed files with 90 additions and 71 deletions

View file

@ -38,9 +38,14 @@ public class Translator
public string BinaryText public string BinaryText
{ {
get get
{
if (Binary != null && Binary.Length > 0)
{ {
return Binary.Join(); return Binary.Join();
} }
return null;
}
} }
// http://en.wikipedia.org/wiki/Hexadecimal // http://en.wikipedia.org/wiki/Hexadecimal
@ -49,9 +54,14 @@ public string BinaryText
public string HexadecimalText public string HexadecimalText
{ {
get get
{
if (Hexadecimal != null && Hexadecimal.Length > 0)
{ {
return Hexadecimal.Join().ToUpperInvariant(); return Hexadecimal.Join().ToUpperInvariant();
} }
return null;
}
} }
// http://en.wikipedia.org/wiki/ASCII // http://en.wikipedia.org/wiki/ASCII
@ -60,9 +70,14 @@ public string HexadecimalText
public string ASCIIText public string ASCIIText
{ {
get get
{
if (ASCII != null && ASCII.Length > 0)
{ {
return ASCII.Join(); return ASCII.Join();
} }
return null;
}
} }
// http://en.wikipedia.org/wiki/Base64 // http://en.wikipedia.org/wiki/Base64
@ -85,10 +100,20 @@ public string ASCIIText
// http://en.wikipedia.org/wiki/RIPEMD // http://en.wikipedia.org/wiki/RIPEMD
public string RIPEMD160 { get; private set; } public string RIPEMD160 { get; private set; }
public void Clear()
{
Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = RIPEMD160 = null;
Binary = null;
Hexadecimal = null;
ASCII = null;
}
public bool EncodeText(string text) public bool EncodeText(string text)
{ {
try try
{ {
Clear();
if (!string.IsNullOrEmpty(text)) if (!string.IsNullOrEmpty(text))
{ {
Text = text; Text = text;
@ -117,13 +142,14 @@ public bool DecodeBinary(string binary)
{ {
try try
{ {
string result = TranslatorHelper.BinaryToText(binary); Text = TranslatorHelper.BinaryToText(binary);
return EncodeText(result); return !string.IsNullOrEmpty(Text);
} }
catch catch
{ {
} }
Text = null;
return false; return false;
} }
@ -131,13 +157,14 @@ public bool DecodeHex(string hex)
{ {
try try
{ {
string result = TranslatorHelper.HexadecimalToText(hex); Text = TranslatorHelper.HexadecimalToText(hex);
return EncodeText(result); return !string.IsNullOrEmpty(Text);
} }
catch catch
{ {
} }
Text = null;
return false; return false;
} }
@ -145,13 +172,14 @@ public bool DecodeASCII(string ascii)
{ {
try try
{ {
string result = TranslatorHelper.ASCIIToText(ascii); Text = TranslatorHelper.ASCIIToText(ascii);
return EncodeText(result); return !string.IsNullOrEmpty(Text);
} }
catch catch
{ {
} }
Text = null;
return false; return false;
} }
@ -159,33 +187,17 @@ public bool DecodeBase64(string base64)
{ {
try try
{ {
string result = TranslatorHelper.Base64ToText(base64); Text = TranslatorHelper.Base64ToText(base64);
return EncodeText(result); return !string.IsNullOrEmpty(Text);
} }
catch catch
{ {
} }
Text = null;
return false; return false;
} }
public static bool Test()
{
bool result = true;
Translator translator = new Translator();
string text = Helpers.GetAllCharacters();
translator.EncodeText(text);
translator.DecodeBinary(translator.BinaryText);
result &= translator.Text == text;
translator.DecodeHex(translator.HexadecimalText);
result &= translator.Text == text;
//translator.DecodeASCII(translator.ASCIIText);
//result &= translator.Text == text;
translator.DecodeBase64(translator.Base64);
result &= translator.Text == text;
return result;
}
public string HashToString() public string HashToString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -204,7 +216,7 @@ public override string ToString()
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendLine($"Text: {Text}"); sb.AppendLine($"Text: {Text}");
sb.AppendLine($"Binary: {BinaryText}"); sb.AppendLine($"Binary: {BinaryText}");
sb.AppendLine($"Hex: {HexadecimalText}"); sb.AppendLine($"Hexadecimal: {HexadecimalText}");
sb.AppendLine($"ASCII: {ASCIIText}"); sb.AppendLine($"ASCII: {ASCIIText}");
sb.AppendLine($"Base64: {Base64}"); sb.AppendLine($"Base64: {Base64}");
sb.Append(HashToString()); sb.Append(HashToString());

View file

@ -47,10 +47,6 @@ public HashCheckForm()
hashCheck.FileCheckCompleted += fileCheck_FileCheckCompleted; hashCheck.FileCheckCompleted += fileCheck_FileCheckCompleted;
translator = new Translator(); translator = new Translator();
#if DEBUG
if (!Translator.Test()) MessageBox.Show("Text conversion failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
} }
#region File hash check #region File hash check
@ -143,9 +139,17 @@ private void txtFilePath_DragDrop(object sender, DragEventArgs e)
#region Text conversions #region Text conversions
private void FillConversionInfo() private void FillConversionInfo()
{
FillConversionInfo(translator.Text);
}
private void FillConversionInfo(string text)
{ {
if (translator != null) if (translator != null)
{ {
if (!string.IsNullOrEmpty(text))
{
translator.EncodeText(text);
txtHashCheckText.Text = translator.Text; txtHashCheckText.Text = translator.Text;
txtHashCheckBinary.Text = translator.BinaryText; txtHashCheckBinary.Text = translator.BinaryText;
txtHashCheckHex.Text = translator.HexadecimalText; txtHashCheckHex.Text = translator.HexadecimalText;
@ -153,11 +157,17 @@ private void FillConversionInfo()
txtHashCheckBase64.Text = translator.Base64; txtHashCheckBase64.Text = translator.Base64;
txtHashCheckHash.Text = translator.HashToString(); txtHashCheckHash.Text = translator.HashToString();
} }
else
{
translator.Clear();
txtHashCheckText.Text = txtHashCheckBinary.Text = txtHashCheckHex.Text = txtHashCheckASCII.Text = txtHashCheckBase64.Text = txtHashCheckHash.Text = "";
}
}
} }
private void btnHashCheckCopyAll_Click(object sender, EventArgs e) private void btnHashCheckCopyAll_Click(object sender, EventArgs e)
{ {
if (translator != null && !string.IsNullOrEmpty(translator.Text)) if (translator != null)
{ {
string text = translator.ToString(); string text = translator.ToString();
@ -170,42 +180,39 @@ private void btnHashCheckCopyAll_Click(object sender, EventArgs e)
private void btnHashCheckEncodeText_Click(object sender, EventArgs e) private void btnHashCheckEncodeText_Click(object sender, EventArgs e)
{ {
if (translator.EncodeText(txtHashCheckText.Text)) FillConversionInfo(txtHashCheckText.Text);
{
FillConversionInfo();
}
} }
private void btnHashCheckDecodeBinary_Click(object sender, EventArgs e) private void btnHashCheckDecodeBinary_Click(object sender, EventArgs e)
{ {
if (translator.DecodeBinary(txtHashCheckBinary.Text)) string binary = txtHashCheckBinary.Text;
{ translator.DecodeBinary(binary);
FillConversionInfo(); FillConversionInfo();
} txtHashCheckBinary.Text = binary;
} }
private void btnHashCheckDecodeHex_Click(object sender, EventArgs e) private void btnHashCheckDecodeHex_Click(object sender, EventArgs e)
{ {
if (translator.DecodeHex(txtHashCheckHex.Text)) string hex = txtHashCheckHex.Text;
{ translator.DecodeHex(hex);
FillConversionInfo(); FillConversionInfo();
} txtHashCheckHex.Text = hex;
} }
private void btnHashCheckDecodeASCII_Click(object sender, EventArgs e) private void btnHashCheckDecodeASCII_Click(object sender, EventArgs e)
{ {
if (translator.DecodeASCII(txtHashCheckASCII.Text)) string ascii = txtHashCheckASCII.Text;
{ translator.DecodeASCII(ascii);
FillConversionInfo(); FillConversionInfo();
} txtHashCheckASCII.Text = ascii;
} }
private void btnHashCheckDecodeBase64_Click(object sender, EventArgs e) private void btnHashCheckDecodeBase64_Click(object sender, EventArgs e)
{ {
if (translator.DecodeBase64(txtHashCheckBase64.Text)) string base64 = txtHashCheckBase64.Text;
{ translator.DecodeBase64(base64);
FillConversionInfo(); FillConversionInfo();
} txtHashCheckBase64.Text = base64;
} }
#endregion Text conversions #endregion Text conversions

View file

@ -195,7 +195,7 @@
<value>True</value> <value>True</value>
</data> </data>
<data name="lblResult.Location" type="System.Drawing.Point, System.Drawing"> <data name="lblResult.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 104</value> <value>5, 136</value>
</data> </data>
<data name="lblResult.Size" type="System.Drawing.Size, System.Drawing"> <data name="lblResult.Size" type="System.Drawing.Size, System.Drawing">
<value>40, 13</value> <value>40, 13</value>
@ -222,7 +222,7 @@
<value>True</value> <value>True</value>
</data> </data>
<data name="lblTarget.Location" type="System.Drawing.Point, System.Drawing"> <data name="lblTarget.Location" type="System.Drawing.Point, System.Drawing">
<value>5, 152</value> <value>5, 184</value>
</data> </data>
<data name="lblTarget.Size" type="System.Drawing.Size, System.Drawing"> <data name="lblTarget.Size" type="System.Drawing.Size, System.Drawing">
<value>41, 13</value> <value>41, 13</value>
@ -249,7 +249,7 @@
<value>True</value> <value>True</value>
</data> </data>
<data name="lblProgressPercentage.Location" type="System.Drawing.Point, System.Drawing"> <data name="lblProgressPercentage.Location" type="System.Drawing.Point, System.Drawing">
<value>528, 76</value> <value>432, 110</value>
</data> </data>
<data name="lblProgressPercentage.Size" type="System.Drawing.Size, System.Drawing"> <data name="lblProgressPercentage.Size" type="System.Drawing.Size, System.Drawing">
<value>21, 13</value> <value>21, 13</value>
@ -273,7 +273,7 @@
<value>11</value> <value>11</value>
</data> </data>
<data name="btnStartHashCheck.Location" type="System.Drawing.Point, System.Drawing"> <data name="btnStartHashCheck.Location" type="System.Drawing.Point, System.Drawing">
<value>120, 70</value> <value>8, 104</value>
</data> </data>
<data name="btnStartHashCheck.Size" type="System.Drawing.Size, System.Drawing"> <data name="btnStartHashCheck.Size" type="System.Drawing.Size, System.Drawing">
<value>128, 24</value> <value>128, 24</value>
@ -300,7 +300,7 @@
<value>8, 72</value> <value>8, 72</value>
</data> </data>
<data name="cbHashType.Size" type="System.Drawing.Size, System.Drawing"> <data name="cbHashType.Size" type="System.Drawing.Size, System.Drawing">
<value>104, 21</value> <value>128, 21</value>
</data> </data>
<data name="cbHashType.TabIndex" type="System.Int32, mscorlib"> <data name="cbHashType.TabIndex" type="System.Int32, mscorlib">
<value>4</value> <value>4</value>
@ -318,10 +318,10 @@
<value>7</value> <value>7</value>
</data> </data>
<data name="pbProgress.Location" type="System.Drawing.Point, System.Drawing"> <data name="pbProgress.Location" type="System.Drawing.Point, System.Drawing">
<value>256, 70</value> <value>144, 104</value>
</data> </data>
<data name="pbProgress.Size" type="System.Drawing.Size, System.Drawing"> <data name="pbProgress.Size" type="System.Drawing.Size, System.Drawing">
<value>264, 24</value> <value>280, 24</value>
</data> </data>
<data name="pbProgress.TabIndex" type="System.Int32, mscorlib"> <data name="pbProgress.TabIndex" type="System.Int32, mscorlib">
<value>7</value> <value>7</value>
@ -339,7 +339,7 @@
<value>6</value> <value>6</value>
</data> </data>
<data name="txtResult.Location" type="System.Drawing.Point, System.Drawing"> <data name="txtResult.Location" type="System.Drawing.Point, System.Drawing">
<value>8, 120</value> <value>8, 152</value>
</data> </data>
<data name="txtResult.Size" type="System.Drawing.Size, System.Drawing"> <data name="txtResult.Size" type="System.Drawing.Size, System.Drawing">
<value>552, 20</value> <value>552, 20</value>
@ -360,7 +360,7 @@
<value>4</value> <value>4</value>
</data> </data>
<data name="txtTarget.Location" type="System.Drawing.Point, System.Drawing"> <data name="txtTarget.Location" type="System.Drawing.Point, System.Drawing">
<value>8, 168</value> <value>8, 200</value>
</data> </data>
<data name="txtTarget.Size" type="System.Drawing.Size, System.Drawing"> <data name="txtTarget.Size" type="System.Drawing.Size, System.Drawing">
<value>552, 20</value> <value>552, 20</value>
@ -733,13 +733,13 @@
<value>5, 120</value> <value>5, 120</value>
</data> </data>
<data name="lblHashCheckHex.Size" type="System.Drawing.Size, System.Drawing"> <data name="lblHashCheckHex.Size" type="System.Drawing.Size, System.Drawing">
<value>29, 13</value> <value>71, 13</value>
</data> </data>
<data name="lblHashCheckHex.TabIndex" type="System.Int32, mscorlib"> <data name="lblHashCheckHex.TabIndex" type="System.Int32, mscorlib">
<value>6</value> <value>6</value>
</data> </data>
<data name="lblHashCheckHex.Text" xml:space="preserve"> <data name="lblHashCheckHex.Text" xml:space="preserve">
<value>Hex:</value> <value>Hexadecimal:</value>
</data> </data>
<data name="&gt;&gt;lblHashCheckHex.Name" xml:space="preserve"> <data name="&gt;&gt;lblHashCheckHex.Name" xml:space="preserve">
<value>lblHashCheckHex</value> <value>lblHashCheckHex</value>