二进制或16进制和ASCALL码的转换函数;(150分)

  • 主题发起人 主题发起人 xiaocheng112415
  • 开始时间 开始时间
X

xiaocheng112415

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:小弟这厢有礼了。请你们帮我看看.能有原代码最好,能找到也不错。我是在写串口输入和输出时用的。因为串口只有二进制数,单片机也只接收16进制,而我发出的是ASCII码,即字符串。我还有就是我想知道哪有标识符的用法说明。delphi自带的帮助,英文我看不懂。呵呵。谢谢各位大侠。
 
DELPHI自带的原代码
function IntToHex(Value: Integer; Digits: Integer): string;
// FmtStr(Result, '%.*x', [Digits, Value]);
asm
CMP EDX, 32 // Digits < buffer length?
JBE @A1
XOR EDX, EDX
@A1: PUSH ESI
MOV ESI, ESP
SUB ESP, 32
PUSH ECX // result ptr
MOV ECX, 16 // base 16 EDX = Digits = field width
CALL CvtInt
MOV EDX, ESI
POP EAX // result ptr
CALL System.@LStrFromPCharLen
ADD ESP, 32
POP ESI
end;

function IntToHex(Value: Int64; Digits: Integer): string;
// FmtStr(Result, '%.*x', [Digits, Value]);
asm
CMP EAX, 32 // Digits < buffer length?
JLE @A1
XOR EAX, EAX
@A1: PUSH ESI
MOV ESI, ESP
SUB ESP, 32 // 32 chars
MOV ECX, 16 // base 10
PUSH EDX // result ptr
MOV EDX, EAX // zero filled field width: 0 for no leading zeros
LEA EAX, Value;
CALL CvtInt64

MOV EDX, ESI
POP EAX // result ptr
CALL System.@LStrFromPCharLen
ADD ESP, 32
POP ESI
end;
 
多谢,那如果是二进制呢,是该先转为16进制吧,还是也有原码。
 
delphi 标识符的用法谁有?发个到我邮箱好吗??
 
把ASCII码转成16进制:
Format('%.2x'#32, [Ord(Ascii码)])




unit BinHexTools;

interface

function IntToBin(I: Integer): string; //integer to 二进制
function HexToBin(HexNr : string): string;
function HexCharToInt(HexToken : char):Integer;
function HexCharToBin(HexToken : char): string;
function pow(base, power: integer): integer;
function BinStrToInt(BinStr : string) : integer;
function DecodeSMS7Bit(PDU : string):string;
function ReverseStr(SourceStr : string) : string;

implementation

uses sysutils, dialogs;

function IntToBin(I: Integer): string;
var
H: string;
begin
H := SysUtils.IntToHex(I, 2);
Result := HexToBin(H);
end;

function HexCharToInt(HexToken : char):Integer;
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }

Result:=0;

if (HexToken>#47) and (HexToken<#58) then { chars 0....9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { chars A....F }
Result:=Ord(HexToken)-65 + 10;
end;

function HexCharToBin(HexToken : char): string;
var DivLeft : integer;
begin
DivLeft:=HexCharToInt(HexToken); { first HEX->BIN }
Result:='';
{ Use reverse dividing }
repeat { Trick; divide by 2 }
if odd(DivLeft) then
{ result = odd ? then bit = 1 }
Result:='1'+Result { result = even ? then bit = 0 }
else
Result:='0'+Result;

DivLeft:=DivLeft div 2; { keep dividing till 0 left and length = 4 }
until (DivLeft=0) and (length(Result)=4); { 1 token = nibble = 4 bits }
end;

function HexToBin(HexNr : string): string;
{ only stringsize is limit of binnr }
var Counter : integer;
begin
Result:='';

for Counter:=1 to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;

function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;

for counter:=1 to power do
Result:=Result*base;
end;

function BinStrToInt(BinStr : string) : integer;
var counter : integer;
begin
if length(BinStr)>16 then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);

Result:=0;

for counter:=1 to length(BinStr) do
if BinStr[Counter]='1' then
Result:=Result+pow(2,length(BinStr)-counter);
end;

function DecodeSMS7Bit(PDU : string):string;
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';

for Counter:=1 to length(PDU) do
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0 then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';

PrevOctet:='';
end;

if Odd(Counter) then { only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);

Charbin:='';
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];

for Counter2:=1 to 7-length(PrevOctet) do
Charbin:=OctetBin[8-Counter2+1]+Charbin;

if BinStrToInt(Charbin)<>0 then Result:=Result+Chr(BinStrToInt(CharBin))
else Result:=Result+' ';

PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);
end;
end;
end;

function ReverseStr(SourceStr : string) : string;
var Counter : integer;
begin
Result:='';

for Counter:=1 to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
end;

end.
 
忘了给邮箱号了:xiaocheng112415@163.com
 
高手啊。谢谢。[8D]等标识符问题一起解决。马上给分
 
大侠jfyes,好象少了点什么?就算ASCII码用字符串表示,也没有strtohex函数
 
就象我输入04040040,输出应该是30 34 30 34 30 30 34 30。
反过来好象可以在上面单元凑出来。
 
大侠jfyes,不如你帮我想下怎么把
16位码‘00 ff fa da ed'变成string吧。还要求相加和,
 
谢谢大家,我靠一位师兄解决了;代码如下,大家一起看看。明天结贴,顶者都有分。。。
function Hexstradd(hexstr:string):string;
var i,count:integer;
total:integer;
c:string;
begin
total:=$00;
result:='';
c:='';

count:=length(hexstr);
try
for i:=1 to count do
begin
if hexstr<>' ' then
begin
c:=c+hexstr;

if length(c) = 2 then
begin
total:=total+strtoint('$'+c);
c:='';
end;
end;
end;
result:=inttostr(total);
except
end;

end;




function HexToASCII(const s: string): string;
var
I,Count: Integer;
C: string;
begin
C := '';
Result := '';

Count := Length(s);
try
for I := 1 to Count do
begin
if s <> ' ' then
begin
C := C + s;

if Length(C) = 2 then
begin
Result := Result + Char(StrToInt('$' + C));
C := '';
end;
end;
end;

if C <> '' then
Result := Result + Char(StrToInt('$' + C));
except
end;
end;

function ASCIIToHex(const s: string): string;
var
I: Integer;
begin
Result := '';

for I := 1 to Length(s) do
Result := Result + IntToHex(Ord(s),2);

Result := Trim(Result);
end;

function last_two(const s:string):string;
var count:integer;
begin
result:='';
try
count:=length(s);
result:=result+s[count-1];
result:=result+s[count];
except
showmessage('求和最后两位失败');
end;
end;
 
好象不用这么复杂吧,在PC这头如果是用ASCII来编码,可以这么做:
//字符串转换成ASCII字节数组(返回校验和)
function StrToBytes(ss: string; var bs:array of Byte):Byte;
var
i,Count:Integer;
v:Byte;
begin
v:=0;
Count:=Length(ss);
for i := 0 to Count-1 do
begin
bs:=Ord(ss[i+1]);
v:=v xor bs;//校验和
end;
Result:=v;
end;
//字节数组(ASCII表示的)转换成字符串
function BytesToStr(ba:array of Byte;StartIndex,EndIndex:Integer): string;
var
s:string;
i:Integer;
begin
s:='';
for i := StartIndex to EndIndex do
s:=s+Char(ba);
Result:=s;
end;
 
后退
顶部