将 TByteDynArray(字节数组)复制到字符串。

huangapple go评论60阅读模式
英文:

Copy TByteDynArray (array of byte) to string

问题

我无法翻译代码部分,以下是已翻译的内容:

"How can I copy contents of a TByteDynArray variable to a string variable or even better, to a TMemoryStream?"

Remy,感谢你的回答。

嗯,我无法让它工作。
我正在尝试这样做:
obtReferenciaPagamentoResponse.pdf 是一个 TByteDynArray(字节数组),通过 WebService 调用传输,该 WebService 在 XSD 中被引用为 xsd:base64Binary。

procedure saveFile;
var
  LInput, LOutput: TMemoryStream;
  Id: Integer;
  Buff: AnsiString;
  //Buff: String;
begin
  LInput := TMemoryStream.Create;
  LOutput := TMemoryStream.Create;

  // 也尝试过这样
  //SetLength(Buff, Length(obtReferenciaPagamentoResponse.pdf));
  //Move(obtReferenciaPagamentoResponse.pdf[0], Buff[1], Length(obtReferenciaPagamentoResponse.pdf));

  // 尝试其他字符集
  Buff := TEncoding.Ansi.GetString(obtReferenciaPagamentoResponse.pdf);

  LInput.Write(Buff[1], Length(Buff) * SizeOf(Buff[1]));
  LInput.Position := 0;

  TNetEncoding.Base64.Decode(LInput, LOutput);
  LOutput.Position := 0;
  LOutput.SaveToFile(SaveDialog2.FileName);

  LInput.Free;
  LOutput.Free;
end;

但是 PDF 文件保存不完整,我猜测可能是因为在打开时总是损坏。我做错了什么?

英文:

How can I copy contents of a TByteDynArray variable to a string variable or even better, to a TMemoryStream?

Remy, thanks for your answer.

Well, I can't get it to work.
I'm doing this:
obtReferenciaPagamentoResponse.pdf is a TByteDynArray (array of byte) that comes throught a WebService call, that is referenced on the XSD like xsd:base64Binary.

procedure saveFile;
var
  LInput, LOutput: TMemoryStream;
  Id: Integer;
  Buff: AnsiString;
  //Buff: String;
begin
  LInput := TMemoryStream.Create;
  LOutput := TMemoryStream.Create;

  // Tried like this also
  //SetLength(Buff, Length(obtReferenciaPagamentoResponse.pdf));
  //Move(obtReferenciaPagamentoResponse.pdf[0], Buff[1],       Length(obtReferenciaPagamentoResponse.pdf));

  // Tried other charsets
  Buff := TEncoding.Ansi.GetString(obtReferenciaPagamentoResponse.pdf);

  LInput.Write(Buff[1], Length(Buff) * SizeOf(Buff[1]));
  LInput.Position := 0;

  TNetEncoding.Base64.Decode(LInput, LOutput);
  LOutput.Position := 0;
  LOutput.SaveToFile(SaveDialog2.FileName);

  LInput.Free;
  LOutput.Free;
end;

But the PDF file is saved incompleted, I guess, because is always corrupted on open.
What am I doing wrong?

答案1

得分: 1

String 自 2009 年以来是 UnicodeString 的别名。由于 UnicodeString 中的字符现在以 UTF-16 编码,因此将原始字节复制到 (Unicode)String 中除非字节也以 UTF-16 编码,否则是没有意义的。在这种情况下,您可以简单地使用 SetLength() 来分配 String 的长度为适当数量的 Char,然后使用 Move() 将原始字节移动到 String 的分配内存中。否则,使用 TEncoding.GetString() 来使用适当的字符集将字节解码为 UTF-16 String

至于 TMemoryStream,它具有用于将原始字节写入流的 Write() 方法。只需将其 Position 属性设置为所需的偏移量,然后写入字节。

英文:

String is an alias for UnicodeString since 2009. As UnicodeString characters are now encoded in UTF-16, it does not make sense to copy raw bytes into a (Unicode)String unless the bytes are also encoded in UTF-16. In that case, you can simply use SetLength() to allocate the String's length to the appropriate number of Chars and then Move() the raw bytes into the String's allocated memory. Otherwise, use TEncoding.GetString() instead to decode the bytes into a UTF-16 String using the appropriate charset.

As for TMemoyStream, it has a Write() method for writing raw bytes into the stream. Simply set its Position property to the desired offset and then write the bytes.

huangapple
  • 本文由 发表于 2023年2月14日 03:35:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定