| 1 | unit UMainForm;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
|---|
| 7 | Spin, VarBlockSerializer, ComCtrls, ExtCtrls;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TMainForm }
|
|---|
| 12 |
|
|---|
| 13 | TMainForm = class(TForm)
|
|---|
| 14 | published
|
|---|
| 15 | ButtonEncodeIndexed: TButton;
|
|---|
| 16 | ButtonDecodeIndexed: TButton;
|
|---|
| 17 | ButtonDecodeRaw: TButton;
|
|---|
| 18 | ButtonDecodeString: TButton;
|
|---|
| 19 | ButtonEncodeRaw: TButton;
|
|---|
| 20 | ButtonEncodeString: TButton;
|
|---|
| 21 | ButtonFloatDecode1: TButton;
|
|---|
| 22 | ButtonFloatEncode1: TButton;
|
|---|
| 23 | ButtonSIntDecode: TButton;
|
|---|
| 24 | ButtonSIntEncode: TButton;
|
|---|
| 25 | ButtonUIntDecode: TButton;
|
|---|
| 26 | ButtonUIntEncode: TButton;
|
|---|
| 27 | CheckBoxMask1: TCheckBox;
|
|---|
| 28 | CheckBoxMask2: TCheckBox;
|
|---|
| 29 | CheckBoxMask3: TCheckBox;
|
|---|
| 30 | EditIndexedItem1: TEdit;
|
|---|
| 31 | EditIndexedItem2: TEdit;
|
|---|
| 32 | EditIndexedItem3: TEdit;
|
|---|
| 33 | EditIndexed: TEdit;
|
|---|
| 34 | EditFloat: TEdit;
|
|---|
| 35 | EditRaw: TEdit;
|
|---|
| 36 | EditRawData: TEdit;
|
|---|
| 37 | EditSInt: TEdit;
|
|---|
| 38 | EditString: TEdit;
|
|---|
| 39 | EditStringData: TEdit;
|
|---|
| 40 | EditUInt: TEdit;
|
|---|
| 41 | FloatSpinEdit1: TFloatSpinEdit;
|
|---|
| 42 | GroupBox1: TGroupBox;
|
|---|
| 43 | GroupBox2: TGroupBox;
|
|---|
| 44 | GroupBox3: TGroupBox;
|
|---|
| 45 | GroupBox4: TGroupBox;
|
|---|
| 46 | GroupBox5: TGroupBox;
|
|---|
| 47 | Label1: TLabel;
|
|---|
| 48 | Label2: TLabel;
|
|---|
| 49 | Label3: TLabel;
|
|---|
| 50 | Label4: TLabel;
|
|---|
| 51 | Label5: TLabel;
|
|---|
| 52 | Label6: TLabel;
|
|---|
| 53 | PageControl1: TPageControl;
|
|---|
| 54 | SpinEditFloat: TSpinEdit;
|
|---|
| 55 | SpinEditSInt: TSpinEdit;
|
|---|
| 56 | SpinEditUInt: TSpinEdit;
|
|---|
| 57 | TabSheet1: TTabSheet;
|
|---|
| 58 | TabSheet2: TTabSheet;
|
|---|
| 59 | procedure ButtonDecodeIndexedClick(Sender: TObject);
|
|---|
| 60 | procedure ButtonEncodeIndexedClick(Sender: TObject);
|
|---|
| 61 | procedure ButtonDecodeRawClick(Sender: TObject);
|
|---|
| 62 | procedure ButtonDecodeStringClick(Sender: TObject);
|
|---|
| 63 | procedure ButtonEncodeRawClick(Sender: TObject);
|
|---|
| 64 | procedure ButtonEncodeStringClick(Sender: TObject);
|
|---|
| 65 | procedure ButtonFloatDecode1Click(Sender: TObject);
|
|---|
| 66 | procedure ButtonFloatEncode1Click(Sender: TObject);
|
|---|
| 67 | procedure ButtonSIntDecodeClick(Sender: TObject);
|
|---|
| 68 | procedure ButtonSIntEncodeClick(Sender: TObject);
|
|---|
| 69 | procedure ButtonUIntDecodeClick(Sender: TObject);
|
|---|
| 70 | procedure ButtonUIntEncodeClick(Sender: TObject);
|
|---|
| 71 | procedure FormShow(Sender: TObject);
|
|---|
| 72 | public
|
|---|
| 73 | function StreamToString(Stream: TStream): string;
|
|---|
| 74 | procedure StringToStream(Text: string; Stream: TStream);
|
|---|
| 75 | end;
|
|---|
| 76 |
|
|---|
| 77 | var
|
|---|
| 78 | MainForm: TMainForm;
|
|---|
| 79 |
|
|---|
| 80 | implementation
|
|---|
| 81 |
|
|---|
| 82 | {$R *.lfm}
|
|---|
| 83 |
|
|---|
| 84 | { TMainForm }
|
|---|
| 85 |
|
|---|
| 86 | procedure TMainForm.ButtonDecodeRawClick(Sender: TObject);
|
|---|
| 87 | var
|
|---|
| 88 | Block: TVarBlockSerializer;
|
|---|
| 89 | Stream: TMemoryStream;
|
|---|
| 90 | begin
|
|---|
| 91 | try
|
|---|
| 92 | Block := TVarBlockSerializer.Create;
|
|---|
| 93 | Stream := TMemoryStream.Create;
|
|---|
| 94 | StringToStream(EditRawData.Text, Block.Stream);
|
|---|
| 95 | Block.Stream.Position := 0;
|
|---|
| 96 | Block.ReadVarStream(Stream);
|
|---|
| 97 | EditRaw.Text := StreamToString(Stream);
|
|---|
| 98 | finally
|
|---|
| 99 | Stream.Free;
|
|---|
| 100 | Block.Free;
|
|---|
| 101 | end;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | procedure TMainForm.ButtonEncodeIndexedClick(Sender: TObject);
|
|---|
| 105 | var
|
|---|
| 106 | IndexedBlock: TVarBlockIndexed;
|
|---|
| 107 | Stream: TMemoryStream;
|
|---|
| 108 | begin
|
|---|
| 109 | try
|
|---|
| 110 | IndexedBlock := TVarBlockIndexed.Create;
|
|---|
| 111 | Stream := TMemoryStream.Create;
|
|---|
| 112 | if CheckBoxMask1.Checked then begin
|
|---|
| 113 | StringToStream(EditIndexedItem1.Text, Stream);
|
|---|
| 114 | IndexedBlock.WriteVarStream(0, Stream);
|
|---|
| 115 | end;
|
|---|
| 116 | if CheckBoxMask2.Checked then begin
|
|---|
| 117 | StringToStream(EditIndexedItem2.Text, Stream);
|
|---|
| 118 | IndexedBlock.WriteVarStream(1, Stream);
|
|---|
| 119 | end;
|
|---|
| 120 | if CheckBoxMask3.Checked then begin
|
|---|
| 121 | StringToStream(EditIndexedItem3.Text, Stream);
|
|---|
| 122 | IndexedBlock.WriteVarStream(2, Stream);
|
|---|
| 123 | end;
|
|---|
| 124 | Stream.Clear;
|
|---|
| 125 | IndexedBlock.WriteToStream(Stream);
|
|---|
| 126 | EditIndexed.Text := StreamToString(Stream);
|
|---|
| 127 | finally
|
|---|
| 128 | IndexedBlock.Free;
|
|---|
| 129 | Stream.Free;
|
|---|
| 130 | end;
|
|---|
| 131 | end;
|
|---|
| 132 |
|
|---|
| 133 | procedure TMainForm.ButtonDecodeIndexedClick(Sender: TObject);
|
|---|
| 134 | var
|
|---|
| 135 | IndexedBlock: TVarBlockIndexed;
|
|---|
| 136 | Stream: TMemoryStream;
|
|---|
| 137 | begin
|
|---|
| 138 | try
|
|---|
| 139 | IndexedBlock := TVarBlockIndexed.Create;
|
|---|
| 140 | Stream := TMemoryStream.Create;
|
|---|
| 141 | StringToStream(EditIndexed.Text, Stream);
|
|---|
| 142 | IndexedBlock.ReadFromStream(Stream);
|
|---|
| 143 |
|
|---|
| 144 | CheckBoxMask1.Checked := IndexedBlock.TestIndex(0);
|
|---|
| 145 | CheckBoxMask2.Checked := IndexedBlock.TestIndex(1);
|
|---|
| 146 | CheckBoxMask3.Checked := IndexedBlock.TestIndex(2);
|
|---|
| 147 |
|
|---|
| 148 | Stream.Clear;
|
|---|
| 149 | if IndexedBlock.TestIndex(0) then begin
|
|---|
| 150 | IndexedBlock.ReadVarStream(0, Stream);
|
|---|
| 151 | EditIndexedItem1.Text := StreamToString(Stream);
|
|---|
| 152 | end else EditIndexedItem1.Text := '';
|
|---|
| 153 | if IndexedBlock.TestIndex(1) then begin
|
|---|
| 154 | IndexedBlock.ReadVarStream(1, Stream);
|
|---|
| 155 | EditIndexedItem2.Text := StreamToString(Stream);
|
|---|
| 156 | end else EditIndexedItem2.Text := '';
|
|---|
| 157 | if IndexedBlock.TestIndex(2) then begin
|
|---|
| 158 | IndexedBlock.ReadVarStream(2, Stream);
|
|---|
| 159 | EditIndexedItem3.Text := StreamToString(Stream);
|
|---|
| 160 | end else EditIndexedItem3.Text := '';
|
|---|
| 161 | finally
|
|---|
| 162 | IndexedBlock.Free;
|
|---|
| 163 | Stream.Free;
|
|---|
| 164 | end;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | procedure TMainForm.ButtonDecodeStringClick(Sender: TObject);
|
|---|
| 168 | var
|
|---|
| 169 | Block: TVarBlockSerializer;
|
|---|
| 170 | begin
|
|---|
| 171 | try
|
|---|
| 172 | Block := TVarBlockSerializer.Create;
|
|---|
| 173 | StringToStream(EditStringData.Text, Block.Stream);
|
|---|
| 174 | Block.Stream.Position := 0;
|
|---|
| 175 | EditString.Text := Block.ReadVarString;
|
|---|
| 176 | finally
|
|---|
| 177 | Block.Free;
|
|---|
| 178 | end;
|
|---|
| 179 | end;
|
|---|
| 180 |
|
|---|
| 181 | procedure TMainForm.ButtonEncodeRawClick(Sender: TObject);
|
|---|
| 182 | var
|
|---|
| 183 | Block: TVarBlockSerializer;
|
|---|
| 184 | Stream: TMemoryStream;
|
|---|
| 185 | begin
|
|---|
| 186 | try
|
|---|
| 187 | Block := TVarBlockSerializer.Create;
|
|---|
| 188 | Stream := TMemoryStream.Create;
|
|---|
| 189 | StringToStream(EditRaw.Text, Stream);
|
|---|
| 190 | Block.WriteVarStream(Stream);
|
|---|
| 191 | EditRawData.Text := StreamToString(Block.Stream);
|
|---|
| 192 | finally
|
|---|
| 193 | Stream.Free;
|
|---|
| 194 | Block.Free;
|
|---|
| 195 | end;
|
|---|
| 196 | end;
|
|---|
| 197 |
|
|---|
| 198 | procedure TMainForm.ButtonEncodeStringClick(Sender: TObject);
|
|---|
| 199 | var
|
|---|
| 200 | Block: TVarBlockSerializer;
|
|---|
| 201 | begin
|
|---|
| 202 | try
|
|---|
| 203 | Block := TVarBlockSerializer.Create;
|
|---|
| 204 | Block.WriteVarString(EditString.Text);
|
|---|
| 205 | EditStringData.Text := StreamToString(Block.Stream);
|
|---|
| 206 | finally
|
|---|
| 207 | Block.Free;
|
|---|
| 208 | end;
|
|---|
| 209 | end;
|
|---|
| 210 |
|
|---|
| 211 | procedure TMainForm.ButtonFloatDecode1Click(Sender: TObject);
|
|---|
| 212 | var
|
|---|
| 213 | Block: TVarBlockSerializer;
|
|---|
| 214 | begin
|
|---|
| 215 | try
|
|---|
| 216 | Block := TVarBlockSerializer.Create;
|
|---|
| 217 | StringToStream(EditFloat.Text, Block.Stream);
|
|---|
| 218 | Block.Stream.Position := 0;
|
|---|
| 219 | FloatSpinEdit1.Value := Block.ReadVarFloat(SpinEditFloat.Value);
|
|---|
| 220 | finally
|
|---|
| 221 | Block.Free;
|
|---|
| 222 | end;
|
|---|
| 223 | end;
|
|---|
| 224 |
|
|---|
| 225 | procedure TMainForm.ButtonFloatEncode1Click(Sender: TObject);
|
|---|
| 226 | var
|
|---|
| 227 | Block: TVarBlockSerializer;
|
|---|
| 228 | begin
|
|---|
| 229 | try
|
|---|
| 230 | Block := TVarBlockSerializer.Create;
|
|---|
| 231 | Block.WriteVarFloat(FloatSpinEdit1.Value, SpinEditFloat.Value);
|
|---|
| 232 | EditFloat.Text := StreamToString(Block.Stream);
|
|---|
| 233 | finally
|
|---|
| 234 | Block.Free;
|
|---|
| 235 | end;
|
|---|
| 236 | end;
|
|---|
| 237 |
|
|---|
| 238 | procedure TMainForm.ButtonSIntDecodeClick(Sender: TObject);
|
|---|
| 239 | var
|
|---|
| 240 | Block: TVarBlockSerializer;
|
|---|
| 241 | begin
|
|---|
| 242 | try
|
|---|
| 243 | Block := TVarBlockSerializer.Create;
|
|---|
| 244 | StringToStream(EditSInt.Text, Block.Stream);
|
|---|
| 245 | Block.Stream.Position := 0;
|
|---|
| 246 | SpinEditSInt.Value := Block.ReadVarSInt;
|
|---|
| 247 | finally
|
|---|
| 248 | Block.Free;
|
|---|
| 249 | end;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | procedure TMainForm.ButtonSIntEncodeClick(Sender: TObject);
|
|---|
| 253 | var
|
|---|
| 254 | Block: TVarBlockSerializer;
|
|---|
| 255 | begin
|
|---|
| 256 | try
|
|---|
| 257 | Block := TVarBlockSerializer.Create;
|
|---|
| 258 | Block.WriteVarSInt(SpinEditSInt.Value);
|
|---|
| 259 | EditSInt.Text := StreamToString(Block.Stream);
|
|---|
| 260 | finally
|
|---|
| 261 | Block.Free;
|
|---|
| 262 | end;
|
|---|
| 263 | end;
|
|---|
| 264 |
|
|---|
| 265 | procedure TMainForm.ButtonUIntDecodeClick(Sender: TObject);
|
|---|
| 266 | var
|
|---|
| 267 | Block: TVarBlockSerializer;
|
|---|
| 268 | begin
|
|---|
| 269 | try
|
|---|
| 270 | Block := TVarBlockSerializer.Create;
|
|---|
| 271 | StringToStream(EditUInt.Text, Block.Stream);
|
|---|
| 272 | Block.Stream.Position := 0;
|
|---|
| 273 | SpinEditUInt.Value := Block.ReadVarUInt;
|
|---|
| 274 | finally
|
|---|
| 275 | Block.Free;
|
|---|
| 276 | end;
|
|---|
| 277 | end;
|
|---|
| 278 |
|
|---|
| 279 | procedure TMainForm.ButtonUIntEncodeClick(Sender: TObject);
|
|---|
| 280 | var
|
|---|
| 281 | Block: TVarBlockSerializer;
|
|---|
| 282 | begin
|
|---|
| 283 | try
|
|---|
| 284 | Block := TVarBlockSerializer.Create;
|
|---|
| 285 | Block.WriteVarUInt(SpinEditUInt.Value);
|
|---|
| 286 | EditUInt.Text := StreamToString(Block.Stream);
|
|---|
| 287 | finally
|
|---|
| 288 | Block.Free;
|
|---|
| 289 | end;
|
|---|
| 290 | end;
|
|---|
| 291 |
|
|---|
| 292 | procedure TMainForm.FormShow(Sender: TObject);
|
|---|
| 293 | begin
|
|---|
| 294 | PageControl1.TabIndex := 0;
|
|---|
| 295 | end;
|
|---|
| 296 |
|
|---|
| 297 | function TMainForm.StreamToString(Stream: TStream): string;
|
|---|
| 298 | begin
|
|---|
| 299 | Result := '';
|
|---|
| 300 | Stream.Position := 0;
|
|---|
| 301 | while Stream.Position < Stream.Size do
|
|---|
| 302 | Result := Result + IntToHex(Stream.ReadByte, 2) + ' ';
|
|---|
| 303 | end;
|
|---|
| 304 |
|
|---|
| 305 | function TryHexToInt(Data: string; var Value: Integer): Boolean;
|
|---|
| 306 | var
|
|---|
| 307 | I: Integer;
|
|---|
| 308 | begin
|
|---|
| 309 | Data := UpperCase(Data);
|
|---|
| 310 | Result := True;
|
|---|
| 311 | Value := 0;
|
|---|
| 312 | for I := 0 to Length(Data) - 1 do begin
|
|---|
| 313 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
|
|---|
| 314 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 315 | else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
|
|---|
| 316 | Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 317 | else Result := False;
|
|---|
| 318 | end;
|
|---|
| 319 | end;
|
|---|
| 320 |
|
|---|
| 321 | procedure TMainForm.StringToStream(Text: string; Stream: TStream);
|
|---|
| 322 | var
|
|---|
| 323 | Part: string;
|
|---|
| 324 | Number: Integer;
|
|---|
| 325 | begin
|
|---|
| 326 | Stream.Size := 0;
|
|---|
| 327 | Text := Trim(Text);
|
|---|
| 328 | while Pos(' ', Text) > 0 do begin
|
|---|
| 329 | Part := Copy(Text, 1, Pos(' ', Text) - 1);
|
|---|
| 330 | if TryHexToInt(Part, Number) and (Number < 256) then
|
|---|
| 331 | Stream.WriteByte(Number);
|
|---|
| 332 | Delete(Text, 1, Pos(' ', Text));
|
|---|
| 333 | end;
|
|---|
| 334 | if TryHexToInt(Text, Number) and (Number < 256) and (Text <> '') then
|
|---|
| 335 | Stream.WriteByte(Number);
|
|---|
| 336 | end;
|
|---|
| 337 |
|
|---|
| 338 | end.
|
|---|
| 339 |
|
|---|