Click here to Skip to main content
11,585,177 members (70,069 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI
Hello,
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; 
	} 
	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':
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);

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 Frown | :( Please, help!
Posted 15-Nov-11 9:23am

1 solution

Rate this: bad
good
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;
  Permalink  
Comments
Bubbles85 at 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
0 OriginalGriff 1,748
1 Sergey Alexandrovich Kryukov 1,387
2 Abhinav S 746
3 Suvendu Shekhar Giri 713
4 Dave Kreskowiak 687


Advertise | Privacy | Mobile
Web04 | 2.8.150707.1 | Last Updated 15 Nov 2011
Copyright © CodeProject, 1999-2015
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