fork download
  1. {$MODE OBJFPC}{$LONGSTRINGS ON}
  2.  
  3. uses
  4. SysUtils;
  5.  
  6. function StringToSmallInt(APointer: Pointer): SmallInt;
  7. var
  8. ResBytes: packed array [0 .. 1] of Byte absolute Result;
  9. RawValue: AnsiString;
  10. begin
  11. RawValue := PAnsiChar(APointer);
  12.  
  13. ResBytes[0] := StrToInt(Copy(RawValue, 5, 3));
  14. ResBytes[1] := StrToInt(Copy(RawValue, 1, 3));
  15. end;
  16.  
  17. function SmallIntToString(APointer: Pointer): AnsiString;
  18. var
  19. ValBytes: packed array [0 .. 1] of Byte;
  20. begin
  21. ValBytes[0] := PByte(APointer + 1)^;
  22. ValBytes[1] := PByte(APointer + 0)^;
  23.  
  24. Result := Format('$%2.2x $%2.2x', [ValBytes[0], ValBytes[1]]);
  25. end;
  26.  
  27. var
  28. StrValue: AnsiString = '$0D $33';
  29. IntValue: SmallInt;
  30. begin
  31. WriteLn('Input: "', StrValue, '"');
  32.  
  33. IntValue := StringToSmallInt(PChar(StrValue));
  34. StrValue := SmallIntToString(@IntValue);
  35.  
  36. WriteLn('Output: "', StrValue, '"');
  37. end.
Success #stdin #stdout 0s 576KB
stdin
Standard input is empty
stdout
Input:  "$0D $33"
Output: "$0D $33"