将 RepeatedField<ByteString> 转换为 Byte[]
Convert RepeatedField<ByteString> to Byte[]
在 Docker 中定义了一个 gRPC Python 服务。
我的 PROTO 文件中的服务:
rpc ClientCommand(ClientRequest) returns (ClientResponse){}
“ClientResponse”的定义:
message ClientResponse{
int32 request_id = 1;
repeated int32 prediction_status = 2;
repeated string prediction_info = 3;
repeated string prediction_error = 4;
repeated string prediction_result_name = 5;
repeated bytes prediction_result = 6;
repeated bytes prediction_config = 7;
repeated bytes prediction_log = 8;
}
在客户端,我想捕获重复的字节并将它们转换成一个文件(我知道它更适合流,但目前我想这样做)。
重复的字符串和整数我可以完美转换成List --> OK
我想将重复的字节转换为 Byte[]。他们的类型:Google.ProtoBuf.Collections.RepeatedField.
起初似乎无法将此类型转换为 Byte[]。有人可以帮我解决这个问题吗?我的临时解决方案:
byte[] test = new byte[100];
Google.Protobuf.ByteString[] test2 = new Google.Protobuf.ByteString[100];
response.PredictionResult.CopyTo(test2,0);
test2.CopyTo(test,0);
WriteFile(@"C:\programs\file.txt", test);
与此同时我想通了:
public void WriteFileResult(string fileName,Google.Protobuf.Collections.RepeatedField<ByteString> data)
{
byte[] bytes = new byte[data.Count];
ByteString[] dataByteString = new ByteString[data.Count];
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!path.EndsWith(@"\")) path += @"\";
if (File.Exists(Path.Combine(path, fileName)))
File.Delete(Path.Combine(path, fileName));
using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.CreateNew, FileAccess.Write))
{
data.CopyTo(dataByteString, 0);
bytes = dataByteString[0].ToByteArray();
fs.Write(bytes, 0, (int)bytes.Length);
_serviceResponseModel.Json = false;
//fs.Close()
}
}
在 Docker 中定义了一个 gRPC Python 服务。
我的 PROTO 文件中的服务:
rpc ClientCommand(ClientRequest) returns (ClientResponse){}
“ClientResponse”的定义:
message ClientResponse{
int32 request_id = 1;
repeated int32 prediction_status = 2;
repeated string prediction_info = 3;
repeated string prediction_error = 4;
repeated string prediction_result_name = 5;
repeated bytes prediction_result = 6;
repeated bytes prediction_config = 7;
repeated bytes prediction_log = 8;
}
在客户端,我想捕获重复的字节并将它们转换成一个文件(我知道它更适合流,但目前我想这样做)。
重复的字符串和整数我可以完美转换成List --> OK
我想将重复的字节转换为 Byte[]。他们的类型:Google.ProtoBuf.Collections.RepeatedField
起初似乎无法将此类型转换为 Byte[]。有人可以帮我解决这个问题吗?我的临时解决方案:
byte[] test = new byte[100];
Google.Protobuf.ByteString[] test2 = new Google.Protobuf.ByteString[100];
response.PredictionResult.CopyTo(test2,0);
test2.CopyTo(test,0);
WriteFile(@"C:\programs\file.txt", test);
与此同时我想通了:
public void WriteFileResult(string fileName,Google.Protobuf.Collections.RepeatedField<ByteString> data)
{
byte[] bytes = new byte[data.Count];
ByteString[] dataByteString = new ByteString[data.Count];
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!path.EndsWith(@"\")) path += @"\";
if (File.Exists(Path.Combine(path, fileName)))
File.Delete(Path.Combine(path, fileName));
using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.CreateNew, FileAccess.Write))
{
data.CopyTo(dataByteString, 0);
bytes = dataByteString[0].ToByteArray();
fs.Write(bytes, 0, (int)bytes.Length);
_serviceResponseModel.Json = false;
//fs.Close()
}
}