用以下函数对英文字符串进行编码,即将8位ASCII编码转换为7位编码function Encode(var s:String):String;
var
i,j,len,cur:Integer;
t,s,returnstr:String;
begin
returnstr:='';
len:=length(s);
i:=1;
j:=0;
while i<=len do
begin
if i<len then
cur:=(ord(s[i]) shr j) or ((ord(s[i+1]) shl (7-j)) and $ff)
else
cur:=(ord(s[i]) shr j) and $7f;
FmtStr(t,'%2.2x',[cur]);
returnstr:=returnstr+t;
inc(i);
j:=(j+1) mod 7;
if j=0 then
inc(i);
end;
result:=returnstr;
end;求一解码函数,即将7位字符编码转换为8为字符编码,谢谢帮忙!
例如 1234 编码后得到31D98C06
求一函数把 31D98C06 转为1234
如果用中文就是16bit一个汉字(包括标点),所以中文只能由70个,这就是为啥西文不是中文的整2倍的原因。
你好! 谢谢你的指点,小弟功底不扎实,请帮忙把函数写出来吧,谢谢!
// PBuf输入需要解码的缓冲区地址指针,PRes为输出结果指针,Size为输出结果长度
procedure Decode7(const PBuf:PByte; var Size: Integer; PRes: PByte);
var
buff, buf: array[0..4095]of byte;
I, J: Integer;
B1: Byte;
begin
ZeroMemory(@buff, sizeof(buff));
ZeroMemory(@Buf, SizeOf(Buf));
if size> 3500 then
begin
showmessage('长度超长,超出部分将无效!');
size := 3500;
end;
CopyMemory(@Buf, PBuf, size);
I := 0;
J := 0;
while I < Size + 1 do
begin
case (I mod 8) of
0:
begin
B1 := Buf[J] shl 1;
Buff[I] := B1 shr 1;
Inc(I);
Inc(J);
end;
1..6:
begin
B1 := Buf[J] shl (1 + (I mod 8));
B1 := B1 shr 1;
Buff[I] := B1 or(Buf[J-1] shr (8 - (I mod 8)));
Inc(I);
Inc(J);
end;
7:
begin
Buff[I] := Buf[J-1] shr 1;
Inc(I);
Inc(Size);
end;
end;
end;
CopyMemory(PRes, @Buff, Size);
end;
其中Size输入时,请输入需要解码的缓冲区大小;输出后为解码后的缓冲区大小
function TfrmMain.GsmToString(s : string) : string;
var
maskValue : byte;
subStr,tStr : string;
sLen,subLen,count,i,j : integer;
begin
Result := '';
sLen := Length (s);
count := sLen div 7;
if sLen mod 7 <> 0 then
Inc (count);
for i := 1 to count do
begin
subStr := Copy (s,(i - 1) * 7 + 1,7);
subLen := Length (subStr);
SetLength (tStr,subLen + 1);
for j := 1 to subLen + 1 do
tStr[j] := #0;
maskValue := $7f;
for j := 1 to subLen do
begin
tStr[j] := Chr ((Ord (subStr[j]) and maskValue) shl (j-1)+Ord (tStr[j]));
tStr[j + 1] := Chr (Ord (subStr[j]) and (not maskValue) shr (8 - j));
maskValue := maskValue shr 1;
end;
Result := Result + tStr;
end;
if sLen mod 7 <> 0 then
Delete (Result,Length (Result),1);
end;