关于DELPHI数组,指针,字符串转换的例子!

这段代码展示了在Delphi中如何在PChar、字符数组、指针和字符串之间进行数据转换。主要操作包括:PChar内容转数组、数组到指针、指针内容到字符串以及数组转回PChar。此外,还包含了一个函数`Client_Cmd`,它接收客户端发送的数据并进行解析,根据接收到的数据类型执行相应的操作。
摘要由CSDN通过智能技术生成

var
   aa:   array [0..5] of Char;
   bb:Pointer;
   cc:string;
   dd:PChar;

procedure TForm1.Button1Click(Sender: TObject);
begin


dd:='abcdef';
//以下是pchar内容转数组
FillChar(aa,length(dd),#0);
Move(dd^,aa,SizeOf(aa));

//以下是数组传递到指针
bb:=GetMemory(SizeOf(aa));
Move(aa,bb^,SizeOf(aa));

//以下是指针内容传递给字符串
SetLength(cc,SizeOf(aa));
Move(bb^,cc[1],SizeOf(aa));

FreeMemory(bb);//这个是释放内存

//以下是数组转pchar
dd:='';
dd:=PChar(@aa);

MessageBox(0,PChar('cc: '+cc),PChar('dd: '+dd),0);


end;

function Client_Cmd(dwConnID: HP_CONNID; const pData: Pointer; iLength: Integer): Boolean;
var
  sCmd: AnsiString;
  tmpJSONObject: TJSONObject;
  cmdType: DWORD;
  tmpRclient: RClient;
begin
  try
    try
      SetLength(sCmd, iLength);
      CopyMemory(pAnsichar(sCmd), pData, iLength);
      OutputDebugString_MZ(Format('接受到客户端请求%d > %s ', [dwConnID, sCmd]));
      tmpJSONObject := TJSONObject.ParseJSONValue(sCmd) as TJSONObject;
      if tmpJSONObject <> nil then
      begin
        cmdType := StrToInt(tmpJSONObject.GetValue('type').Value);
        case cmdType of
          注册客户端:
            begin
                //{"type":1,"clientName":'主机名',"mac":'sdfsdfsdf',""}
              tmpJSONObject.TryGetValue('clientName', tmpRclient.pcName);
              tmpJSONObject.TryGetValue('mac', tmpRclient.mac);
              tmpRclient.connID := dwConnID;
//              if dm. then

              ClientDictionary.AddOrSetValue(tmpRclient.pcName, tmpRclient);
              Exit;
            end;

        end;
      end;
    except
      OutputDebugString_MZ('客户端发包解析异常>' + sCmd);
    end;
  finally
  end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值