将byte []转换为类时出现问题 [英] Problem casting a byte[] to a class

查看:67
本文介绍了将byte []转换为类时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中运行时执行static_cast,据我了解,

" as"是要使用的关键字。不幸的是,编译器试图将编译时间转换为
。见下文。


/ * TestA.cs * /

名称空间TEST {

class TestA {

public ushort val1;

public ushort val2;

public ushort val3;

}

}


/ *主程序* /

名称空间TEST {

class Program {

static void Main( string [] args){

TestA test = null;

byte [] testArray = new byte [6];

for(int i = 0; i< testArray.Length; i ++){

testArray [i] =(byte)(i + 1);

}

//尝试做C ++ - 样式static_cast

//编译错误:

//无法将类型''byte []''转换为''TEST。 TestA''

//通过内置转换

test = testArray作为TestA;

}

}

}


我做错了什么?

I''m trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.

/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
}
}

/* Main Program */
namespace TEST {
class Program {
static void Main(string[] args) {
TestA test = null;
byte[] testArray = new byte[6];
for (int i = 0; i < testArray.Length; i++) {
testArray[i] = (byte)(i + 1);
}
// Trying to do a C++-style static_cast
// Compilation error:
// Cannot convert type ''byte[]'' to ''TEST.TestA''
// via a built-in conversion
test = testArray as TestA;
}
}
}

What am I doing wrong?

推荐答案

你的假设数据是按顺序存储的,并且
类,这两个假设都是不正确的。


通常你可以序列化你的类并消毒,但如果数据需要

在我已经发现需要多次的紧凑字节数组

需要手动解析数据。我认为你可以重载

序列化功能,但我发现下面的内容比较简单。


