当前位置:首页 > Windows程序 > 正文

进制转换 函数表

2021-03-29 Windows程序

********************************************************

16转10,否则输出-1

function Hex(c: char): Integer;
var
  x: Integer;
begin
  if ( Ord(c)>= Ord(‘0‘)) and (Ord(c) <= Ord(‘9‘)) then
    x:= Ord(c) - Ord(‘0‘)
  else if (Ord(c) >= Ord(‘a‘)) and (Ord(c) <= Ord(‘f‘)) then
    x:= Ord(c) - Ord(‘a‘) + 10
  else if (Ord(c) >= Ord(‘A‘)) and (Ord(c) <= Ord(‘F‘)) then
    x:= Ord(c) - Ord(‘A‘) + 10
  else
    x:= -1;                                       //输入错误
  Result:= x;
end;

 
2.字符 TO 10

***************************************************************
该函数接收1~2个,字符转换成功后输出对应10进制数的值,否则输出-1 

function HexToInt(Str: string): Integer;
var
  tmpInt1, tmpInt2: Integer;
begin
  if Length(Str) = 1 then
  begin
    Result:= Hex(Str[1]);
  end
  else if Length(Str) = 2 then
  begin
    tmpInt1:= Hex(Str[1]);
    tmpInt2:= Hex(Str[2]);
    if (tmpInt1 = -1) or (tmpInt2 = -1) then
      Result:= -1
    else
      Result:= tmpInt1 * 16 + tmpInt2;
  end
  else
    Result:= -1;                                  //输入错误,,转换失败
end;

3.字符串 TO ASCII码

*******************************************************************
字符串转换成ASCII码字符串 

function StrToHexStr(const S: string): string;
var
  i: Integer;
begin
  for i:= 1 to Length(S) do
  begin
    if i = 1 then
      Result:= IntToHex(Ord(S[1]), 2)
    else
      Result:= Result + ‘ ‘ + IntToHex(Ord(S[i]), 2);
  end;
end;

4.字符串去掉所有空格

***********************************************************************

该函数去掉字符串中所有空格

function TrimAll(Str: string): string;
var
  mLen, i: Integer;
begin
  mLen:= Length(Str);
  //初始化返回值
  Result:= ‘‘;
  for i:= 0 to mLen do
  begin
    //是空格就去掉
    if Str[i] = ‘‘ then
      Continue;
    Result:= Result + Str[i];
  end;
end;

5.10 TO 2

**************************************************
十进制转换成二进制字符串 

function DTob(decimal:longint):string;
const symbols:string[16]=‘01‘;
var
  scratch:string;
  remainder:byte;
begin
  repeat
    remainder:=decimal mod 2;
    scratch:=symbols[remainder+1]+scratch;
    decimal:=decimal div 2;
  until(decimal=0);
  result:=scratch;
end;

6.10 TO16字符

**************************************************
1.十进制转换成十六进制字符串

function DToHex(decimal:longint):string;
const symbols:string[16]=‘0123456789abcdef‘;
var
  scratch:string;
  remainder:byte;
begin
  repeat
    remainder:=decimal mod 16;
    scratch:=symbols[remainder+1]+scratch;
    decimal:=decimal div 16;
  until(decimal=0);
  result:=scratch;
end;

2.十进制转十六进制字符

var HexArr: array[1..15]of string= (‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘);

function Int2Hex(Value: Integer): string;
    var
      iTemp: Integer;
      i: Integer;
    begin
      Result :=‘‘;
      i := 0;
      while i<4 do
      begin
        case i of
          0: iTemp := Value shr 24 and $FF;
          1: iTemp := Value shr 16 and $FF;
          2: iTemp := Value shr 8 and $FF;
          3: iTemp := Value and $FF;
        end;
        if not Boolean(iTemp) then Result := Result + ‘00‘
        else
        begin
          Result := Result + HexArr[iTemp div 16];
          Result := Result + HexArr[iTemp mod 16];
        end;
        Inc(i);
      end;
    end;

7.字符串 TO 16进制

*************************************************************************

Eidt1.Text:=StrToInt(‘$‘+‘0000FFFF‘);   //65535

 

 

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69856.html