GSM规范中的部分编码转换
2005-12-28 21:28
211 查看
在做跟手机短信相关的东东时候常遇到各种编码格式数据的转换,特写了几个函数,供参考。 function EncodeEnglish(var s:string):string;
var
i,j,len:Integer;
cur:Integer;
t:String;
begin
Result:='''';
len:=Length(s);
//j 用于移位计数
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]);
Result:=Result+t;
inc(i);
//移位计数达到7位的特别处理
j:=(j+1) mod 7;
if j=0 then inc(i);
end;
end;
//end;
function BinaryUniCode2Gb2312(ABinaryString:PChar;APosStart,APosEnd:integer):string;
var
i,iLen:integer;
AscHexText,TmpHexStr:string;
AsciiInt:integer ;
AscLen,AscUniLen:integer;
UniHexstr,GB2312:string;
begin
iLen:=Length(ABinaryString);
for i:= APosStart-1 to APosEnd-1 do
begin
AsciiInt:=ord(ABinaryString[i]);
TmpHexStr:=Format(''%x'',[AsciiInt]);
if length(TmpHexStr)=1 then
TmpHexStr:=''0''+TmpHexStr;
AscHexText:=AscHexText+TmpHexStr;
end; //for
AscLen:=Length(AscHexText);
AscUniLen:=AscLen div 4;
for i:=0 to AscUniLen-1 do
begin
UniHexstr:=Copy(AscHexText,i*4+1,4);
// Gb2312 := Gb2312 + UnicodeToGb2312(HexAscii2DecimalInt(uniHexstr));
end; // for
result := Gb2312 ;
end;
function WideStringToUnicode_Ex(s:WideString;ADestStr:PChar;Limit:integer):integer;
var sLen:integer;
buffer:array[1..1024] of char;
tmpchar:char;
i:integer;
begin
sLen:=Length(s);
if sLen>0 then
begin
CopyMemory(@buffer,Pointer(s),2*SLen);
for i:=0 to SLen-1 do
begin
tmpchar:=buffer[2*i+1];
buffer[2*i+1]:=buffer[2*i+2];
buffer[2*i+2]:=tmpchar;
end;
if SLen>Limit then
SLen:=Limit;
CopyMemory(ADestStr,@buffer,2*SLen);
result:=SLen*2;
result:=2*SLen;
end
else
begin
result:=0;
end;
end;
var
i,j,len:Integer;
cur:Integer;
t:String;
begin
Result:='''';
len:=Length(s);
//j 用于移位计数
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]);
Result:=Result+t;
inc(i);
//移位计数达到7位的特别处理
j:=(j+1) mod 7;
if j=0 then inc(i);
end;
end;
//end;
function BinaryUniCode2Gb2312(ABinaryString:PChar;APosStart,APosEnd:integer):string;
var
i,iLen:integer;
AscHexText,TmpHexStr:string;
AsciiInt:integer ;
AscLen,AscUniLen:integer;
UniHexstr,GB2312:string;
begin
iLen:=Length(ABinaryString);
for i:= APosStart-1 to APosEnd-1 do
begin
AsciiInt:=ord(ABinaryString[i]);
TmpHexStr:=Format(''%x'',[AsciiInt]);
if length(TmpHexStr)=1 then
TmpHexStr:=''0''+TmpHexStr;
AscHexText:=AscHexText+TmpHexStr;
end; //for
AscLen:=Length(AscHexText);
AscUniLen:=AscLen div 4;
for i:=0 to AscUniLen-1 do
begin
UniHexstr:=Copy(AscHexText,i*4+1,4);
// Gb2312 := Gb2312 + UnicodeToGb2312(HexAscii2DecimalInt(uniHexstr));
end; // for
result := Gb2312 ;
end;
function WideStringToUnicode_Ex(s:WideString;ADestStr:PChar;Limit:integer):integer;
var sLen:integer;
buffer:array[1..1024] of char;
tmpchar:char;
i:integer;
begin
sLen:=Length(s);
if sLen>0 then
begin
CopyMemory(@buffer,Pointer(s),2*SLen);
for i:=0 to SLen-1 do
begin
tmpchar:=buffer[2*i+1];
buffer[2*i+1]:=buffer[2*i+2];
buffer[2*i+2]:=tmpchar;
end;
if SLen>Limit then
SLen:=Limit;
CopyMemory(ADestStr,@buffer,2*SLen);
result:=SLen*2;
result:=2*SLen;
end
else
begin
result:=0;
end;
end;
相关文章推荐
- 网站开发编码规范(参考Google Java部分编码规范)
- 【笔记】JavaScript编码规范- 类型分配&强制转换
- GSM规范中的部分编码转换
- GSM规范中的部分编码转换
- 数据类型转换和编码规范
- python编码规范工具PyLint (未亲测,但是头部分具有参考价值)
- C语言的类型系统-编码, 类型转换及其规范
- java主类结构、基本数据类型、 变量与常量、运算符、数据类型转换、代码注释与编码规范
- 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码
- native2ascii.exe程序可以用他来转换编码
- Objective-C编码规范:26个方面解决iOS开发问题
- PEP8 Python 编码规范整理
- NSString和NSData通过Base64编码的相互转换
- 团队项目开发"编码规范"之一:概述
- 团队项目开发"编码规范"之八:代码格式检查
- [iOS]把字符串汉字转换成UTF-8编码
- PHP编码规范
- Python 编码的部分总结
- android json数据传递时编码格式的转换
- 当前使用的IP地址有4个字节(32)组成,即IPV4编码方式。每个IP地址包换两部分:网络号和主机号。当分配给主机号的二进制位越多,则能标识的主机数就越多,相应地能标识的网络数就越少,反之亦然。