Delphi的几种类型转换

  1. Delphi的几种类型转换   
  2.    
  3. unit Support;  
  4.   
  5. interface  
  6.   
  7. type dword=longword;  
  8.   
  9. function WordToStr(Value: word): string;  
  10. function DwordToStr(Value: dword): string;  
  11. function StrToWord(Value: string): word;  
  12. function StrToDword(Value: string): dword;  
  13.   
  14. procedure SetBit(var Str: string; BitNr: dword; Value: boolean);  
  15. function GetBit(Str: string; BitNr: dword): boolean;  
  16.   
  17. function Pack(I: string):string;  
  18. function UnPack(I: string): string;  
  19.   
  20. procedure FindBest(Main, Sub: string;var FoundLen, FoundPos: integer);  
  21.   
  22. implementation  
  23.   
  24. // DwordToStr() : Converts a DWORD to a 4 byte string  
  25. function DwordToStr(Value: dword): string;  
  26. var  
  27. ResultPtr: PChar;  
  28. begin  
  29. SetLength(Result, 4);  
  30. ResultPtr:=@Result[1];  
  31. asm  
  32. MOV EAX, [ResultPtr]  
  33. MOV EBX, Value  
  34. MOV [EAX], EBX end;  
  35. end;  
  36.   
  37. // StrToDWord() : Converts a 4 byte string to a DWORD  
  38. function StrToDword(Value: string): dword;  
  39. var  
  40. ValuePtr: PChar;  
  41. begin  
  42. ValuePtr:=@Value[1];  
  43. asm  
  44. MOV EAX, [ValuePtr]  
  45. MOV EAX, [EAX]  
  46. MOV Result, EAX end;  
  47. end;  
  48.   
  49. // WordToStr() : Converts a WORD to a 2 byte string  
  50. function WordToStr(Value: word): string;  
  51. var  
  52. ResultPtr: PChar;  
  53. begin  
  54. SetLength(Result, 2);  
  55. ResultPtr:=@Result[1];  
  56. asm  
  57. MOV EAX, [ResultPtr]  
  58. MOV BX, Value  
  59. MOV [EAX], BX end;  
  60. end;  
  61.   
  62. // StrToWord() : Converts a 2 byte string to a WORD  
  63. function StrToWord(Value: string): word;  
  64. var  
  65. ValuePtr: PChar;  
  66. begin  
  67. ValuePtr:=@Value[1];  
  68. asm  
  69. MOV EAX, [ValuePtr]  
  70. MOV AX, [EAX]  
  71. MOV Result, AX end;  
  72. end;  
  73.   
  74.    
  75.   
  76. function HexStrToStr(const S:string):string;  
  77. //16进制字符串转换成字符串  
  78. var  
  79. t:Integer;  
  80. ts:string;  
  81. M,Code:Integer;  
  82. begin  
  83. t:=1;  
  84. Result:='';  
  85. while t<=Length(S) do  
  86. begin   //xlh 2006.10.21  
  87.     while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do  
  88.       inc(t);  
  89.     if (t+1>Length(S))or(not (S[t+1in ['0'..'9','A'..'F','a'..'f'])) then  
  90.       ts:='$'+S[t]  
  91.     else  
  92.       ts:='$'+S[t]+S[t+1];  
  93.     Val(ts,M,Code);  
  94.     if Code=0 then  
  95.       Result:=Result+Chr(M);  
  96.     inc(t,2);  
  97. end;  
  98. end;  
  99.   
  100. function StrToHexStr(const S:string):string;  
  101. //字符串转换成16进制字符串  
  102. var  
  103. I:Integer;  
  104. begin  
  105. for I:=1 to Length(S) do  
  106. begin  
  107.     if I=1 then  
  108.       Result:=IntToHex(Ord(S[1]),2)  
  109.     else Result:=Result+' '+IntToHex(Ord(S[I]),2);  
  110. end;  
  111. end;  
  112.   
  113.    
  114.   
  115.   
  116. // SetBit() : Sets a single BIT in a string to true or false  
  117. procedure SetBit(var Str: string; BitNr: dword; Value: boolean);  
  118. var  
  119. CharNr: dword;  
  120. CharBit: byte;  
  121. Original, Mask: byte;  
  122. begin  
  123. CharNr:=(BitNr DIV 8)+1;  
  124. CharBit:=(BitNr MOD 8);  
  125. Original:=byte(Str[CharNr]);  
  126. Mask:=1 shl CharBit;  
  127. if Value=true then  
  128. Original:=(Original or Mask)  
  129. else  
  130. Original:=(Original and not Mask);  
  131. Str[CharNr]:=char(Original);  
  132. end;  
  133.   
  134. // GetBit() : Returns the state of a single bit in a string  
  135. function GetBit(Str: string; BitNr: dword): boolean;  
  136. var  
  137. CharNr: dword;  
  138. CharBit: byte;  
  139. Original, Mask: byte;  
  140. begin  
  141. CharNr:=(BitNr DIV 8)+1;  
  142. CharBit:=(BitNr MOD 8);  
  143. Original:=byte(Str[CharNr]);  
  144. Mask:=1 shl CharBit;  
  145. if (Original and Mask)=Mask then  
  146. Result:=true  
  147. else  
  148. Result:=false;  
  149. end;  
  150.   
  151. // Pack() : Compresses a string to a hopefully smaller string  
  152. function Pack(I: string):string;  
  153. var  
  154. Header: string;  
  155. Tag,T1,T2: string;  
  156. Buffer: string;  
  157.   
  158. History: string;  
  159. FindStr: string;  
  160. P: integer;  
  161. FP,FL: integer;  
  162. begin  
  163. SetLength(Tag,(Length(I) DIV 8)+1); // Create TAG string  
  164. Header:=DwordToStr(Length(I)); // Create Header string (length of original)  
  165.   
  166. // Pack the string  
  167. P:=1while P<=Length(I) do begin  
  168. FindStr:=Copy(I,P,10);  
  169. FindBest(History,FindStr,FL,FP);  
  170. if FL>2 then begin // if match found in history and length>2  
  171. Buffer:=Buffer+WordToStr((FP SHL 3)+(FL-3));  
  172. History:=History+Copy(History,FP,FL);  
  173. T1:=Copy(I,P,FL);  
  174. T2:=Copy(History,FP,FL);  
  175. SetBit(Tag,P-1,true);  
  176. P:=P+(FL-1);  
  177. end else begin // if no match found in history  
  178. Buffer:=Buffer+I[P];  
  179. History:=History+I[P];  
  180. SetBit(Tag,P-1,false);  
  181. end;  
  182. if Length(History)>8100 then History:=Copy(History,1024,8100); INC(P);  
  183. end;  
  184.   
  185. Result:=Header+Tag+Buffer;  
  186. end;  
  187.   
  188. // UnPack() : DeCompresses a string compressed with Pack()  
  189. function UnPack(I: string): string;  
  190. var  
  191. Tag,T: string;  
  192. Buffer: string;  
  193.   
  194. TmpWrd: string;  
  195. History: string;  
  196. P, OL: integer;  
  197. FP, FL: integer;  
  198. begin  
  199. // Split I in Tag and Buffer  
  200. OL:=StrToDword(I);  
  201. SetLength(Buffer, OL);  
  202. SetLength(Tag,(OL DIV 8)+1);  
  203. P:=5;  
  204. Tag:=Copy(I,P,Length(Tag));  
  205. P:=P+Length(Tag);  
  206. Buffer:=Copy(I,P,Length(Buffer));  
  207. Result:='';  
  208.   
  209. // begin unpacking  
  210. P:=1while Length(Result)<OL do begin  
  211. if GetBit(Tag, Length(Result))=true then begin // if is packed  
  212. TmpWrd:=Buffer[P]+Buffer[P+1];  
  213. FL:=(StrToWord(TmpWrd) and 7)+3;  
  214. FP:=(StrToWord(TmpWrd) shr 3and 8191;  
  215. Result:=Result+Copy(History,FP,FL);  
  216. History:=History+Copy(History,FP,FL);  
  217. T:=Copy(History,FP,FL);  
  218. P:=P+1;  
  219. end else begin // if is not packed  
  220. Result:=Result+Buffer[P];  
  221. History:=History+Buffer[P];  
  222. end;  
  223. if Length(History)>8100 then History:=Copy(History,1024,8100); INC(P);  
  224. end;  
  225. end;  
  226.   
  227. // FindBest() : Finds a substring in another string an returns position and  
  228. // the number of characters upto where they are equal  
  229. procedure FindBest(Main, Sub: string;var FoundLen, FoundPos: integer);  
  230. var  
  231. P,T,FL,MaxLen: integer;  
  232. begin  
  233. if Length(Sub)>Length(Main) then  
  234. MaxLen:=Length(Main)  
  235. else  
  236. MaxLen:=Length(Sub);  
  237. FoundLen:=0; FoundPos:=0;  
  238. for P:=1 to Length(Main)-MaxLen do begin  
  239. FL:=0;  
  240. for T:=1 to MaxLen do begin  
  241. if Main[P+T-1]=Sub[T] then FL:=T else Break;  
  242. end;  
  243. if FL>FoundLen then begin  
  244. FoundLen:=FL;  
  245. FoundPos:=P;  
  246. end;  
  247. end;  
  248. end;  
  249.   
  250. end.  
  251.   
  252. 在做一些和硬件通讯的程序中,交换和读写的数据格式有时是 16 进制,比如说从 IC 卡 或 ID 卡获取到的数据,把他们转换成 10 进制可能更便于使用。   
  253. Delphi 中 16 进制数以 $ 为前缀,所以将 16 进制形式的字符串转换为整数即可:  
  254.   
  255. function HexToDec(const AHexString: String): Integer;  
  256. begin  
  257. // 16 进制转换为 10 进制;  
  258. Result := StrToInt(’$’ + AHexString);  
  259. end;  
  260.   
  261.    
  262.   
  263.    
  264.   
  265.    
  266.   
  267. //十六进制(S)-->>十进制(I)  [重写:Jey]  
  268. function hextoint(s: string): Integer;  
  269. begin          //$代表16进制  
  270.   Result:=StrToInt('$'+s);  
  271. end;  
  272.   
  273. //十进制转换为二进制字符串  [重写:Jey]  
  274. function inttoBin(i: integer): string;  
  275. begin  
  276.  while i <>0 do  
  277.  begin          //i mod 2取模,再使用format格式化  
  278.    result:=Format('%d'+result,[i mod 2]);  
  279.    i:=i div 2  
  280.  end  
  281. end;  
  282.   
  283. //二进制(S)-->>十进制(D)    [重写:Jey]  
  284. uses Math;   
  285. function hextoint(s: string): Double;  
  286. begin  
  287.   while Length(s) <>0 do  
  288.   begin          //2^(长度-1)次方  
  289.     if s[1]='1' then  Result:=Result+power(2,Length(s)-1);  
  290.     s:=Copy(s,2,Length(s));  
  291.   end  
  292. end;  
  293.   
  294. //十进制(I)-->>十六进制(S)  
  295. //D自带函数,Digits长度,一般设4.  
  296. function IntToHex(Value: Integer; Digits: Integer): string;  
  297.   
  298. //数据(S)-->>二进制(S)   
  299. //任何数据都是以二进制形式存储的! (转)   
  300. function conertde(s:string):string;   
  301. var   
  302.  i:integer;   
  303. begin   
  304.  for i:=1 to length(s) do   
  305.    result:=result+inttohex(ord(s[i]),2);   
  306. end;  
  307.   
  308. 16进制字符转换为10进制 delphi  
  309. Function HexToDec(const Value :Integer ) : string;  
  310. var  
  311. s : string;  
  312. begin  
  313.   s := '$' + IntToStr(Value);  
  314.   Result := InToStr(StrToInt(s));  
  315. end;  
  316.   
  317. 2)方法2  
  318. Funtion HexToDec(const Value :Integer) : string;  
  319. CONST HEX : ARRAY['A'..'F'] OF INTEGER =   
  320. (10,11,12,13,14,15);  
  321. VAR  
  322.   str : String;  
  323.   In : Integer;  
  324.   i : integer;  
  325. BEGIN  
  326.   Str := UpperCase(IntToStr(Value));  
  327.   Int := 0;  
  328.   FOR i := 1 TO Length(str) DO  
  329.     IF str[i] < 'A' THEN  
  330.     Int := Int * 16 + ORD(str[i]) - 48  
  331.   ELSE  
  332.     Int := Int * 16 + HEX[str[i]];  
  333.     
  334.   Result := IntToStr(Int);  
  335. end;  

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值