Skip to content

Commit 3237541

Browse files
committed
GH-38 GH-39 Decode Hex String to text with a bit of FSharp
1 parent 45371cc commit 3237541

File tree

6 files changed

+174
-29
lines changed

6 files changed

+174
-29
lines changed

R7.Webmate.Fs/Library.fs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
namespace R7.Webmate
1+
namespace R7.Webmate.Text.Models
22

3-
module Say =
4-
let hello name =
5-
printfn "Hello %s" name
3+
open System
4+
5+
type public DecodeHexStringModel() =
6+
7+
let mutable hexString = ""
8+
member this.HexString
9+
with get () = hexString
10+
and set (value: string) =
11+
let hexString2 = if value.StartsWith("0x") then value.Substring(2) else value
12+
let hexString3 = if hexString2.Length % 2 = 1 then hexString2.Substring(0, hexString2.Length - 1) else hexString2
13+
hexString <- hexString3
14+
15+
member this.ConvertToByte(hex: string) =
16+
try Convert.ToByte(hex, 16) with | _ -> Convert.ToByte(0)
17+
18+
member this.Process() =
19+
let bytes: byte[] = Array.zeroCreate(this.HexString.Length / 2)
20+
for i in 0 .. bytes.Length - 1 do
21+
let hex = this.HexString.Substring(i * 2, 2)
22+
Array.set bytes i (this.ConvertToByte hex)
23+
bytes

R7.Webmate.Xwt/MainWindow.cs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public MainWindow ()
3636
notebook.Add (new CaseChangerWidget (), T.GetString ("Case Changer"));
3737
notebook.Add (new UuidGeneratorWidget (), T.GetString ("UUID Generator"));
3838
notebook.Add (new ExternalToolsWidget (), T.GetString ("External Tools"));
39+
notebook.Add (new DecodeHexStringWidget(), T.GetString ("Decode Hex String"));
40+
3941
notebook.CurrentTabChanged += Notebook_CurrentTabChanged;
4042

4143
UpdateTitle (notebook.CurrentTab.Label);

R7.Webmate.Xwt/R7.Webmate.Xwt.csproj

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<ProjectGuid>{2505B342-AD1C-46EA-AA9F-BB232CC36CC8}</ProjectGuid>
@@ -29,6 +29,7 @@
2929
<Compile Include="XwtHelper.cs" />
3030
<Compile Include="Text\TableCleanerWidget.cs" />
3131
<Compile Include="Text\TextCleanerWidget.cs" />
32+
<Compile Include="Text\DecodeHexStringWidget.cs" />
3233
<Compile Include="Text\TextViewDialog.cs" />
3334
<Compile Include="Text\TextViewLabel.cs" />
3435
<Compile Include="TextCatalogKeeper.cs" />
@@ -122,6 +123,9 @@
122123
</None>
123124
</ItemGroup>
124125
<ItemGroup>
126+
<Reference Include="FSharp.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
127+
<HintPath>..\packages\FSharp.Core.7.0.200\lib\netstandard2.0\FSharp.Core.dll</HintPath>
128+
</Reference>
125129
<Reference Include="NGettext, Version=0.6.1.0, Culture=neutral, PublicKeyToken=08d3d1c89dfd2985">
126130
<HintPath>..\packages\NGettext.0.6.7\lib\net46\NGettext.dll</HintPath>
127131
<Private>True</Private>
@@ -150,23 +154,16 @@
150154
<Private>True</Private>
151155
</Reference>
152156
</ItemGroup>
157+
<ItemGroup />
153158
<ItemGroup>
154-
<Folder Include="Icons\" />
155-
<Folder Include="resources\locale\" />
156-
<Folder Include="config\" />
157-
<Folder Include="Text\" />
158-
<Folder Include="Properties\" />
159-
<Folder Include="Misc\" />
160-
</ItemGroup>
161-
<ItemGroup>
159+
<ProjectReference Include="..\R7.Webmate.Fs\R7.Webmate.Fs.fsproj">
160+
<Project>{c9e616ea-f142-4c31-8e97-fee8bb39b7b5}</Project>
161+
<Name>R7.Webmate.Fs</Name>
162+
</ProjectReference>
162163
<ProjectReference Include="..\R7.Webmate\R7.Webmate.csproj">
163164
<Project>{C97EAFDF-3190-4A13-B1DC-A033C45228B8}</Project>
164165
<Name>R7.Webmate</Name>
165166
</ProjectReference>
166-
<ProjectReference Include="..\R7.Webmate.Fs\R7.Webmate.Fs.fsproj">
167-
<Project>{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}</Project>
168-
<Name>R7.Webmate.Fs</Name>
169-
</ProjectReference>
170167
</ItemGroup>
171168
<ItemGroup>
172169
<Content Include="resources\app-icons\r7-webmate-64px.png">
@@ -176,7 +173,7 @@
176173
</ItemGroup>
177174
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
178175
<Target Name="CopyBackendAssemblies">
179-
<XmlPeek XmlInputPath="packages.config" Query="/packages/package[@id='Xwt']/@version">
176+
<XmlPeek XmlInputPath="packages.config" Query="/packages/package[@id='Xwt']/@version">
180177
<Output TaskParameter="Result" PropertyName="XwtPackageVersion" />
181178
</XmlPeek>
182179
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="powershell ./CopyBackendAssemblies.ps1 -OutputPath $(OutputPath) -XwtPackageVersion $(XwtPackageVersion)" />
@@ -192,4 +189,4 @@
192189
</Properties>
193190
</MonoDevelop>
194191
</ProjectExtensions>
195-
</Project>
192+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Text;
3+
using NGettext;
4+
using R7.Webmate.Text.Models;
5+
using R7.Webmate.Xwt.Icons;
6+
using Xwt;
7+
8+
namespace R7.Webmate.Xwt.Text
9+
{
10+
public class DecodeHexStringWidget: Widget
11+
{
12+
protected ICatalog T = TextCatalogKeeper.GetDefault ();
13+
14+
protected DecodeHexStringModel Model = new DecodeHexStringModel ();
15+
16+
protected Button btnPaste;
17+
18+
protected Button btnDecode;
19+
20+
protected VBox vboxResults = new VBox ();
21+
22+
protected HBox hboxGenerate = new HBox ();
23+
24+
protected HBox hboxPaste = new HBox();
25+
26+
protected ScrollView scrResults;
27+
28+
protected TextViewLabel lblSrc = new TextViewLabel ();
29+
30+
protected CheckBox chkAutoProcess = new CheckBox ();
31+
32+
public DecodeHexStringWidget ()
33+
{
34+
var vbox = new VBox ();
35+
36+
lblSrc.AllowQuickCopy = false;
37+
lblSrc.AllowEdit = true;
38+
39+
btnPaste = new Button (IconHelper.GetIcon ("paste").WithSize (IconSize.Medium), T.GetString ("Paste"));
40+
btnPaste.Clicked += BtnPaste_Clicked;
41+
42+
chkAutoProcess.Label = T.GetString ("Process on paste?");
43+
chkAutoProcess.Active = true;
44+
45+
hboxPaste.PackStart(btnPaste, true, true);
46+
47+
btnDecode = new Button (IconHelper.GetIcon ("play-circle").WithSize (IconSize.Medium), T.GetString("Decode"));
48+
btnDecode.Clicked += btnDecode_Clicked;
49+
50+
hboxGenerate.PackStart (btnDecode, true, true);
51+
52+
scrResults = new ScrollView (vboxResults);
53+
54+
vbox.PackStart(hboxPaste, true, true);
55+
vbox.PackStart(lblSrc, false, true);
56+
vbox.PackStart (hboxGenerate, false, true);
57+
vbox.PackStart (chkAutoProcess, false, true);
58+
vbox.PackStart (scrResults, true, true);
59+
60+
vbox.Margin = Const.VBOX_MARGIN;
61+
Content = vbox;
62+
Content.Show ();
63+
}
64+
65+
void BtnPaste_Clicked (object sender, EventArgs e)
66+
{
67+
Model.HexString = Clipboard.GetText () ?? string.Empty;
68+
lblSrc.Text = Model.HexString;
69+
70+
if (chkAutoProcess.Active) {
71+
btnDecode_Clicked(sender, e);
72+
}
73+
}
74+
75+
void btnDecode_Clicked (object sender, EventArgs e)
76+
{
77+
Model.HexString = lblSrc.Text;
78+
var bytes = Model.Process();
79+
80+
var text1 = Encoding.GetEncoding("Windows-1251").GetString(bytes);
81+
var result1 = new TextResult {
82+
Text = text1,
83+
Label = "Windows-1251"
84+
};
85+
86+
var text2 = Encoding.UTF8.GetString(bytes);
87+
var result2 = new TextResult {
88+
Text = text2,
89+
Label = "UTF-8"
90+
};
91+
92+
vboxResults.Clear();
93+
AddResult(1, result1);
94+
AddResult(2, result2);
95+
}
96+
97+
protected void AddResult (int index, TextResult result)
98+
{
99+
var lblResult = new TextViewLabel();
100+
lblResult.Text = result.Text;
101+
102+
var vboxResult = new VBox();
103+
vboxResult.MarginLeft = Const.VBOX_MARGIN;
104+
vboxResult.MarginRight = Const.VBOX_MARGIN;
105+
vboxResult.MarginBottom = 3;
106+
vboxResult.PackStart(lblResult, false, true);
107+
108+
var frmResult = new Frame();
109+
110+
frmResult.Label = string.Format(T.GetString("Result #{0} - {1}"), index, T.GetString(result.Label));
111+
112+
frmResult.Content = vboxResult;
113+
vboxResults.PackStart(frmResult);
114+
}
115+
}
116+
}

