Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换为c++;字节数组到c的结构#_C#_C++_Struct - Fatal编程技术网

C# 转换为c++;字节数组到c的结构#

C# 转换为c++;字节数组到c的结构#,c#,c++,struct,C#,C++,Struct,我似乎不知道该怎么做。我在c++中有一小部分代码,我需要在c#中有同样的代码。以下是c++代码: struct Texel { unsigned char r, g, b, a; }; Texel mytexturedata[] = { {0x00, 0xFF, 0x00, 0xFF}, // green {0xFF, 0xFF, 0x00, 0xFF}, // yellow {0xFF, 0x00, 0x00, 0xFF}, // red }; 你是如何做到这一点的 谢

我似乎不知道该怎么做。我在
c++
中有一小部分代码,我需要在
c#
中有同样的代码。以下是
c++
代码:

struct Texel { unsigned char r, g, b, a; }; 
Texel mytexturedata[] = 
{
   {0x00, 0xFF, 0x00, 0xFF}, // green
   {0xFF, 0xFF, 0x00, 0xFF}, // yellow
   {0xFF, 0x00, 0x00, 0xFF}, // red
};
你是如何做到这一点的


谢谢

将结构转换为类

public class Texel
{
    public byte r { get; set; }
    public byte g { get; set; }
    public byte b { get; set; }
    public byte a { get; set; }
}
然后你可以这样做你的作业

Texel[] mytexturedata =
{
    new Texel() { r = 0x00, g = 0xFF, b = 0x00, a = 0xFF }, // Green
    new Texel() { r = 0xFF, g = 0xFF, b = 0x00, a = 0xFF }, // Yellow
    new Texel() { r = 0xFF, g = 0x00, b = 0x00, a = 0xFF }  // Red
};

将结构转换为类

public class Texel
{
    public byte r { get; set; }
    public byte g { get; set; }
    public byte b { get; set; }
    public byte a { get; set; }
}
然后你可以这样做你的作业

Texel[] mytexturedata =
{
    new Texel() { r = 0x00, g = 0xFF, b = 0x00, a = 0xFF }, // Green
    new Texel() { r = 0xFF, g = 0xFF, b = 0x00, a = 0xFF }, // Yellow
    new Texel() { r = 0xFF, g = 0x00, b = 0x00, a = 0xFF }  // Red
};

Texel[]mytexturedata={new Texel(){r=0x00,g=0xFF,b=0x00,a=0xFF},…}更改为
类Texel{byte r{get;set;}byte g{get;set;}byte b{get;set;}byte a{get;set;}
。顺便说一句,
unsigned char
对应于C#中的
byte
char
是16位宽(UTF-16)。我认为byte比C#中的unsigned char类型更好
Texel[]mytexturedata={new Texel(){r=0x00,g=0xFF,b=0x00,a=0xFF},…}更改为
类Texel{byte r{get;set;}byte g{get;set;}byte b{get;set;}byte a{get;set;}
。顺便说一句,
unsigned char
对应于C#中的
byte
char
是16位宽(UTF-16)。我认为byte比C#中的unsigned char类型更好。