Thread: Passing an 8-bit String

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question Passing an 8-bit String

    I've been asked to write some code that is unlike anything I've had to do before, so I'm looking for suggestions on how I should go about doing this.

    I need to have an 8-bit string where the first 4-bits represent the command (either erosion or dilation of an image...those 2 functions are already written) and the last 4-bits represent the one parameter that those functions take (an int value representing the size). I have to pass that string into a new function that calls either the erosion or dilation methods based on the first 4-bits of the string with the appropriate parameter from the last 4-bits of the string. The 8-bit string is terminated at null with all 0's. The person who explained it to me is not in for a few days, so I'm at a big dilemma, and don't know where to begin. He wrote down that the 8-bit string could look like this:

    command | parameter
    0001 0001 (i think this one represented erosion)
    0010 0001
    0000 0000

    and that the 8-bit string could be hard coded for now for testing purposes. I just don't understand how the method that I need to write will be able to "extract" the first 4-bits to represent either the erosion or dilation method and then "extract" the last 4-bits for the parameter and also know if there are multiple commands within the string and handle those until there is a null terminator. I'm not even sure how he wants me to hard code this string. Do I represent it with the binary representation? If anybody has any idea of where I could start with this, it would be really appreciated. Thanks

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well the typical way to do this would be to use an int (or even a char), and hex
    numbers - two hex digits represent 8 bits, and to maximise efficiency you could
    cast to a char. Then you can use bitwise operators to extract the data from the
    two nibble's (or nybble as I like to spell it).

    It seems that you may not be allowed to do it this way, so if you have to
    use a string, look up strncpy, strcmp and in the event that the low nibble
    can have any one of 16 possible values, use strtol to access the numerical
    data rather than dealing with ASCII values for 1 and 0.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question

    Okay, then my next question is, or maybe I didn't fully understand, what would I have to do to match up the first hex digit with the correct method and the second hex digit with the correct parameter so that I would be able to call them after I used bitwise operators to extract the hex digits from the string? Do I some how assign those methods and parameters to a hex digit or do I use switch or if statements to call the methods based on the data?

    Thank you for your help. This problem is obviously beyond the programming knowledge that I've been use to.

  4. #4
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Bitwise "extraction" (binary AND):
    Code:
    char bits = 0xFF; // bits = 1111 1111
    
    if (bits & 0xF0){ // extracts the four bits in the command part
        ...
    }
    For more information I suggest you ask everyone's best pal google something like "c++ binary operands".
    Last edited by Osaou; 07-31-2006 at 10:02 AM.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I assume that you are using an int to store your "string" of bits, so I've written
    this small example to demonstrate how you might extract your data.

    Code:
    #include <stdio.h>
    
    /*some definitions of values*/
    #define BITMASK 0xF
    #define NIBBLE 4
    
    int main (void)
    {
    	int a_byte = 0xA8; /*some generic value*/
    	int temp_var1 = 0;
    	int temp_var2 = 0;
    
    	temp_var1 = a_byte & BITMASK;		  /*this line uses the & operator to isolate the lower bits*/
    	temp_var2 = (a_byte >> NIBBLE) & BITMASK; /*this then shifts the upper bits and isolates them */
    
    	printf ("Our byte was %x in hex\n\n", a_byte, a_byte, a_byte);
    	printf ("The lower nibble was %x\n\n", temp_var1, temp_var1);
    	printf ("The upper nibble was %x\n\n", temp_var2, temp_var2);
    
    	/*now we could use the values in temp_var1 and temp_var2 in switch statements, function parameters etc*/
    
    	getchar ();
    }
    So considering your date format, you can use the upper nibble in a switch
    statement (temp_var2), and the lower nibble will always be your data parameter
    (temp_var1). Hope this helps.
    Last edited by Richie T; 07-31-2006 at 10:27 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question

    Richie,

    Thanks for the example. Things are starting to make a lot more sense in terms of how I need to manipulate my data. The last question I have is in regards to your code. What if you have more definitions of values? For instance:

    #define BITMASK 0xF
    #define BITMASK1 0xA
    #define NIBBLE 1
    #define NIBBLE2 2
    #define NIBBLE3 3
    #define NIBBLE4 4

    Would these two lines change?

    temp_var1 = a_byte & BITMASK;
    temp_var2 = (a_byte >> NIBBLE) & BITMASK;

    Because I have something similar to the above 6 definitions, and I will be using a switch statement where one of the NIBBLE's will be the parameter.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I think you misunderstood what Riche was doing...
    If you understand what you're doing, you're not learning anything.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    pseudocode time:

    Code:
    0b00100110 == 38 or 0x26
    
    0b1111 == 15 or 0xF
    
    0b00100110 & 0b1111 == 0b0110 or 0x6 or 6
    
    0b00100110 >> 4 == 0b00000010 /*notice that it shifted over so that the Hi nibble is now the Lo nibble */
    
    0b00000010 & 0b1111 == 0b0010 or 0x2 or 2

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    49
    Thank you for the breakdown. I have everything working in my project and the application works appropriately with all of your suggestions. Thank you again!
    Last edited by slowcoder; 08-01-2006 at 01:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  3. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM