Is there anyway to create a 24-bit object?

This is a discussion on Is there anyway to create a 24-bit object? within the C++ Programming forums, part of the General Programming Boards category; So, I'm working on creating an implementation of TLS and it requires a 24-bit Message Length field preceded by an ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Is there anyway to create a 24-bit object?

    So, I'm working on creating an implementation of TLS and it requires a 24-bit Message Length field preceded by an 8-bit Handshake Message Type. For the 8-bit field I can just use a char right? But how can I create a 24-bit object?

    I know I've seen somewhere, something called a u_int24 or something like that, but I don't know how to create one or what that actually is. Does anyone know?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps you could put the fields into a structure and have the message length member be a bitfield with 24 bits.
    Code:
    struct type
    {
       /* ... */
       unsigned length : 24;
       /* ... */
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    The problem with that is it actually has to be 24 bits in length. It has to be sent in a packet that only allows for 24-bits of data for that field. Any struct will be padded to word boundaries (I think) so it will be treated as 32-bits anyway.

    I'm hoping the solution to my problem is NOT to create a struct with 4, 8-bit values and just treat the last three values as one 24-bit integer.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Use bitsets?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Structures may be packed, but such things are implementation dependent. Or you could just go about things by using bytes and packing stuff yourself.

    http://c-faq.com/struct/padding.html
    http://c-faq.com/strangeprob/ptralign.html
    Last edited by Dave_Sinkula; 06-14-2006 at 12:38 PM. Reason: Added links.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    What function are you passing this 24 bit data to? What does the prototype have as the argument type? Perhaps there is a predefined 24 bit object for you to fill.

    Or is it that you're saying whatever your sending to only receives 24 bit objects? Otherwise I'd agree that bitsets are a good solution.
    Last edited by SlyMaelstrom; 06-14-2006 at 12:40 PM.
    Sent from my iPad®

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    What are bitsets?

    It's not a function that I'm trying to pass it to. I am creating a TLS implementation that requires a 24-bit field. This means that when I create the packet and send it and it is received, the receiver will expect only 24-bits of the data or that field. If I pass 32-bits there, then the last 8-bits will be treated as part of the next field in the packet.

    That's why it HAS to be 24-bits and must only occupy 24-bits of space.

    My development environment is Microsoft Visual Studio .NET 2005 and I'm running under Windows XP. I provide this information so maybe someone can tell me if structs are padded to word boundaries?

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not seeing how 24bits relates here. Just... send it. How do you send 32, 16, etc members? You might have to write a function to send 24 bits... would this not work:
    Code:
    SendA24BitThing(int socket, unsigned int my_num)
    {
      unsigned char buf[3];
      buf[0] = (my_num >> 16) & 0xFF;
      buf[1] = (my_num >> 8) & 0xFF;
      buf[2] = my_num & 0xFF;
    
      SendBuffer(socket, buf, 3);
    }
    ...as an example. The just send the rest of your packet the normal way, using whatever function calls you do to send data.

    Alternatively, since an 8bit value follows, use 32 bits and pack the two together. A bit of work either way...
    Last edited by Cactus_Hugger; 06-14-2006 at 01:11 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    What is the "& 0xFF" for? Sorry, I'm not familiar with things like bitshifting and Hex notation.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by maxhavoc
    It's not a function that I'm trying to pass it to. I am creating a TLS implementation that requires a 24-bit field. This means that when I create the packet and send it and it is received, the receiver will expect only 24-bits of the data or that field. If I pass 32-bits there, then the last 8-bits will be treated as part of the next field in the packet.

    That's why it HAS to be 24-bits and must only occupy 24-bits of space.
    Yes, yes, yes. It is possible for you to pack a 32-bit object containing a 24-bit value into exactly 3 8-bit bytes. I'm just saying that it is some work.

    Quote Originally Posted by maxhavoc
    My development environment is Microsoft Visual Studio .NET 2005 and I'm running under Windows XP. I provide this information so maybe someone can tell me if structs are padded to word boundaries?
    http://msdn.microsoft.com/library/de...REDIR_pack.asp
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by Cactus_Hugger
    Alternatively, since an 8bit value follows, use 32 bits and pack the two together. A bit of work either way...
    How do I do this exactly? A link to a tutorial would be helpful, I'd google it, but I'm not sure what this would be called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 06:18 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21