uByteTools.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. unit uByteTools;
  2. interface
  3. uses
  4. SysUtils;
  5. type
  6. {$IF RTLVersion<25}
  7. IntPtr=Integer;
  8. {$IFEND IntPtr}
  9. TByteTools = class(TObject)
  10. public
  11. class function varToByteString(const v; len: Cardinal; Split: string = ' '):
  12. String;
  13. class function varToHexString(const v; len: Cardinal; Split: string = ' '):
  14. String;
  15. /// <summary>
  16. /// 16进制转 二进制
  17. /// </summary>
  18. class function HexToBin(pvHexStr:String; buf:Pointer):Integer;
  19. /// <summary>
  20. /// 16进制字符到二进制
  21. /// </summary>
  22. class function HexValue(c: Char): Integer;
  23. /// <summary>
  24. /// 是否16进制字符
  25. /// </summary>
  26. class function IsHexChar(c: Char): Boolean;
  27. /// <summary>
  28. /// 高低位进行交换
  29. /// </summary>
  30. class function swap32(v:Integer):Integer;
  31. /// <summary>
  32. /// 高低位进行交换
  33. /// </summary>
  34. class function swap64(v:int64):Int64;
  35. /// <summary>
  36. /// 高低位进行交换
  37. /// </summary>
  38. class function swap16(const v):Word;
  39. end;
  40. implementation
  41. class function TByteTools.HexToBin(pvHexStr: String;
  42. buf: Pointer): Integer;
  43. var
  44. l: Integer;
  45. p, ps: PChar;
  46. pd: PByte;
  47. begin
  48. l := Length(pvHexStr);
  49. p := PChar(pvHexStr);
  50. ps := p;
  51. pd := PByte(buf);
  52. Result := 0;
  53. while p - ps < l do
  54. begin
  55. if IsHexChar(p[0]) and IsHexChar(p[1]) then
  56. begin
  57. pd^ := (HexValue(p[0]) shl 4) + HexValue(p[1]);
  58. inc(Result);
  59. Inc(pd);
  60. Inc(p, 2);
  61. end
  62. else
  63. begin
  64. Exit;
  65. end;
  66. end;
  67. end;
  68. class function TByteTools.HexValue(c: Char): Integer;
  69. begin
  70. if (c >= '0') and (c <= '9') then
  71. Result := Ord(c) - Ord('0')
  72. else if (c >= 'a') and (c <= 'f') then
  73. Result := 10 + Ord(c) - Ord('a')
  74. else
  75. Result := 10 + Ord(c) - Ord('A');
  76. end;
  77. class function TByteTools.IsHexChar(c: Char): Boolean;
  78. begin
  79. Result := ((c >= '0') and (c <= '9')) or ((c >= 'a') and (c <= 'f')) or ((c >= 'A') and (c <= 'F'));
  80. end;
  81. class function TByteTools.swap16(const v): Word;
  82. begin
  83. // FF, EE : EE->1, FF->2
  84. PByte(@result)^ := PByte(IntPtr(@v) + 1)^;
  85. PByte(IntPtr(@result) + 1)^ := PByte(@v)^;
  86. end;
  87. class function TByteTools.swap32(v: Integer): Integer;
  88. var
  89. lvPByte : PByte;
  90. begin
  91. result := v;
  92. lvPByte := PByte(@result);
  93. PByte(lvPByte)^ := byte(v shr 24);
  94. PByte(IntPtr(lvPByte) + 1)^ := byte(v shr 16);
  95. PByte(IntPtr(lvPByte) + 2)^ := byte(v shr 8);
  96. PByte(IntPtr(lvPByte) + 3)^ := byte(v);
  97. end;
  98. class function TByteTools.swap64(v: int64): Int64;
  99. var
  100. lvPByte : PByte;
  101. begin
  102. result := v;
  103. lvPByte := PByte(@result);
  104. PByte(lvPByte)^ := byte(v shr 56); //8 * 7
  105. PByte(IntPtr(lvPByte) + 1)^ := byte(v shr 48); //6
  106. PByte(IntPtr(lvPByte) + 2)^ := byte(v shr 40); //5
  107. PByte(IntPtr(lvPByte) + 3)^ := byte(v shr 32); //4
  108. PByte(IntPtr(lvPByte) + 4)^ := byte(v shr 24); //3
  109. PByte(IntPtr(lvPByte) + 5)^ := byte(v shr 16); //2
  110. PByte(IntPtr(lvPByte) + 6)^ := byte(v shr 8); //2
  111. PByte(IntPtr(lvPByte) + 7)^ := byte(v); //1
  112. end;
  113. class function TByteTools.varToByteString(const v; len: Cardinal; Split: string
  114. = ' '): String;
  115. var
  116. lvSource:PByte;
  117. i: Integer;
  118. begin
  119. lvSource := PByte(@v);
  120. for i := 1 to len do
  121. begin
  122. Result := Result + IntToStr(lvSource^) + Split;
  123. Inc(lvSource);
  124. end;
  125. end;
  126. class function TByteTools.varToHexString(const v; len: Cardinal; Split: string
  127. = ' '): String;
  128. var
  129. lvSource:PByte;
  130. i: Integer;
  131. begin
  132. lvSource := PByte(@v);
  133. for i := 1 to len do
  134. begin
  135. Result := Result + IntToHex(lvSource^, 2) + Split;
  136. Inc(lvSource);
  137. end;
  138. end;
  139. end.