想把一组二进制数据转换成文本BB A6 C9 EE 33 30 30 这且数据对应的字应该是“沪深300”,使用什么方法可以把他转换成功
解决方案 »
- XE3的帮助文件是怎么回事?不提供帮助文档?
- QQ2010皮肤控件正式免费开放(支持2007和XE)
- 赵本山已与borland签协议:收购borland的ide部门。
- 又一个串口问题没搞懂,多谢谢指教
- 一个摄像头的问题
- quickreport1内字段太多,做两张纸打印完20字段
- 动态创建一个panel时如何产生一个onclick事件??
- 会delphi7的进.只要知道delphi的人都进来!!!!!!!!!!!
- 怎样处理鼠标拖动事件。
- MIDAS三层结构,ActiveForm客户端发布所需库文件列表
- Fastreport Chart 动态Bar Series
- 有没有类似dbcombobox的控件,有个value属性可以绑定到数据库的字段?
procedure TForm1.Button1Click(Sender: TObject);
var
stream: TStringStream;
begin
stream := TStringStream.Create;
stream.Size := 8;
stream.Bytes[0] := $BB;
stream.Bytes[1] := $A6;
stream.Bytes[2] := $C9;
stream.Bytes[3] := $EE;
stream.Bytes[4] := $33;
stream.Bytes[5] := $30;
stream.Bytes[6] := $30;
stream.Bytes[7] := $00; EDIT1.Text:=stream.DataString; {我们}
stream.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s:string[7];
begin
s[1]:=chr($BB);
s[2]:=chr($A6);
s[3]:=chr($C9);
s[4]:=chr($EE);
s[5]:=chr($33);
s[6]:=chr($30);
s[7]:=chr($30);
showmessage(s);
end;
//16进制的字符串转换化为内存块
procedure HexStrToBuf(const Text:String;var Buf:Pointer;var Len:Integer);
var
i:Integer;
S:String;
Arr:array of Byte;
begin
//如 AA FF 01 05 51 转换为数据
Len:=0;
SetLength(Arr,1024);
for i:=1 to Length(Text) do
begin
if Text[i]=' ' then
else
S:=S+Text[i];
if Length(S)=2 then
begin
Arr[Len]:=StrToIntDef('$'+S,0);
Inc(Len);
S:='';
end;
end;
if Len>0 then
begin
Buf:=GetMemory(Len);
CopyMemory(Buf,@Arr[0],Len);
end;
SetLength(Arr,0);
end;
//检查是否是中文
function IsMBCSChar(const ch: Char): Boolean;
begin
Result := (ByteType(ch, 1) <> mbSingleByte);
end;
//内存块转换为字符串
function BufToString(Buf:Pointer;Len:Integer):String;
var
i:Integer;
PB:PByte;
PC:Array [0..1] of Char;
begin
//有中文字符串进行判断
PB:=Buf;
i:=0;
while i<Len do
begin
if IsMBCSChar(Char(PB)) then
begin
CopyMemory(@PC[0],PB,2);
if 0=i then
Result:= PC
else
Result:=Result+''+PC;
PB:=Pointer(Integer(Buf)+i+2);
i:=i+2;
end
else
begin
if 0=i then
Result:=Char(PB^)
else
Result:=Result+''+Char(PB^);
PB:=Pointer(Integer(Buf)+i+1);
i:=i+1;
end;
end;
end;//测试代码
var
S:String;
buf:Pointer;
Len:Integer;
begin
S:='BB A6 C9 EE 33 30 30';
HexStrToBuf(S,buf,Len);
S:=BufToString(buf,Len);
FreeMemory(Buf);
Showmessage(S);
end;
var
S:String;
buf:Pointer;
Len:Integer;
begin
S:='BB A6 C9 EE 33 30 30';
HexStrToBuf(S,buf,Len);
S:=StrPas(buf); //7楼的方法
FreeMemory(Buf);
Showmessage(S);
end;
function GetChars(aHexStr: string): string;
var
i: Integer;
begin
aHexStr := StringReplace(aHexStr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
for i := 1 to Length(aHexStr) do
begin
if Odd(i) then
begin
Result := Result + Char(StrToIntDef('$' + Copy(aHexStr, i, 2), 0));
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := GetChars('BB A6 C9 EE 33 30 30');
end;
var
i, iLen: Integer;
begin
Result := asHex;
iLen := Length(Result);
if Odd(iLen) then
begin
Result := '0' + Result;
Inc(iLen);
end; for i := iLen downto 1 do
begin
if Odd(i) then Continue; Insert(' ', Result, i - 1);
end;
Result := Trim(Result);
end;
function GetHex(const aStr: string): string;
var
i: Integer;
begin
for i := 1 to Length(aStr) do
begin
Result := Result + Format('%.2x', [Ord(aStr[i])]); //IntToHex(Ord(S[i]), 2)
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text := FormatHexDisp(GetHex(s))
end;