类TestA {

public ushort val1 ;

public ushort val2;

public ushort val3;


public TestA(byte [] data)

{

val1 = BitConverter.ToUInt16(0);

val2 = BitConverter.ToUInt16(2);

val3 = BitConverter。 ToUInt16(4);

}

public ToArray

{

get {

MemoryStream ms = new MemoryStream();

ms.Wirte(BitConverter.GetBytes(val1,0,2);

ms.Wirte(BitConverter.GetBytes(val2, 0,2);

ms.Wirte(BitConverter.GetBytes(val3,0,2);

ms.Capacity = ms.Length;

返回ms.ToArray();

}

}

}


问候,

John


" OB" fu ****** @ bellsouth.netwrote in message

news :12 ************* @ corp.supernews.com ...
Your assuming the data is sequentially stored and no other data exist in the
class, both assumptions are incorrect.

Normally you can serialize your class and desterilized but if the data needs
to be in a compact byte array which I have found need for several times you
will need to parse the data manually. I think you can overload the
serialization function but I have found the below simpler.

class TestA {
public ushort val1;
public ushort val2;
public ushort val3;

public TestA(byte[] data)
{
val1 = BitConverter.ToUInt16(0);
val2 = BitConverter.ToUInt16(2);
val3 = BitConverter.ToUInt16(4);
}
public ToArray
{
get {
MemoryStream ms = new MemoryStream();
ms.Wirte(BitConverter.GetBytes(val1, 0, 2);
ms.Wirte(BitConverter.GetBytes(val2, 0, 2);
ms.Wirte(BitConverter.GetBytes(val3, 0, 2);
ms.Capacity = ms.Length;
return ms.ToArray();
}
}
}

Regards,
John

"O.B." <fu******@bellsouth.netwrote in message
news:12*************@corp.supernews.com...

我正在尝试在C#中运行时执行static_cast,据我所知,

" as"是要使用的关键字。不幸的是,编译器试图将编译时间转换为
。见下文。


/ * TestA.cs * /

名称空间TEST {

class TestA {

public ushort val1;

public ushort val2;

public ushort val3;

}

}


/ *主程序* /

名称空间TEST {

class Program {

static void Main( string [] args){

TestA test = null;

byte [] testArray = new byte [6];

for(int i = 0; i< testArray.Length; i ++){

testArray [i] =(byte)(i + 1);

}

//尝试做C ++ - 样式static_cast

//编译错误:

//无法将类型''byte []''转换为''TEST。 TestA''

//通过内置转换

test = testArray作为TestA;

}

}

}


我做错了什么?
I''m trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.

/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
}
}

/* Main Program */
namespace TEST {
class Program {
static void Main(string[] args) {
TestA test = null;
byte[] testArray = new byte[6];
for (int i = 0; i < testArray.Length; i++) {
testArray[i] = (byte)(i + 1);
}
// Trying to do a C++-style static_cast
// Compilation error:
// Cannot convert type ''byte[]'' to ''TEST.TestA''
// via a built-in conversion
test = testArray as TestA;
}
}
}

What am I doing wrong?



O.B。 < fu ****** @ bellsouth.netwrote:
O.B. <fu******@bellsouth.netwrote:

我正在尝试在C#中运行时执行static_cast,据我了解,

" as"是要使用的关键字。不幸的是,编译器试图将编译时间转换为
。见下文。
I''m trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.



你不能用C#做这件事(谢天谢地,IMO)。您应该在序列化时查看

,或者向TestA添加一个方法,将一个字节数组转换为一个TestA实例。

(或一个流)。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件

You can''t do that in C# (thank goodness, IMO). You should either look
at serialization, or add a method to TestA to convert an array of bytes
(or a stream) into an instance of TestA.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


哦,顺便说一句,如果你转换为一个结构并手动设置偏移你

然后可以做一个内存副本来完成你做的事情在C.


有点像这样,不要经常这样做一点点模糊。


[StructLayout( LayoutKind.Explicit)]

public struct TestA

{

[FieldOffset(0)] public ushort Val1;

[FieldOffset(2)] public ushort Val2;

[FieldOffs et(4)] public ushort Val2;

}


Marshal.Copy(Data,0,IntPtr< TestA>,6);


问候,

John


" OB" < fu ****** @ bellsouth.netwrote in message

news:12 ************* @ corp.supernews.com ...
Oh by the way if you convert to a structure and set the offsets manually you
can then do a memory copy to accomplish what you did in C.

Sort of like this, don''t do it as often so a little fuzzer on how.

[StructLayout(LayoutKind.Explicit)]
public struct TestA
{
[FieldOffset(0)] public ushort Val1;
[FieldOffset(2)] public ushort Val2;
[FieldOffset(4)] public ushort Val2;
}

Marshal.Copy(Data, 0, IntPtr <TestA>, 6);

Regards,
John

"O.B." <fu******@bellsouth.netwrote in message
news:12*************@corp.supernews.com...

我正在尝试在C#中运行时执行static_cast,据我了解,

" as"是要使用的关键字。不幸的是,编译器试图将编译时间转换为
。见下文。


/ * TestA.cs * /

名称空间TEST {

class TestA {

public ushort val1;

public ushort val2;

public ushort val3;

}

}


/ *主程序* /

名称空间TEST {

class Program {

static void Main( string [] args){

TestA test = null;

byte [] testArray = new byte [6];

for(int i = 0; i< testArray.Length; i ++){

testArray [i] =(byte)(i + 1);

}

//尝试做C ++ - 样式static_cast

//编译错误:

//无法将类型''byte []''转换为''TEST。 TestA''

//通过内置转换

test = testArray作为TestA;

}

}

}


我做错了什么?
I''m trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.

/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
}
}

/* Main Program */
namespace TEST {
class Program {
static void Main(string[] args) {
TestA test = null;
byte[] testArray = new byte[6];
for (int i = 0; i < testArray.Length; i++) {
testArray[i] = (byte)(i + 1);
}
// Trying to do a C++-style static_cast
// Compilation error:
// Cannot convert type ''byte[]'' to ''TEST.TestA''
// via a built-in conversion
test = testArray as TestA;
}
}
}

What am I doing wrong?



这篇关于将byte []转换为类时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