R7.Webmate.Xwt/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="FSharp.Core" version="7.0.200" targetFramework="net472" />
34
<package id="NGettext" version="0.6.7" targetFramework="net472" />
45
<package id="NLog" version="4.7.10" targetFramework="net472" />
56
<package id="Xwt" version="0.2.234" targetFramework="net472" />

R7.Webmate.sln

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
21
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.33502.453
4+
MinimumVisualStudioVersion = 10.0.40219.1
45
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "R7.Webmate.Xwt", "R7.Webmate.Xwt\R7.Webmate.Xwt.csproj", "{2505B342-AD1C-46EA-AA9F-BB232CC36CC8}"
56
EndProject
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "R7.Webmate", "R7.Webmate\R7.Webmate.csproj", "{C97EAFDF-3190-4A13-B1DC-A033C45228B8}"
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "R7.Webmate", "R7.Webmate\R7.Webmate.csproj", "{C97EAFDF-3190-4A13-B1DC-A033C45228B8}"
78
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "R7.Webmate.Fs", "R7.Webmate.Fs\R7.Webmate.Fs.fsproj", "{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}"
9-
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "R7.Webmate.Tests", "R7.Webmate.Tests\R7.Webmate.Tests.csproj", "{DA075051-D5E0-445D-8826-F683F89494E0}"
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "R7.Webmate.Tests", "R7.Webmate.Tests\R7.Webmate.Tests.csproj", "{DA075051-D5E0-445D-8826-F683F89494E0}"
1110
EndProject
1211
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "R7.Webmate.Xwt.DependencyHelper", "R7.Webmate.Xwt.DependencyHelper\R7.Webmate.Xwt.DependencyHelper.csproj", "{B801B613-7219-4C07-9340-A33451516F68}"
1312
EndProject
13+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "R7.Webmate.Fs", "R7.Webmate.Fs\R7.Webmate.Fs.fsproj", "{C9E616EA-F142-4C31-8E97-FEE8BB39B7B5}"
14+
EndProject
1415
Global
1516
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1617
Debug|Any CPU = Debug|Any CPU
@@ -29,10 +30,20 @@ Global
2930
{DA075051-D5E0-445D-8826-F683F89494E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
3031
{DA075051-D5E0-445D-8826-F683F89494E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
3132
{DA075051-D5E0-445D-8826-F683F89494E0}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{73C8C014-A9CB-44C0-8C38-32B7FDBF9FCC}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{B801B613-7219-4C07-9340-A33451516F68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{B801B613-7219-4C07-9340-A33451516F68}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{B801B613-7219-4C07-9340-A33451516F68}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{B801B613-7219-4C07-9340-A33451516F68}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{C9E616EA-F142-4C31-8E97-FEE8BB39B7B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{C9E616EA-F142-4C31-8E97-FEE8BB39B7B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{C9E616EA-F142-4C31-8E97-FEE8BB39B7B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{C9E616EA-F142-4C31-8E97-FEE8BB39B7B5}.Release|Any CPU.Build.0 = Release|Any CPU
41+
EndGlobalSection
42+
GlobalSection(SolutionProperties) = preSolution
43+
HideSolutionNode = FALSE
44+
EndGlobalSection
45+
GlobalSection(ExtensibilityGlobals) = postSolution
46+
SolutionGuid = {4A2F1E7C-FC47-4090-8766-F467092DD3B3}
3647
EndGlobalSection
3748
GlobalSection(MonoDevelopProperties) = preSolution
3849
Policies = $0

0 commit comments

Comments
 (0)