tbin2hex.pp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. program BinToHex_tests;
  2. {$IFDEF FPC}
  3. {$mode Delphi}
  4. {$ENDIF}
  5. uses
  6. SysUtils, Strutils;
  7. var
  8. BinByteArray: array[0..10] of Byte = (0, 2, 3, 7, 8, 10, 11, 12, 13, 14, 15);
  9. HexText: String;
  10. HexTextA: AnsiString;
  11. HexTextW: WideString;
  12. BinBytes, HexBytes: TBytes;
  13. begin
  14. { mode dependent char }
  15. SetLength(HexText, Length(BinByteArray) * 2);
  16. BinToHex(@BinByteArray[0], PChar(HexText), Length(BinByteArray));
  17. if HexText <> '00020307080A0B0C0D0E0F' then halt(1);
  18. SetLength(HexText, Length(BinByteArray) * 2);
  19. BinToHex(Pointer(@BinByteArray[0]), PChar(HexText), Length(BinByteArray));
  20. if HexText <> '00020307080A0B0C0D0E0F' then halt(2);
  21. SetLength(HexText, Length(BinByteArray) * 2);
  22. BinToHex(@BinByteArray, PChar(HexText), Length(BinByteArray));
  23. if HexText <> '00020307080A0B0C0D0E0F' then halt(3);
  24. { ansichar variants }
  25. SetLength(HexTextA, Length(BinByteArray) * 2);
  26. BinToHex(@BinByteArray[0], PAnsiChar(HexTextA), Length(BinByteArray));
  27. if HexTextA <> '00020307080A0B0C0D0E0F' then halt(4);
  28. SetLength(HexTextA, Length(BinByteArray) * 2);
  29. BinToHex(Pointer(@BinByteArray[0]), PAnsiChar(HexTextA), Length(BinByteArray));
  30. if HexTextA <> '00020307080A0B0C0D0E0F' then halt(5);
  31. SetLength(HexTextA, Length(BinByteArray) * 2);
  32. BinToHex(@BinByteArray, PAnsiChar(HexTextA), Length(BinByteArray));
  33. if HexTextA <> '00020307080A0B0C0D0E0F' then halt(6);
  34. { widechar variants }
  35. SetLength(HexTextW, Length(BinByteArray) * 2);
  36. BinToHex(@BinByteArray[0], PWideChar(HexTextW), Length(BinByteArray));
  37. if HexTextW <> '00020307080A0B0C0D0E0F' then halt(7);
  38. SetLength(HexTextW, Length(BinByteArray) * 2);
  39. BinToHex(Pointer(@BinByteArray[0]), PWideChar(HexTextW), Length(BinByteArray));
  40. if HexTextW <> '00020307080A0B0C0D0E0F' then halt(8);
  41. SetLength(HexTextW, Length(BinByteArray) * 2);
  42. BinToHex(@BinByteArray, PWideChar(HexTextW), Length(BinByteArray));
  43. if HexTextW <> '00020307080A0B0C0D0E0F' then halt(9);
  44. { two char pointer variants }
  45. SetLength(HexTextA, Length(BinByteArray) * 2);
  46. BinToHex(PAnsiChar(@BinByteArray[0]), PAnsiChar(HexTextA), Length(BinByteArray));
  47. if HexTextA <> '00020307080A0B0C0D0E0F' then halt(10);
  48. SetLength(HexTextW, Length(BinByteArray) * 2);
  49. BinToHex(PAnsiChar(@BinByteArray), PWideChar(HexTextW), Length(BinByteArray));
  50. if HexTextW <> '00020307080A0B0C0D0E0F' then halt(11);
  51. { TBytes variants }
  52. BinBytes := TBytes.Create(1, 4, 5, 9, 10, 11, 12, 13, 14, 15);
  53. try
  54. SetLength(HexBytes, Length(BinBytes) * 2);
  55. FillByte(HexBytes[0], Length(HexBytes), 0);
  56. BinToHex(BinBytes, 0, HexBytes, 0, Length(BinBytes));
  57. if TEncoding.ANSI.GetString(HexBytes) <> '010405090A0B0C0D0E0F' then halt(12);
  58. SetLength(HexBytes, Length(BinBytes) * 2);
  59. FillByte(HexBytes[0], Length(HexBytes), Ord('a'));
  60. BinToHex(BinBytes, 2, HexBytes, 0, Length(BinBytes) - 2);
  61. if TEncoding.Default.GetString(HexBytes) <> '05090A0B0C0D0E0Faaaa' then halt(13);
  62. SetLength(HexBytes, Length(BinBytes) * 2);
  63. FillByte(HexBytes[0], Length(HexBytes), Ord('a'));
  64. BinToHex(BinBytes, 4, HexBytes, 2, Length(BinBytes) - 4);
  65. if TEncoding.Default.GetString(HexBytes) <> 'aa0A0B0C0D0E0Faaaaaa' then halt(14);
  66. SetLength(HexBytes, Length(BinBytes) * 2);
  67. FillByte(HexBytes[0], Length(HexBytes), Ord('a'));
  68. BinToHex(BinBytes, 0, HexBytes, 4, Length(BinBytes) - 2);
  69. if TEncoding.Default.GetString(HexBytes) <> 'aaaa010405090A0B0C0D' then halt(15);
  70. finally
  71. SetLength(HexBytes, 0);
  72. SetLength(BinBytes, 0);
  73. end;
  74. writeln('everything passed');
  75. end.