Hi
Member 12669478
,
Basically what you are trying to do is send the grid values (2D string) over the network as byte[] and reconstruct it to the same 2D array. You are treating all the values as
string
.
This might be a bit tricky as the receiver end needs to know the dimensions of the array, and a mark for individual elements in the byte array. I suggest you can use pipe (|) as a delimiter (I am assuming there is no pipe in the texts; having pipe in texts is highly unlikely).
After you populate the
DataValue
you can add these lines:
const int PIPEDELIMETER = 124;
List<byte> ListOfBytes = new List<byte>();
ListOfBytes.Add(Convert.ToByte(DataValue.GetUpperBound(0) + 1));
ListOfBytes.Add(PIPEDELIMETER);
ListOfBytes.Add(Convert.ToByte(DataValue.GetUpperBound(1) + 1));
ListOfBytes.Add(PIPEDELIMETER);
for (int i = 0; i <= DataValue.GetUpperBound(0); i++)
{
for (int j = 0; j <= DataValue.GetUpperBound(1); j++)
{
ListOfBytes.AddRange(Encoding.ASCII.GetBytes(DataValue[i, j]));
ListOfBytes.Add(PIPEDELIMETER);
}
}
byte[] Bytes = ListOfBytes.ToArray();
Say
DataValue
has dimensions [3, 2], and the first two elements are:
DataValue[0, 0] = "Issac";
DataValue[0, 1] = "Newton";
Then the
Bytes
array has the following signature:
[0]: 3
[1]: 124
[2]: 2
[3]: 124
[4]: 73
[5]: 115
[6]: 115
[7]: 97
[8]: 99
[9]: 124
[10]: 78
[11]: 101
[12]: 119
[13]: 116
[14]: 111
[15]: 110
[16]: 124
.............
.............
Code as follows at the receiver end:
int FirstDimensionCount = Bytes[0];
int SecondDimensionCount = Bytes[2];
string[,] DataValueConvertedBack = new string[FirstDimensionCount, SecondDimensionCount];
int BytesCount = 4;
char[] CharsFromBytes = Encoding.UTF8.GetString(Bytes).ToCharArray();
StringBuilder SBuilder = new StringBuilder();
for (int i = 0; i < FirstDimensionCount; i++)
{
for (int j = 0; j < SecondDimensionCount; j++)
{
while (CharsFromBytes[BytesCount] != '|')
SBuilder.Append(CharsFromBytes[BytesCount++]);
DataValueConvertedBack[i, j] = SBuilder.ToString();
BytesCount++;
SBuilder.Clear();
}
}