tags:

views:

51

answers:

2

Hi,

I work in a c# wpf application in which I want to do several things. I'm working with byte arrays to compose MIDI Show Control messages (specified in the MSC Specification 1.0).

The structure of this message is that a 0x00 byte is like a comma between all the parts of the message. I compose a message like this:

 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        


            };

I want the user to fill in unsigned integers (like 215.5) and I want to convert these numbers to bytes (without 0x00 bytes because then the message is interpreted wrong).

What is the best way to convert the numbers and place the byte array in the places mentioned above?

+1  A: 

You might want to take a look at the BitConverter class, which is designed to convert base types into byte arrays.

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

But I'm not sure what guidance you are seeking for placing the items into your array. Array.Copy can work to simply copy the bytes in, but maybe I am misunderstanding.

Brazzle
A: 

Found it out like this:

Used someone else's converter code like this:

    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 {            len++;            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;    }

Then I use this to convert the integer:

byte[] mybytearray = VlqEncode(integer);

I then make a new arraylist in which I add each item in sequence:

ArrayList mymessage = new ArrayList(); foreach(byte uvalue in mymessage){mymessage.Add((byte)uvalue);} mymessage.Add((byte)0x00);

and so on until I have the correct message. I then only have to convert this a byte array like this:

byte[] data = new byte[mymessage.count]; data = (byte[])mymessage.ToArray(typeof(byte));

internetmw