字节数组,SetValue问题 [英] array of bytes, SetValue problem
问题描述
你好,
我在数组和字节类型方面遇到问题.
我有两个字节数组:
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);
然后是两个函数(方法?).
此代码将U2代码中的数字转换为整数.
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;
}
这个将一些数字放入"fake_samples"数组中,然后从"fake_samples"中获取数字并将它们存储在数组"sampes"中:
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->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!
推荐答案
该值应隐式装在Object ^上,这样就可以了...也许你只需要一个演员...
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);
您真的需要使用SetValue吗?
Do you really need to use SetValue?
fake_samples[i] = (Byte)0x00;
这篇关于字节数组,SetValue问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!