我现在得到了一个很长的(128个)由0,1组成的字符串
我想把它们每四位一组,转为16进制
比如原来的字符串是:0100101000001101
转换后得到4A0D请问怎样实现
我用2进制转16进制的函数试过但是结果不正确,是不是由于串过长的原因??
我想把它们每四位一组,转为16进制
比如原来的字符串是:0100101000001101
转换后得到4A0D请问怎样实现
我用2进制转16进制的函数试过但是结果不正确,是不是由于串过长的原因??
function BinToHex(x:string):string;
var h,temp:string;
b,n:integer;
begin
result:='';
temp:=RightStr(x,4);
while Length(temp)>0 do
begin
b:=StrToInt(temp);
n:=(b div 1000)*8+(b div 100 mod 10)*4+
(b div 10 mod 10)*2+b mod 10;
case n of
10: h:='A';
11: h:='B';
12: h:='C';
13: h:='D';
14: h:='E';
15: h:='F';
else h:=IntToStr(n);
end;
x:=copy(x,1,length(x)-4);
result:=h+result;
temp:=RightStr(x,4);
end;
end;
function BinToHexEx(Const ABitStr:String):String;
var
AHexStr:String[4];
AStrPos:PChar;
AIndex:Integer;
ALen:Integer;
begin
AStrPos:=PChar(ABitStr);
ALen:=Length(ABitStr) div 4 ;
SetLength(Result,ALen);
AIndex:=0;
While 0<ALen do
begin
SetString(AHexStr,AStrPos,4);
Inc(AIndex,1);
Inc(ALen,-1);
Inc(AStrPos,4);
if AHexStr='0000' then Result[AIndex]:='0' else
if AHexStr='0001' then Result[AIndex]:='1' else
if AHexStr='0010' then Result[AIndex]:='2' else
if AHexStr='0011' then Result[AIndex]:='3' else
if AHexStr='0100' then Result[AIndex]:='4' else
if AHexStr='0101' then Result[AIndex]:='5' else
if AHexStr='0110' then Result[AIndex]:='6' else
if AHexStr='0111' then Result[AIndex]:='7' else
if AHexStr='1000' then Result[AIndex]:='8' else
if AHexStr='1001' then Result[AIndex]:='9' else
if AHexStr='1010' then Result[AIndex]:='A' else
if AHexStr='1011' then Result[AIndex]:='B' else
if AHexStr='1100' then Result[AIndex]:='C' else
if AHexStr='1101' then Result[AIndex]:='D' else
if AHexStr='1110' then Result[AIndex]:='E' else
if AHexStr='1111' then Result[AIndex]:='F' else Result[AIndex]:='0';
end;
end;
const
CO_HEXSTR='00102001130100401015011060111710008100191010A1011B1100C1101D1110E1111F00011';
var
AStrPos,ABinPos:PChar;
AIndex:Integer;
ALen:Integer;
AHexStr:String;
begin
ABinPos:=PChar(ABntStr);
ALen:=Length(ABntStr) div 4 ;
SetLength(Result,ALen);
AIndex:=1;
While AIndex<=ALen do
begin
SetString(AHexStr,ABinPos,4);
AStrPos:=StrPos(CO_HEXSTR,PChar(AHexStr));
if AStrPos=nil then
Result[AIndex]:='0'
else
Result[AIndex]:=(AStrPos+4)[0];
Inc(AIndex,1);
Inc(ABinPos,4);
end;
end;