字符串转为BYTE数组(50分)

R

RickLee

Unregistered / Unconfirmed
GUEST, unregistred user!
比如有一个字符串:This is 中国人
把它转为每个字符以两个字节表示形式的BYTE数组。
 
var
S:string;
begin
S:='字符串';
//字符串是可以这样的。当它是char数组
s[1],s[2]
//你看着办吧
end;
 
把一个BYTE数组:比如:byte[0] = 60 ,byte[1] =0 byte[2] =36 byte[3]=0,以二进制方式写入文件。另外,转化成BYTE的问题
有汉字时怎么办?
 
判断双字节
var
S:string;
W: widestring;
begin
S:='字符串';
w:=s;
if s[0]<>w[0] then
//你看着办吧,
end;
 
procedure TForm1.Button3Click(Sender: TObject);
var
s:widestring;
c:array of widechar;
n,i:integer;
begin
s:='This is 中国人';
n:=length(s);
setlength(c,n+1);
for i:=1 to n do c[i-1]:=s;
c[n]:=#0;
showmessage(c[0]);
showmessage(c[10]);
setlength(c,0);
end;
 
widechar为UNICODE字符
jsxjd is Good
 
中文在字符串数组中是两个字节表示一个中文,而英文却是一个字节一个,光从
字节内容无法将其分开,所以一开始就要将其分开,如何分开才是难点。
 
可以给我一个把字符串保存为UNICODE编码的文件的例子吗?就像用记事本把文件保存为UNICODE编码那样的。
 
有那么复杂吗?

var
str: string;
a: array of byte;
begin
str := '中国人';
SetLength(a,Length(str));
move(str[1],a[0],Length(str));

showmessage(pchar(@a[0]));
end;
 
把字符串保存到UniCode文件:
var s:String;
ws:WideString;
begin
s:='是中2332hjhjfd中国';
ws:=s;//这里实现普通字符串到UniCode字符串的转换,不过如果在非中文系统下,这句就没有用了.非中文系统必须有专们的函数来转换.
With TMemoryStream.Create do
begin
try
Write(ws[1],Length(ws)*2);
Seek(0,0);
SaveToFile('C:/a.txt');
finally
Free;
end;
end;
end;
 

Similar threads

顶部