Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:41:25+00:00 2026-05-16T17:41:25+00:00

I want to pack a MIDI message into an NSData object. int messageType =

  • 0

I want to pack a MIDI message into an NSData object.

int messageType = 3; // 0-15
int channel = 5;      // 0-15
int data1 = 56;       // 0-127
int data2 = 78;       // 0-127

int packed = data2;
packed += data1 * 127;
packed += channel * 16129; // 127^2
packed += messageType * 258064; // 127^2 * 16

NSLog(@"packed %d", packed);

NSData *packedData = [NSData dataWithBytes:&packed length:sizeof(packed)];

int recovered;
[packedData getBytes:&recovered];

NSLog(@"recovered %d", recovered);

This works wonderfully and while I’m proud of myself, I know that the conversion to bytes is not done correctly: it should be a direct conversion without a lot of addition and multiplication. How can that be done?

Edit: I’m now aware that I can just do this

char theBytes[] = {messageType, channel, data1, data2};
NSData *packedData = [NSData dataWithBytes:&theBytes length:sizeof(theBytes)];

and on the Java side

byte[] byteBuffer = new byte[4]; // Receive buffer
while (in.read(byteBuffer) != -1) {  
    System.out.println("data2="  + byteBuffer[3]);
}

and it will work, but I’d like the solution to get an NSData with just 3 bytes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:41:25+00:00Added an answer on May 16, 2026 at 5:41 pm

    Here’s a 3-byte solution that I put together.

    char theBytes[] = {message_type  * 16 + channel, data1, data2};
    NSData *packedData = [NSData dataWithBytes:&theBytes length:sizeof(theBytes)];
    
    char theBytesRecovered[3];
    [packedData getBytes:theBytesRecovered];
    
    int messageTypeAgain = (int)theBytesRecovered[0]/16;
    int channelAgain = (int)theBytesRecovered[0] % 16;
    int data1Again = (int)theBytesRecovered[1];
    int data2Again = (int)theBytesRecovered[2];
    
    NSLog(@"packed %d %d %d %d", messageTypeAgain, channelAgain, data1Again, data2Again);
    

    and on the other side of the wire, this is just as easy to pick up, because each byte is a byte. I just finished trying this on the iOS side and the Java side, and there are no problems on either. There is no problem with endian-ness, because each integer fits into one single byte (or two in one byte, in one case).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a server and client that sends and receives UDP packets
I want to create a simple Console in Tcl/Tk I have two problems. First
I have the following code I want to detect the screen off of the
I want to put an instance of scapy.layers.dhcp.BOOTP on a multiprocessing.Queue . Every time
I have this python script b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, len(country), country, len(device), device,
I have SQL Server 2005 Standard Service Pack 2 9.00.4053.00 (Intel X86) Table has
i was making a bash script for my server which pack some directories with
I am new to Blackberry. Present I am working on 4.7 Blackberry component pack.Now
I am trying to get a grip around the packing and unpacking of binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.