T
taskswap
I'm converting an application that relies heavily on a binary network
protocol. Within this protocol are a lot of byte[] arrays of character
data, like:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public unsafe struct MsgAddEntry {
public byte MsgType;
public uint Tag;
public fixed byte ID[10];
public fixed byte Val1[20];
public fixed byte Opt1[20];
public fixed byte Val2[20];
public fixed byte Opt2[20];
}
I know, it's archaic, but it works very well and has the least network
impact of any method. I've been able to work with this data using the
above method so far, but I'm having one problem. Whenever I convert
to/from these byte fields, I have to hard-code length strings. That is,
IN ADDITION to the 20 it's hard-coded to above, I have to hard-code the
length in my code when I do the conversion. The compiler won't let me
use sizeof() or .Length on the array.
This is obviously pretty inconvenient, especially since there are
hundreds of messages and almost a thousand different fields. The fields
change once every few months, so having to change two locations is
almost unmaintainable. Is there a better way to do this?
While on this topic, is there a better way to convert these byte arrays
to strings? I can't use Encoding directly because these aren't "real"
managed byte arrays. So far I've been marshaling/copying the data into
a managed byte[] and then converting from there. This works, but it's
two memory copies of the data for a single conversion - not very
efficient. If it helps, the encoding will never change; it's a legacy
system, so it's good old one-byte-per-char ASCII.
Regards,
Chad
protocol. Within this protocol are a lot of byte[] arrays of character
data, like:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public unsafe struct MsgAddEntry {
public byte MsgType;
public uint Tag;
public fixed byte ID[10];
public fixed byte Val1[20];
public fixed byte Opt1[20];
public fixed byte Val2[20];
public fixed byte Opt2[20];
}
I know, it's archaic, but it works very well and has the least network
impact of any method. I've been able to work with this data using the
above method so far, but I'm having one problem. Whenever I convert
to/from these byte fields, I have to hard-code length strings. That is,
IN ADDITION to the 20 it's hard-coded to above, I have to hard-code the
length in my code when I do the conversion. The compiler won't let me
use sizeof() or .Length on the array.
This is obviously pretty inconvenient, especially since there are
hundreds of messages and almost a thousand different fields. The fields
change once every few months, so having to change two locations is
almost unmaintainable. Is there a better way to do this?
While on this topic, is there a better way to convert these byte arrays
to strings? I can't use Encoding directly because these aren't "real"
managed byte arrays. So far I've been marshaling/copying the data into
a managed byte[] and then converting from there. This works, but it's
two memory copies of the data for a single conversion - not very
efficient. If it helps, the encoding will never change; it's a legacy
system, so it's good old one-byte-per-char ASCII.
Regards,
Chad