2D数组到字节数组 [英] 2D array to byte array
问题描述
我想执行以下操作:
1.在Gridview中检索产品信息。
2.从中读取数据Gridview转换为2D数组整数类型。
3.将2D数组转换为Byte [],或使用Buffer.BlockCopy方法以便通过网络传输。
4.接收字节数组。
5.将其转换为2D数组并填充Gridview。
我尝试过:
I want to do the following:
1. Retrieve the product information in the Gridview.
2. Reading the data from Gridview into 2D array "integer type".
3. Convert 2D array to be Byte[], or using Buffer.BlockCopy Method in order to transmitted over the network.
4. Receive the Byte array.
5. Convert it to 2D array and fill the Gridview.
What I have tried:
<pre>
This is my code
<pre>, var select = "SELECT Pro_ID, Price, Category_ID FROM Products where Empl_ID = 1";
var c = new SqlConnection();
var dataAdapter = new SqlDataAdapter(select, c);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView.DataSource = ds.Tables[0];
dataGridView.Columns[0].HeaderText = "Items";
dataGridView.Columns[1].HeaderText = "Price";
dataGridView.Columns[2].HeaderText = "Quantity";
// reading data from gridview into 2D array
string[,] DataValue = new string[dataGridView.Rows.Count, dataGridView.Columns.Count];
foreach (DataGridViewRow row in dataGridView.Rows)
{
foreach (DataGridViewColumn col in dataGridView.Columns)
{
DataValue[row.Index, col.Index] = dataGridView.Rows[row.Index].Cells[col.Index].Value.ToString();
}
}
我能够实施第一点和第二点,但其余的,我需要一些指导来实现。
I was able to implement the first and second points, but the rest, I need some guidance to implement.
推荐答案
您好会员12669478
,
基本上你要做的是通过网络发送网格值(2D字符串)作为byte []并将其重建为相同的2D数组。您将所有值视为string
。
这可能有点棘手,因为接收器端需要知道数组的尺寸,以及字节数组中各个元素的标记。我建议你可以使用管道(|)作为分隔符(我假设文本中没有管道;文本中的管道很不可能)。
之后填充DataValue
可以添加以下行:
HiMember 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 asstring
.
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 theDataValue
you can add these lines:
const int PIPEDELIMETER = 124;
List<byte> ListOfBytes = new List<byte>();
// Adding metadata - dimension of the first index. Plus the delimiter.
ListOfBytes.Add(Convert.ToByte(DataValue.GetUpperBound(0) + 1));
ListOfBytes.Add(PIPEDELIMETER);
// Adding metadata - dimension of the second index. Plus the delimiter.
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++)
{
// Add the string as bytes in ListOfBytes.
ListOfBytes.AddRange(Encoding.ASCII.GetBytes(DataValue[i, j]));
// Add the delimiter
ListOfBytes.Add(PIPEDELIMETER);
}
}
byte[] Bytes = ListOfBytes.ToArray(); // Convert 2D string array to byte array.
说 DataValue
具有维度[3,2],前两个元素是:
Say DataValue
has dimensions [3, 2], and the first two elements are:
DataValue[0, 0] = "Issac";
DataValue[0, 1] = "Newton";
然后 Bytes
数组具有以下签名:
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
.............
.............
代码为在接收端结束:
/*Reconstruction*/
int FirstDimensionCount = Bytes[0]; // First dimension from metadata.
int SecondDimensionCount = Bytes[2]; // Second dimension from metadata.
// String array to hold the reconstructed data.
string[,] DataValueConvertedBack = new string[FirstDimensionCount, SecondDimensionCount];
int BytesCount = 4; // Data starts from the 4-th index.
// Get the entire bytes array into character array.
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++]); // Build the string character by character.
DataValueConvertedBack[i, j] = SBuilder.ToString(); // Add the string to the specified string array index.
BytesCount++; // Increase the bytes pointer as it is currently pointing to the pipe
SBuilder.Clear(); // Clear out for the next operation.
}
}
// DataValueConvertedBack (receiver end) = DataValue (sending end) at this point.
这篇关于2D数组到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!