Click here to Skip to main content
12,885,268 members (40,447 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ C++/CLI
I'm having a problem with array and Byte type.

I've got two arrays of bytes:
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.

System::Byte ConvertFromU2(System::Byte u2)
        System::Byte result;
	if ((u2 & 0x80) == 0 )		
		result = 1.0 * u2; 
		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':
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:
fake_samples-&gt;SetValue(0x00, i);

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 15-Nov-11 9:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 15 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100