Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm having a problem with array and Byte type.

I've got two arrays of bytes:
C++
static Array^ samples = Array::CreateInstance(System::Byte::typeid,100);
static Array^ fake_samples = Array::CreateInstance(System::Byte::typeid,100);


Then two functions (methods?).
This one converts number in U2 code to integer.

C++
System::Byte ConvertFromU2(System::Byte u2)
{
        System::Byte result;

	if ((u2 & 0x80) == 0 )		
	{
		result = 1.0 * u2; 
	} 
	else
	{
		u2 = u2 ^ 0xFF;
		u2 = u2 + 1;
		result = -1.0 * u2;
	}
	return result;
}


This one puts some numbers in an array 'fake_samples', then takes numbers from an 'fake_samples' and stores them in an array 'sampes':
C++
void CollectSamples(void)
{
	for (int i=25; i<100; i++)
	{
		fake_samples->SetValue(0x00, i);
	}

	for (int i=0; i<100; i++)
	{
	  System::Byte one_byte=(Convert::ToByte(fake_samples->GetValue(i)));
	  System::Byte converted_byte;
	  converted_byte = ConvertFromU2(one_byte);
	  samples->SetValue(converted_byte, i);
	}
}


This is done in such a complicated way due to some future plans concerning this program.
The code compiles, but when running I got an exception in line:
C++
fake_samples-&gt;SetValue(0x00, i);


Exception:
Cannot widen from source type to target type either because the source type is a not a primitive type or the conversion cannot be accomplished.

I don't know what to do :( Please, help!
Posted

1 solution

The value should be implicitly boxed to an Object^ so that should be ok...maybe you just need a cast...
fake_samples->SetValue((Byte)0x00, i);


Do you really need to use SetValue?
fake_samples[i] = (Byte)0x00;
 
Share this answer
 
Comments
Bubbles85 15-Nov-11 16:25pm    
Cast works perfectly! Thank you very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900