Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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#_Wpf - Fatal编程技术网

C# 转换为字节数组并插入到其他数组中

C# 转换为字节数组并插入到其他数组中,c#,wpf,C#,Wpf,我在一个c#wpf应用程序中工作,我想在其中做几件事。我正在使用字节数组来编写MIDI显示控制消息(在MSC规范1.0中指定) 此消息的结构是0x00字节类似于消息所有部分之间的逗号。我写了这样一封信: byte[] data = {(byte)0xF0, // SysEx (byte)0x7F, // Real

我在一个c#wpf应用程序中工作,我想在其中做几件事。我正在使用字节数组来编写MIDI显示控制消息(在MSC规范1.0中指定)

此消息的结构是0x00字节类似于消息所有部分之间的逗号。我写了这样一封信:

 byte[] data =         
                                        {(byte)0xF0, //  SysEx
                                         (byte)0x7F, //  Realtime
                                         (byte)0x7F, //  Device id
                                         (byte)0x02, //  Constant
                                         (byte)0x01, //  Lighting format 
                                         (commandbyte), //  GO         
                                         (qnumber), //  qnumber     
                                         (byte)0x00, //  comma
                                         (qlist), //  qlist 
                                         (byte)0x00, //  comma
                                         (byte)0xF7, //  End of SysEx        


            };
static byte[] VlqEncode(int value)    
{
    uint uvalue = (uint)value;
    if (uvalue < 128) 
        return new byte[] { (byte)uvalue };

    // simplest case        
    // calculate length of buffer required
    int len = 0;                    
    do 
    {            
        uvalue >>= 7;
    } while (uvalue != 0);

    // encode        
    uvalue = (uint)value;        
    byte[] buffer = new byte[len];
    int offset = 0;        
    do {            buffer[offset] = (byte)(uvalue & 127); 
        // only the last 7 bits            
        uvalue >>= 7;            if(uvalue != 0) buffer[offset++] |= 128; 
        // continuation bit        
    } while (uvalue != 0);        
    return buffer;    
}
byte[] data = new byte[mymessage.count];
data = (byte[])mymessage.ToArray(typeof(byte));`
我希望用户填写无符号整数(如215.5),并希望将这些数字转换为字节(不包含0x00字节,因为这样会错误地解释消息)


转换数字并将字节数组放置在上述位置的最佳方法是什么?

您可能想看看设计用于将基本类型转换为字节数组的
位转换器


但我不确定你在寻找什么样的指导来将这些物品放入你的阵列中
Array.Copy
可以简单地将字节复制到中,但可能我误解了。

发现它是这样的:

 byte[] data =         
                                        {(byte)0xF0, //  SysEx
                                         (byte)0x7F, //  Realtime
                                         (byte)0x7F, //  Device id
                                         (byte)0x02, //  Constant
                                         (byte)0x01, //  Lighting format 
                                         (commandbyte), //  GO         
                                         (qnumber), //  qnumber     
                                         (byte)0x00, //  comma
                                         (qlist), //  qlist 
                                         (byte)0x00, //  comma
                                         (byte)0xF7, //  End of SysEx        


            };
static byte[] VlqEncode(int value)    
{
    uint uvalue = (uint)value;
    if (uvalue < 128) 
        return new byte[] { (byte)uvalue };

    // simplest case        
    // calculate length of buffer required
    int len = 0;                    
    do 
    {            
        uvalue >>= 7;
    } while (uvalue != 0);

    // encode        
    uvalue = (uint)value;        
    byte[] buffer = new byte[len];
    int offset = 0;        
    do {            buffer[offset] = (byte)(uvalue & 127); 
        // only the last 7 bits            
        uvalue >>= 7;            if(uvalue != 0) buffer[offset++] |= 128; 
        // continuation bit        
    } while (uvalue != 0);        
    return buffer;    
}
byte[] data = new byte[mymessage.count];
data = (byte[])mymessage.ToArray(typeof(byte));`
使用了其他人的转换器代码,如下所示:

 byte[] data =         
                                        {(byte)0xF0, //  SysEx
                                         (byte)0x7F, //  Realtime
                                         (byte)0x7F, //  Device id
                                         (byte)0x02, //  Constant
                                         (byte)0x01, //  Lighting format 
                                         (commandbyte), //  GO         
                                         (qnumber), //  qnumber     
                                         (byte)0x00, //  comma
                                         (qlist), //  qlist 
                                         (byte)0x00, //  comma
                                         (byte)0xF7, //  End of SysEx        


            };
static byte[] VlqEncode(int value)    
{
    uint uvalue = (uint)value;
    if (uvalue < 128) 
        return new byte[] { (byte)uvalue };

    // simplest case        
    // calculate length of buffer required
    int len = 0;                    
    do 
    {            
        uvalue >>= 7;
    } while (uvalue != 0);

    // encode        
    uvalue = (uint)value;        
    byte[] buffer = new byte[len];
    int offset = 0;        
    do {            buffer[offset] = (byte)(uvalue & 127); 
        // only the last 7 bits            
        uvalue >>= 7;            if(uvalue != 0) buffer[offset++] |= 128; 
        // continuation bit        
    } while (uvalue != 0);        
    return buffer;    
}
byte[] data = new byte[mymessage.count];
data = (byte[])mymessage.ToArray(typeof(byte));`
然后,我创建一个新的arraylist,在其中按顺序添加每个项目:

ArrayList mymessage = new ArrayList();
foreach(byte uvalue in mymessage)
{
    mymessage.Add((byte)uvalue);
}
mymessage.Add((byte)0x00);
` 等等,直到我得到正确的信息。然后,我只需将其转换为如下字节数组:

 byte[] data =         
                                        {(byte)0xF0, //  SysEx
                                         (byte)0x7F, //  Realtime
                                         (byte)0x7F, //  Device id
                                         (byte)0x02, //  Constant
                                         (byte)0x01, //  Lighting format 
                                         (commandbyte), //  GO         
                                         (qnumber), //  qnumber     
                                         (byte)0x00, //  comma
                                         (qlist), //  qlist 
                                         (byte)0x00, //  comma
                                         (byte)0xF7, //  End of SysEx        


            };
static byte[] VlqEncode(int value)    
{
    uint uvalue = (uint)value;
    if (uvalue < 128) 
        return new byte[] { (byte)uvalue };

    // simplest case        
    // calculate length of buffer required
    int len = 0;                    
    do 
    {            
        uvalue >>= 7;
    } while (uvalue != 0);

    // encode        
    uvalue = (uint)value;        
    byte[] buffer = new byte[len];
    int offset = 0;        
    do {            buffer[offset] = (byte)(uvalue & 127); 
        // only the last 7 bits            
        uvalue >>= 7;            if(uvalue != 0) buffer[offset++] |= 128; 
        // continuation bit        
    } while (uvalue != 0);        
    return buffer;    
}
byte[] data = new byte[mymessage.count];
data = (byte[])mymessage.ToArray(typeof(byte));`