简体   繁体   English

将ansichar的数组复制到char delphi 10.2的数组中

[英]copy array of ansichar to array of char delphi 10.2

as we know from Delphi 2007 and below the strings are not Unicode 正如我们从Delphi 2007及以下知道的那样,字符串不是Unicode

Char is 1 byte 字符是1个字节

AnsiChar 1 byte AnsiChar 1个字节

from Delphi 2009 and above the strings are Unicode 从Delphi 2009及以上版本开始,字符串是Unicode

Char is 2 byte 字符是2个字节

AnsiChar 1 byte AnsiChar 1个字节

when i convert my Delphi code from Delphi6 to Delphi 10.2( unicode string) i faced problem of some functions take PAnsiChar to fill array of Char, but the pointer passed is PChar, in non Unicode Delphi i will not get any error 当我将我的Delphi代码从Delphi6转换为Delphi 10.2(unicode字符串)时我遇到了一些函数问题需要PAnsiChar来填充Char数组,但是传递的指针是PChar,在非Unicode Delphi中我不会得到任何错误

but i will get error in new Delphi example 但我会在新的Delphi示例中遇到错误

procedure TswSocket.SetLocalHost(AHost : AnsiString);
var
  HostIpAddress : TIPAddress;
  Buffer: PChar;

  begin
  Buffer:=StrAlloc(Length(AHost)+1);
  strpcopy(Buffer,AHost);
  Buffer[Length(AHost)]:=#0;
  bLocalHost:=StrAlloc(MAXGETHOSTSTRUCT);
  hGetLocalHost:=WSAAsyncGetHostByName(Self.hWindow,WM_SOCKETGETHOSTBYNAME,Buffer,bLocalHost,MAXGETHOSTSTRUCT); // Buffer,bLocalHost must be PAnsiChar instead of PChar

WSAAsyncGetHostByName will fill bLocalHost WSAAsyncGetHostByName将填充bLocalHost

to fix the error i used 修复我使用的错误

procedure TswSocket.SetLocalHost(AHost : AnsiString);
var
  HostIpAddress : TIPAddress;
  Buffer,temp_Buffer : PAnsiChar;

  begin
  Buffer:=AnsiStrAlloc(Length(AHost)+1);
  strpcopy(Buffer,AHost);
  Buffer[Length(AHost)]:=#0;

  temp_Buffer:=AnsiStrAlloc(MAXGETHOSTSTRUCT);
  bLocalHost:=StrAlloc(MAXGETHOSTSTRUCT);
  hGetLocalHost:=WSAAsyncGetHostByName(Self.hWindow,WM_SOCKETGETHOSTBYNAME,Buffer,temp_Buffer,MAXGETHOSTSTRUCT); // no error : Buffer,temp_Buffer is PAnsiChar 

now the result in temp_Buffer( Array of AnsiChar) but we need the result in bLocalHost (array of Char) 现在结果是temp_Buffer(AnsiChar数组),但是我们需要bLocalHost中的结果(Char数组)

is there any function like this copy(PAnsiChar,PChar,size) copy content of PAnsiChar to PChar like array without lost data due the Unicode ,so if temp_Buffer ='some data' then bLocalHost must be ='some data' ? 是否有任何函数,如此复制(PAnsiChar,PChar,大小)PAnsiChar复制内容到PChar像阵列没有因Unicode而丢失数据,所以如果temp_Buffer ='某些数据'那么bLocalHost必须='某些数据'?

There is no function to assign the content of an AnsiChar array to a (Wide)Char array without converting the data. 没有将AnsiChar阵列的内容分配给(Wide)Char阵列而不转换数据的功能。 Use the Win32 API's MultiByteToWideChar() function, or the RTL's UnicodeFromLocaleChars() function, for that purpose. 为此,请使用Win32 API的MultiByteToWideChar()函数或RTL的UnicodeFromLocaleChars()函数。

Otherwise, you can change your bLocalHost variable to be an AnsiChar array. 否则,您可以将bLocalHost变量更改为AnsiChar数组。 Or, you can change it to be a (Unicode)String and let the RTL convert the data for you when assigning a null-terminated (P)AnsiChar directly to it. 或者,您可以将其更改为(Unicode)String并让RTL在为其分配空终止(P)AnsiChar时为您转换数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM