Ls := '$1.541';//要转化的16进制字符串 decs := ? 我怎样将Ls赋于DECS---- ShowMessage(IntToStr(StrToInt('$F012'))); 这样肯定可以但是我要的不是整形啊
unction StringToHex(S: string): string; var i: integer;begin Result := ''; for i := 1 to Length( S ) do Result := Result + IntToHex( Ord( S[i] ), 2 ); end;
function StringToHex(S: string): string; var i: integer;begin Result := ''; for i := 1 to Length( S ) do Result := Result + IntToHex( Ord( S[i] ), 2 ); end;将字符串转为16进制字符串.
取出字符串中的十六进制子字符串,如 var s1,s2:string n1:integer; begin s1:='dingding7829a7ss'; s2:=copy(s1,9,6) s2:='$'+s2 n2:=strtoin(s2) end; 保证你成功。 丁丁 2003.06.18
//sHex,for example,as: '$AF21' or '$AF21.159' //else return error value: -1; function HexToDec(sHex: String): Integer; var i,nLen: Integer; //loop var nValue: Integer; //converted value bSHL: Byte; //shl bit intStr,radixStr: string; //string with integer and radix point nPos: Integer; //the postion of '.' or '$' begin //error nPos := pos('$',sHex); //nil or '$' invalidate position string if (sHex = '') or (nPos <> 1) then begin result := -1; Exit; end; nLen := Length(sHex); //include '$' and it lies in first position of the string for i := 2 to nLen do begin if not (sHex[i] in ['0'..'9','a'..'f','A'..'F']) then begin result := -1; Exit; end; end; nValue := 0; nPos := Pos('.',sHex); if ( nPos <> 0 ) then //include '.' begin intStr := Copy(sHex,2,nPos-1); //integer part Delete(sHex,1,nPos); radixStr := sHex; //radix point part //first integer part bSHL := 0; nLen := Length(intStr); for i := nLen to 1 do begin nValue := nValue + (StrToInt('$'+intStr[i]) SHL (4*bSHL)); bSHL := bSHL + 1; end; //second radix point part bSHL := 0; nLen := Length(radixStr); for i := 1 to nLen do begin nValue := nValue + (StrToInt('$'+radixStr[i]) SHR (4*bSHL)); bSHL := bSHL + 1; end; //return result := nValue; end else //exclude '.' begin nLen := Length(sHex); bSHL := 0; for i := nLen downto 2 do begin nValue := nValue + (StrToInt('$'+sHex[i]) SHL (4*bSHL)); bSHL := bSHL + 1; end; result := nValue; end; end;
sorry,少写了个点。 nLen := Length(sHex); //include '$' and it lies in first position of the string for i := 2 to nLen do begin if not (sHex[i] in ['.','0'..'9','a'..'f','A'..'F']) then //Here Add '.' begin result := -1; Exit; end; end;
晕了。返回值错了。所有更正如下: ////////////////////////////////////////////////////////////////////////////////////sHex,for example,as: '$AF21' or '$AF21.159' //else return error value: -1; function HexToDec(sHex: String): Double; var i,nLen: Integer; //loop var nValue: Double; //converted value bSHL: Integer; //shl bit intStr,radixStr: string; //string with integer and radix point nPos: Integer; //the postion of '.' or '$' begin //error nPos := pos('$',sHex); //nil or '$' invalidate position string if (sHex = '') or (nPos <> 1) then begin result := -1; Exit; end; nLen := Length(sHex); //include '$' and it lies in first position of the string for i := 2 to nLen do begin if not (sHex[i] in ['.','0'..'9','a'..'f','A'..'F']) then begin result := -1; Exit; end; end; nValue := 0.0; nPos := Pos('.',sHex); if ( nPos <> 0 ) then //include '.' begin intStr := Copy(sHex,2,nPos-2); //integer part Delete(sHex,1,nPos); radixStr := sHex; //radix point part //first integer part bSHL := 0; nLen := Length(intStr); for i := nLen downto 1 do begin nValue := nValue + (StrToInt('$'+intStr[i]) SHL (4*bSHL)); bSHL := bSHL + 1; end; //second radix point part bSHL := 1; nLen := Length(radixStr); for i := 1 to nLen do begin nValue := nValue + (StrToInt('$'+radixStr[i]) / (16*bSHL)); bSHL := bSHL + 1; end; //return result := nValue; end else //exclude '.' begin nLen := Length(sHex); bSHL := 0; for i := nLen downto 2 do begin nValue := nValue + (StrToInt('$'+sHex[i]) SHL (4*bSHL)); bSHL := bSHL + 1; end; result := nValue; end; end;/////////////////////////////////////////////////////////////////////////////////
例如:Ls:string
decs:decimal;
Ls := '$1.541';//要转化的16进制字符串
decs := ?
我怎样将Ls赋于DECS----
ShowMessage(IntToStr(StrToInt('$F012')));
这样肯定可以但是我要的不是整形啊
var
i: integer;begin
Result := ''; for i := 1 to Length( S ) do
Result := Result + IntToHex( Ord( S[i] ), 2 );
end;
var
i: integer;begin
Result := ''; for i := 1 to Length( S ) do
Result := Result + IntToHex( Ord( S[i] ), 2 );
end;将字符串转为16进制字符串.
var
s1,s2:string
n1:integer;
begin
s1:='dingding7829a7ss';
s2:=copy(s1,9,6)
s2:='$'+s2
n2:=strtoin(s2)
end;
保证你成功。
丁丁
2003.06.18
//else return error value: -1;
function HexToDec(sHex: String): Integer;
var
i,nLen: Integer; //loop var
nValue: Integer; //converted value
bSHL: Byte; //shl bit
intStr,radixStr: string; //string with integer and radix point
nPos: Integer; //the postion of '.' or '$'
begin
//error
nPos := pos('$',sHex);
//nil or '$' invalidate position string
if (sHex = '') or (nPos <> 1) then
begin
result := -1;
Exit;
end; nLen := Length(sHex);
//include '$' and it lies in first position of the string
for i := 2 to nLen do
begin
if not (sHex[i] in ['0'..'9','a'..'f','A'..'F']) then
begin
result := -1;
Exit;
end;
end; nValue := 0;
nPos := Pos('.',sHex);
if ( nPos <> 0 ) then //include '.'
begin
intStr := Copy(sHex,2,nPos-1); //integer part
Delete(sHex,1,nPos);
radixStr := sHex; //radix point part //first integer part
bSHL := 0;
nLen := Length(intStr);
for i := nLen to 1 do
begin
nValue := nValue + (StrToInt('$'+intStr[i]) SHL (4*bSHL));
bSHL := bSHL + 1;
end; //second radix point part
bSHL := 0;
nLen := Length(radixStr);
for i := 1 to nLen do
begin
nValue := nValue + (StrToInt('$'+radixStr[i]) SHR (4*bSHL));
bSHL := bSHL + 1;
end; //return
result := nValue;
end
else //exclude '.'
begin
nLen := Length(sHex);
bSHL := 0;
for i := nLen downto 2 do
begin
nValue := nValue + (StrToInt('$'+sHex[i]) SHL (4*bSHL));
bSHL := bSHL + 1;
end;
result := nValue;
end;
end;
nLen := Length(sHex);
//include '$' and it lies in first position of the string
for i := 2 to nLen do
begin
if not (sHex[i] in ['.','0'..'9','a'..'f','A'..'F']) then //Here Add '.'
begin
result := -1;
Exit;
end;
end;
////////////////////////////////////////////////////////////////////////////////////sHex,for example,as: '$AF21' or '$AF21.159'
//else return error value: -1;
function HexToDec(sHex: String): Double;
var
i,nLen: Integer; //loop var
nValue: Double; //converted value
bSHL: Integer; //shl bit
intStr,radixStr: string; //string with integer and radix point
nPos: Integer; //the postion of '.' or '$'
begin
//error
nPos := pos('$',sHex);
//nil or '$' invalidate position string
if (sHex = '') or (nPos <> 1) then
begin
result := -1;
Exit;
end; nLen := Length(sHex);
//include '$' and it lies in first position of the string
for i := 2 to nLen do
begin
if not (sHex[i] in ['.','0'..'9','a'..'f','A'..'F']) then
begin
result := -1;
Exit;
end;
end; nValue := 0.0;
nPos := Pos('.',sHex);
if ( nPos <> 0 ) then //include '.'
begin
intStr := Copy(sHex,2,nPos-2); //integer part
Delete(sHex,1,nPos);
radixStr := sHex; //radix point part //first integer part
bSHL := 0;
nLen := Length(intStr);
for i := nLen downto 1 do
begin
nValue := nValue + (StrToInt('$'+intStr[i]) SHL (4*bSHL));
bSHL := bSHL + 1;
end; //second radix point part
bSHL := 1;
nLen := Length(radixStr);
for i := 1 to nLen do
begin
nValue := nValue + (StrToInt('$'+radixStr[i]) / (16*bSHL));
bSHL := bSHL + 1;
end; //return
result := nValue;
end
else //exclude '.'
begin
nLen := Length(sHex);
bSHL := 0;
for i := nLen downto 2 do
begin
nValue := nValue + (StrToInt('$'+sHex[i]) SHL (4*bSHL));
bSHL := bSHL + 1;
end;
result := nValue;
end;
end;/////////////////////////////////////////////////////////////////////////////////