14,975,411 members
1.00/5 (1 vote)
See more:
C#
```int temp_var;
int temp_var1;
int temp_var3;
int temp_var4;
int temp_var5;
int temp_var6;
int result1;
int i;
int j;
int k;

temp_var = MCUData[0];
temp_var1 = MCUData[1];
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));
int f = 0;
for (i = 0; i <= 29; i++)
{
f++;
if (f == 2)
{
f = 0;
i++;
}

for (j = 1; j <= 2; j++)
{

for (k = 1; k <= 122; k++)
{

dataGridView1.Rows[i].Cells[j].Value =result1[k];
}
}
}```

What I have tried:

i want to convert result into result[]
Posted
Updated 22-Jan-19 0:02am
v3
CPallini 22-Jan-19 5:57am

Your code takes awkward paths to simple targets. What are, exactly, your requirements?

## Solution 1

Ok, I'm guessing you want to use array of int in the code? the `int result1`; should be `int[] result1`. Maybe you want the code to create array of size 122? I'm also assuming result = result1.

C#
`int[] result1= new int[((temp_var3) * 1000) + ((temp_var4) * 100) + ((temp_var5) * 10) + ((temp_var6))];`

Now, this code `result1[k]` should work. By the way, where are the code that populating the `result1` array? Maybe you didn't post it?
v3

## Solution 2

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;`

## Solution 3

C#
```for (k = 1; k <= 122; k++)
{

dataGridView1.Rows[i].Cells[j].Value =result1[k];
}```

That loop makes no sense. Even if you do change it you are repeatedly storing values in the same cell of your datagridview. So only the last one will actually be there at the end of the loop. Also in the preceding code you increment the variable `f` a couple of times for no apparent reason.