I receive a string, which is displayed as '{'#0'S'#0'a'#0'm'#0'p'#0'l'#0'e'#0'-'#0'M'#0'e'#0's'#0's'#0'a'#0'g'#0'e'#0'}'#0
in the debugger.
我收到一个字符串,显示为“{”# 0 ' 0 ' a ' # 0我' # 0 # 'p ' # 0孩子' # 0说的“# 0”——“# 0我' # 0本部的# 0 ' # 0 a ' # 0 # 0石头“# 0本部”# 0 ' } ' # 0在调试器中。
I need to print it out in the debug output (OutputDebugString
).
我需要在调试输出(OutputDebugString)中打印出来。
When I run OutputDebugString(PChar(mymsg))
, only the first character of the received string is displayed (probably because of the #0
end-of-string marker).
当我运行OutputDebugString(PChar(mymsg))时,只显示接收到的字符串的第一个字符(可能是由于#0结尾的标记)。
How can I convert that string into something OutputDebugString
can work with?
如何将该字符串转换为OutputDebugString可以使用的东西?
Update 1: Here's the code. I want to print the contents of the variable RxBufStr
.
更新1:这是代码。我要打印变量RxBufStr的内容。
procedure ReceivingThread.OnExecute(AContext : TIdContext);
var
RxBufStr: String;
begin
with AContext.Connection.IOHandler do
begin
CheckForDataOnSource(10);
if not InputBufferIsEmpty then
begin
RxBufStr := InputBuffer.Extract();
end;
end;
end;
2 个解决方案
#1
5
The data you have shown in the question looks like UTF-16 encoded data rather than UTF-8. However, since you are using a Unicode aware Delphi, and a string
data type, clearly there has been an encoding mismatch. Your string variable appears to be double UTF-16 encoded if you can see what I mean!
您在问题中显示的数据看起来像UTF-16编码的数据,而不是UTF-8。但是,由于您使用的是Unicode感知的Delphi和字符串数据类型,显然存在编码不匹配。您的字符串变量似乎是双UTF-16编码的,如果您能明白我的意思!
It would appear therefore that InputBuffer.Extract
is assuming that the data is transmitted using ANSI or UTF-8. In other words, an 8-bit encoding. But in fact the data is transmitted as UTF-16.
因此会出现InputBuffer。提取是假设数据是使用ANSI或UTF-8传输的。换句话说,一个8位编码。但事实上,数据是通过UTF-16传输的。
To solve the problem you need to align the reading of the buffer with the transmission of the buffer. You need to make sure that both sides use the same encoding. UTF-8 would be a good choice.
为了解决这个问题,您需要将缓冲区的读取与缓冲区的传输进行对齐。您需要确保双方都使用相同的编码。UTF-8是一个不错的选择。
If the data in the buffer is UTF-16, then you can extract it with
如果缓冲区中的数据是UTF-16,那么您可以将其提取。
RxBufStr := InputBuffer.Extract(-1, TIdTextEncoding.Unicode);
If you switch to UTF-8 then extract it with
如果你切换到UTF-8,然后把它提取出来。
RxBufStr := InputBuffer.Extract(-1, TIdTextEncoding.UTF8);
#2
0
With
与
RxBufStr := InputBuffer.Extract();
the code does not specifiy a terminator or a data size, so it may happen that the client receives only a part of the sent data.
代码不指定终止符或数据大小,因此可能会发生客户机只接收到发送的数据的一部分。
You can read the data with a given (known) length into a TIdBytes array and then convert it to a string using the correct encoding.
您可以将给定(已知)长度的数据读入到一个TIdBytes数组中,然后使用正确的编码将其转换为字符串。
One way to do it is
一种方法是。
TEncoding.Unicode.GetString( MyByteArray );
(found here)
(发现)
#1
5
The data you have shown in the question looks like UTF-16 encoded data rather than UTF-8. However, since you are using a Unicode aware Delphi, and a string
data type, clearly there has been an encoding mismatch. Your string variable appears to be double UTF-16 encoded if you can see what I mean!
您在问题中显示的数据看起来像UTF-16编码的数据,而不是UTF-8。但是,由于您使用的是Unicode感知的Delphi和字符串数据类型,显然存在编码不匹配。您的字符串变量似乎是双UTF-16编码的,如果您能明白我的意思!
It would appear therefore that InputBuffer.Extract
is assuming that the data is transmitted using ANSI or UTF-8. In other words, an 8-bit encoding. But in fact the data is transmitted as UTF-16.
因此会出现InputBuffer。提取是假设数据是使用ANSI或UTF-8传输的。换句话说,一个8位编码。但事实上,数据是通过UTF-16传输的。
To solve the problem you need to align the reading of the buffer with the transmission of the buffer. You need to make sure that both sides use the same encoding. UTF-8 would be a good choice.
为了解决这个问题,您需要将缓冲区的读取与缓冲区的传输进行对齐。您需要确保双方都使用相同的编码。UTF-8是一个不错的选择。
If the data in the buffer is UTF-16, then you can extract it with
如果缓冲区中的数据是UTF-16,那么您可以将其提取。
RxBufStr := InputBuffer.Extract(-1, TIdTextEncoding.Unicode);
If you switch to UTF-8 then extract it with
如果你切换到UTF-8,然后把它提取出来。
RxBufStr := InputBuffer.Extract(-1, TIdTextEncoding.UTF8);
#2
0
With
与
RxBufStr := InputBuffer.Extract();
the code does not specifiy a terminator or a data size, so it may happen that the client receives only a part of the sent data.
代码不指定终止符或数据大小,因此可能会发生客户机只接收到发送的数据的一部分。
You can read the data with a given (known) length into a TIdBytes array and then convert it to a string using the correct encoding.
您可以将给定(已知)长度的数据读入到一个TIdBytes数组中,然后使用正确的编码将其转换为字符串。
One way to do it is
一种方法是。
TEncoding.Unicode.GetString( MyByteArray );
(found here)
(发现)