Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-26-2004, 07:45 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Splitting Bytes in C

I am working on midi events.

There is one byte in the event which I need to take apart.

B0 (hex value)

B is refers to the event being a control change, and 0 refers to the midi channel.

in C, how can i put each of these in their own variable?
__________________
Mike
sde is offline   Reply With Quote
Old 05-26-2004, 09:30 AM   #2 (permalink)
Blood Night
Registered User
 
Join Date: May 2004
Posts: 4
Blood Night is on a distinguished road
i think it's similar to the operation that the decimal does.

for instance

how will you split the 21 into two variables?
21%10=1
21/10=2

just a referrence
Blood Night is offline   Reply With Quote
Old 05-26-2004, 11:21 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
How are the two bytes stored and to what (in what datatype) do you wish to store the single byte.
In case you were looking for what Blood Night meant, then here is a play thingy:
Code:
#include <iostream.h>

const unsigned short currentbyte = 0xB3;

int main()
{
	//unsigned short = max 2 bytes; e.g. 0xFF;
	unsigned short value, remain;
	
	//A bit of toying: 0x10 = 16 Decimal!

	value = currentbyte / 0x10;
	//Convert to hex
	cout<<hex<<value<<endl; //b
	cout<<value<<endl;	//b
	//convert to decimal
	cout<<dec<<value<<endl;	//11
	cout<<value<<endl;	//11
	
	remain = currentbyte % 0x10;
	cout<<hex<<remain<<endl;	//3
	cout<<remain<<endl;		//3
	cout<<dec<<remain<<endl;	//3
	cout<<remain<<endl;		//3

	return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 05-26-2004, 01:44 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Code:
unsigned char v, control, channel;

v = 0xB0;
control = (v >> 4);
channel = (v & 0xf);
joe_bruin is offline   Reply With Quote
Old 05-26-2004, 02:33 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
the original type is DWORD.

when i do this, i have problems casting DWORD to unsigned char:
Code:
v = (dwParam1>>0);
after a little bit of fussing around with it, this works out good.
Code:
unsigned char cc,channel,value;
cc = (dwParam1>>8);
channel = (dwParam1>>0)%16;
value = (dwParam1>>16);
i am suprised that i'm not getting some sort of type casting errors that second way.

thanks again!
__________________
Mike
sde is offline   Reply With Quote
Old 05-26-2004, 03:09 PM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
alright, let's see if we understand this correctly.

what is the dword's value? is it 0x000000b0 ?
and you want the 'b' and the last '0' (ie, two nibbles from the last byte)?
joe_bruin is offline   Reply With Quote
Old 05-26-2004, 03:17 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
the DWORD is representing a short MIDI Event message.

Normal MIDI Events can be up to 4 bytes: status byte, data1, data2, data3

this is how i understand it anyway.

the control change and channel information is in the first byte (status byte), the value of that is in the second byte(data1). in this message, the third and forth bytes are not used.

http://harmony-central.com/MIDI/Doc/table1.html

That is sorta what is going on here. It's not a great example, but you might get a more clear picture of the MIDI event.
__________________
Mike
sde is offline   Reply With Quote
Old 05-26-2004, 03:24 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
when i open a midi port, i can designate a callback function. this function is triggered whenever a midi event is sent to the midi input.

if you really want to understand what i'm doing, here is some of the callback function:
Code:
void CALLBACK midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
  LPMIDIHDR lpMIDIHeader;
  unsigned char *ptr;
  TCHAR buffer[80];
  unsigned char bytes;
  
  switch (uMsg)
  {
    // Received some regular MIDI message
    case MIM_DATA:
    {    
      // ignore active sensing
      if( dwParam1 != 0xF8 ){
            
        unsigned char cc,channel,value;
        cc = (dwParam1>>8);
        channel = (dwParam1>>0)%16;
        value = (dwParam1>>16);
        
        sprintf(&buffer[0], "cc: %d ch: %d v: %d \r\n", cc, 
                                                        channel, 
                                                        value);
        _cputs(&buffer[0]);
      }
      
      break;
    }
  }
}
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 11:31 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting