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

@ -39,7 +39,12 @@ public string BinaryText
{
get
{
return Binary.Join();
if (Binary != null && Binary.Length > 0)
{
return Binary.Join();
}
return null;
}
}
@ -50,7 +55,12 @@ public string HexadecimalText
{
get
{
return Hexadecimal.Join().ToUpperInvariant();
if (Hexadecimal != null && Hexadecimal.Length > 0)
{
return Hexadecimal.Join().ToUpperInvariant();
}
return null;
}
}
@ -61,7 +71,12 @@ public string ASCIIText
{
get
{
return ASCII.Join();
if (ASCII != null && ASCII.Length > 0)
{
return ASCII.Join();
}
return null;
}
}
@ -85,10 +100,20 @@ public string ASCIIText
// http://en.wikipedia.org/wiki/RIPEMD
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)
{
try
{
Clear();
if (!string.IsNullOrEmpty(text))
{
Text = text;
@ -117,13 +142,14 @@ public bool DecodeBinary(string binary)
{
try
{
string result = TranslatorHelper.BinaryToText(binary);
return EncodeText(result);
Text = TranslatorHelper.BinaryToText(binary);
return !string.IsNullOrEmpty(Text);
}
catch
{
}
Text = null;
return false;
}
@ -131,13 +157,14 @@ public bool DecodeHex(string hex)
{
try
{
string result = TranslatorHelper.HexadecimalToText(hex);
return EncodeText(result);
Text = TranslatorHelper.HexadecimalToText(hex);
return !string.IsNullOrEmpty(Text);
}
catch
{
}
Text = null;
return false;
}
@ -145,13 +172,14 @@ public bool DecodeASCII(string ascii)
{
try
{
string result = TranslatorHelper.ASCIIToText(ascii);
return EncodeText(result);
Text = TranslatorHelper.ASCIIToText(ascii);
return !string.IsNullOrEmpty(Text);
}
catch
{
}
Text = null;
return false;
}
@ -159,33 +187,17 @@ public bool DecodeBase64(string base64)
{
try
{
string result = TranslatorHelper.Base64ToText(base64);
return EncodeText(result);
Text = TranslatorHelper.Base64ToText(base64);
return !string.IsNullOrEmpty(Text);
}
catch
{
}
Text = null;
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()
{
StringBuilder sb = new StringBuilder();
@ -204,7 +216,7 @@ public override string ToString()
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Text: {Text}");
sb.AppendLine($"Binary: {BinaryText}");
sb.AppendLine($"Hex: {HexadecimalText}");
sb.AppendLine($"Hexadecimal: {HexadecimalText}");
sb.AppendLine($"ASCII: {ASCIIText}");
sb.AppendLine($"Base64: {Base64}");
sb.Append(HashToString());

View file

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

View file

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