The purpose of your code is not very clear:
- Indices i, j, k are iterated to some values whose meaning is unknown (29, 2, 122)
- Index k is iterated towards 122, that will never be a valid index within result1, unless it is an array, but the actual logic of the code can't populate enough of it, even applying Bryian Tan solution.
- There are instructions that extract hexadecimal digits from two numbers, but later merged together as they were not hexadecimal.
- Later the code try to access by index to an integer number.
You should explain better what it is supposed/expected to do.
- Consider replacing the magic numbers 29 and 122 with local named constants.
- Consider replacing the
i loop and
f variable with a simpler loop increasing
i by 2 units.
int f = 0;
for (i = 0; i <= 29; i++)
{
f++;
if (f == 2)
{
f = 0;
i++;
}
would become
for (i = 0; i <= 29; i += 2)
{
Based on code populating
temp_var3,
temp_var4,
temp_var5 and
temp_var6 variables, I assume that you want to display the contents of
MCUData[0] and
MCUData[1] in hexadecimal form.
Though, the value assigned to
result1 will make those values overlap due to 10-based factors instead of 16-based factors.
I have some hypothesis:
1- You want hexadecimal representation of
MCUData[0] and
MCUData[1].
2- You want the remainder of the division of
result1 by
k.
3- You want some other result that is not available based only on the provided code.
Case 1:
Change
temp_var3 = (temp_var & 0xF0) >> 4;
temp_var4 = (temp_var & 0x0F);
temp_var5 = (temp_var1 & 0xF0) >> 4;
temp_var6 = (temp_var1 & 0x0F);
result1 = ((temp_var3) * 1000) + ((temp_var4) * 100) + ((temp_var5) * 10) + ((temp_var6));
with
result1 = string.Format("{0:X2}{1:X2}", temp_var, temp_var1);
or
result1 = temp_var.ToString("X2") + temp_var1.ToString("X2");
Case 2:
Change
dataGridView1.Rows[i].Cells[j].Value =result1[k];
to
dataGridView1.Rows[i].Cells[j].Value =result1 % k;