转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi下常用转换(一)         ★★★★

Delphi下常用转换(一)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:675 更新时间:2009/4/23 18:43:24

Delphi下常用转换
作者:lyboy99
e-mail:lyboy99@sina.com  
url: http://hnh.126.com
给大家提供几个常用的转换方法和其转换函数
希望可以对你有帮助

1.HEX -> Integer
2.2.Dec To HEX
3.ASCII to HEX / math
4.Binary to decimal

==============================================================
1.HEX -> Integer
==============================================================
方法. 1
--------------------------------------------------------------------------------

var
  i : integer
  s : string;
begin
  s := ''''$'''' + ThatHexString;
  i := StrToInt(a);
end;


--------------------------------------------------------------------------------

方法. 2

--------------------------------------------------------------------------------

CONST HEX : ARRAY[''''A''''..''''F''''] OF INTEGER = (10,11,12,13,14,15);
VAR str : String;
    Int,
    i   : integer;
BEGIN
  READLN(str);
  Int := 0;
  FOR i := 1 TO Length(str) DO
    IF str[i] < ''''A'''' THEN Int := Int * 16 + ORD(str[i]) - 48
    ELSE Int := Int * 16 + HEX[str[i]];
  WRITELN(Int);
  READLN;
END.

===================================================
2.Dec To HEX

--------------------------------------
HexString := Format(''''%0x'''',DecValue);
--------------------------------------


====================================================
3.ASCII to HEX / math
------------------------
unit Hexstr;

interface
uses String16, SysUtils;

Type
 PByte = ^BYTE;

procedure BytesToHexStr(var hHexStr: String; pbyteArray: PByte; InputLength: WORD);
procedure HexStrToBytes(hHexStr: String; pbyteArray: Pointer);
procedure HexBytesToChar(var Response: String; hexbytes: PChar; InputLength: WORD);

implementation
procedure BytesToHexStr(var hHexStr: String; pbyteArray: PByte; InputLength: WORD);
Const
    HexChars : Array[0..15] of Char = ''''0123456789ABCDEF'''';
var
    i, j: WORD;
begin
 SetLength(hHexStr, (InputLength * 2));
 FillChar(hHexStr, sizeof(hHexStr), #0);
 j := 1;
 for i := 1 to InputLength  do begin
    hHexStr[j] := Char(HexChars[pbyteArray^ shr  4]); inc(j);
    hHexStr[j] := Char(HexChars[pbyteArray^ and 15]); inc(j);
    inc(pbyteArray);
 end;
end;

procedure HexBytesToChar(var Response: String; hexbytes: PChar; InputLength: WORD);
var
 i: WORD;
 c: byte;
begin
 SetLength(Response, InputLength);
 FillChar(Response, SizeOf(Response), #0);
 for i := 0 to (InputLength - 1) do begin
   c := BYTE(hexbytes[i]) And BYTE($f);
   if c > 9 then
     Inc(c, $37)
   else
     Inc(c, $30);
   Response[i + 1] := char(c);
 end;{for}
end;

procedure HexStrToBytes(hHexStr: String; pbyteArray: Pointer);
{pbyteArray must point to enough memory to hold the output}
var
 i, j: WORD;
 tempPtr: PChar;
 twoDigits : String[2];
begin
 tempPtr := pbyteArray;
 j := 1;
 for i := 1 to (Length(hHexStr) DIV 2) do begin
   twoDigits := Copy(hHexStr, j, 2); Inc(j, 2);
   PByte(tempPtr)^ := StrToInt(''''$'''' + twoDigits); Inc(tempPtr);
 end;{for}
end;

end.


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

UNIT String16.
interface
{$IFNDEF Win32}
  procedure SetLength(var S: string; Len: Integer);
  procedure SetString(var Dst: string; Src: PChar; Len: Integer);
{$ENDIF}
implementation
{$IFNDEF Win32}
  procedure SetLength(var S: string; Len: Integer);
  begin
    if Len > 255 then
      S[0] := Chr(255)
    else
      S[0] := Chr(Len)
  end;

  procedure SetString(var Dst: string; Src: PChar; Len: Integer);
  begin
    if Len > 255 then
      Move(Src^, Dst[1], 255)
    else
      Move(Src^, Dst[1], Len);
    SetLength(Dst, Len);
  end;
{$ENDIF}
end.

=================================================================
4.Binary to decimal
-----------------------------------------------------------------

////////////////////////////////////////////////
// convert 32 bit base2 to 32 bit base10      //
// max number = 99 999 999, return -1 if more //
////////////////////////////////////////////////

function Base10(Base2:Integer) : Integer; assembler;
asm
  cmp    eax,100000000            // check upper limit
  jb     @1                       // ok
  mov    eax,-1                   // error flag
  jmp    @exit                    // exit with -1
@1:
  push   ebx                      // save registers
  push   esi
  xor    esi,esi                  // result = 0


  mov    ebx,10                   // diveder base 10
  mov    ecx,8                    // 8 nibbles (10^8-1)
@2:
  mov    edx,0                    // clear remainder
  div    ebx                      // eax DIV 10, edx mod 10
  add    esi,edx                  // result = result + remainder[I]
  ror    esi,4                    // shift nibble
  loop @2                       // loop for all 8 nibbles
  mov    eax,esi                  // function result
  pop    esi                      // restore registers
  pop    ebx
@exit:
end;


--------------------------------------------------------------------------------

方法. 2
[Oliver Townshend, oliver@zip.com.au]


--------------------------------------------------------------------------------

function IntToBin(Value: LongInt;Size: Integer): String;
var
 i: Integer;
begin
 Result:='''''''';
 for i:=Size downto 0 do begin
    if Value and (1 shl i)<>0 then begin
       Result:=Result+''''1'''';
  end else begin
       Result:=Result+''''0'''';
  end;
 end;
end;

function BinToInt(Value: String): LongInt;
var
 i,Size: Integer;
begin
 Result:=0;
 Size:=Length(Value);
 for i:=Size downto 0 do begin
    if Copy(Value,i,1)=''''1'''' then begin
       Result:=Result+(1 shl i);
  end;
 end;
end;


===========================================================================


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台