Delphi中国  
首页 | 电脑常识 | 业界动态 | Delphi相关 | 最新源码 | 网络文摘 | 常用工具 | 专题 | 会员中心
  当前位置:首页>Delphi相关>算法相关>文章内容

Delphi中各种进制的转换
来源:www.delphi86.com 作者:蜗牛 发布时间:2008-07-04  

//*********************************************************
//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;

//*******************************************************************
//该函数接收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;

//****************************
//字符串转换成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;

//***************************
//该函数去掉字符串中所有空格
//***************************
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;

//***************************
//十进制转换成二进制字符串
//***************************
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;

//***************************
//十进制转换成十六进制字符串
//***************************

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;


(阅读次数:

上一篇:使用Delphi编写聊天程序   下一篇:将excel导入到Delphi中
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·你的人民币金额大写数字规范吗
·基本算法(用 PASCAL 描述)
·正则表达式概述
·最精简的小写金额转大写的函数
·delphi递归处理树型结构
·输入汉字自动产生拼音简码
·建立应用软件注册安全机制
·分享Pos函数。(比FastPos还要快)
·Nearest Neighbor、Bilinear、Bi
  相关文章
·分享Pos函数。(比FastPos还要快)
·正则表达式概述
·Nearest Neighbor、Bilinear、Bi
·基本算法(用 PASCAL 描述)
·输入汉字自动产生拼音简码
·建立应用软件注册安全机制
·最精简的小写金额转大写的函数
·你的人民币金额大写数字规范吗
·delphi递归处理树型结构

Delphi中国
苏ICP备07008953