CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2006
    Posts
    194

    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
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Passing an 8-bit String

    What you want to do seems simple enuf at first glance, but I'm confused by one very important basic point: I don't have a clear understanding of your term "string".

    At first I think you're speaking of a String, where each 0 or 1 is actually an 8-bit ASCII char. Then when I read further, I think that you are referring to a string of bits, and each 0 or 1 is just that: a binary bit, and the "string" you speak of is simply an unsigned char.

    Another question I have relates to the gazintas and gazoutas.

    I may be way off base here, but from your description I have the impression that you want a simple encoding function ... one that takes two arguments (command, command_argument), and returns either a String * or an unsigned char (TBD per your answer to question 1, above).

    Is that how you think of the function you need to write?

    My gut feeling is that it MUST be returning an unsigned char .... I can't imagine going to the expense of creating a String to convey information that can be contained in a single byte.

    On the other hand, if the end result is simply an unsigned char, why go to the expense of a function? I would think that if the operands (command, command_argument) are readily available, that you would use a single statement.

    So, can you help us help you? Can you clarify the interface? what are the inputs, and what is the form of the output?

    bill
    Last edited by ThermoSight; July 31st, 2006 at 12:11 PM.

  3. #3
    Join Date
    Jun 2006
    Posts
    194

    Question Re: Passing an 8-bit String

    This was a suggestion that I received from another site:

    Using an int to store your "string" of bits, 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 data 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.


    However, after his posting, it lead me to question:

    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.

    If anybody has suggestions at this point, it would be greatly appreciated.

  4. #4
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Passing an 8-bit String

    Hi SlowCoder.

    yeah, I think that fellow has the right idea, whereas I apparently misunderstood your intent. I thought you were seeking a mechanism to pack the two command components into a single output.

    Your statements .....

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

    will UNpack the combined components, Tempvar2 receiving the high-order nybble while tempvar1 receives the low.

    I would not expect the statements to change, regardless of content, assuming of course that the two components remain within the bounds of a 4-bit nybble.

    Looks like a winner to me. Give it a try.

    Best wishes,

    bill

  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Passing an 8-bit String

    ok im a bit confused.. what is an 8 bit string? i would think that is a string with with 8 bit characters (unicode).

    i think what your looking for is this;

    Code:
    void something(unsigned char X[2])
    {
     switch(X[0])
      {
        case 1:
         // call some function
         return;
      }
    }
    
    {
    unsigned char X[2] = {0};
    X[0] = 1;
    X[1] = 123;
    something(X);
    }

  6. #6
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Passing an 8-bit String

    Hi Mitsukai.

    yeah, that's kinda what I thought at first ... and I shared your confusion, but now I kinda think he didn't mean string in the conventional C/C++ sense, but rather, a FIELD of 8 bits ... i.e. a single byte, signed or unsigned.

    Still, I think I'd better shut up and let him speak for himself.

    Best wishes,

    bill

  7. #7
    Join Date
    May 2002
    Location
    Romania
    Posts
    929

    Re: Passing an 8-bit String

    Perhaps this would help you to clarify your issue:

    http://publib.boulder.ibm.com/infoce...c03defbitf.htm

    Regards,
    Snakekaa
    Move like a snake, think like a snake, be a snake !!!

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Passing an 8-bit String

    oh if were talking about bit fields...

    Code:
    struct hilobyte
    {
       unsigned char lo: 4;
       unsigned char hi: 4;
    };
    
    void something(hilobyte& X)
    {
         switch(X.lo)
          {
            case 1:
    	        // call some function
    	        return;
          }
    }
    
    {
        hilobyte X = {0};
        X.lo = 1; // cant be higher than 256 / 2 = 125
        X.hi = 64; // cant be higher than 256 / 2 = 125
        something(X);
    }
    if your looking for speed:

    Code:
    struct hilobyte
    {
       unsigned char lo: 4;
       unsigned char hi: 4;
    };
    
    void something(hilobyte& X)
    {
       // do something
    }
    
    void somethang(hilobyte& X)
     {
       // do somethang
     }
    
    void(*fArr[125])(hilobyte&) = {something, somethang};
    
    {
        hilobyte X = {0};
        X.lo = 1; // cant be higher than 256 / 2 = 125
        X.hi = 64; // cant be higher than 256 / 2 = 125
       fArr[X.lo](X);
    }
    Last edited by Mitsukai; August 1st, 2006 at 06:54 AM.

  9. #9
    Join Date
    Jun 2006
    Posts
    194

    Re: Passing an 8-bit String

    The code I'm using:

    #define BITMASK 0xF
    #define NIBBLE 4

    int do_filters(char *s)
    {
    while(*s != 0)
    {
    int temp_var1 = 0;
    int temp_var2 = 0;

    temp_var1 = *s & BITMASK;
    temp_var2 = (*s >> NIBBLE) & BITMASK;

    switch (temp_var2)
    {
    case 0xF: filter_dilation(temp_var1); break;
    case 0xA: filter_erosion(temp_var1); break;
    default: return 0;
    }

    *s = (*s >> 8); // this does not work
    }
    return 1;
    }



    The code that I use to call this method:

    char *the_filter = "\xF3";

    do_filters(the_filter);


    The while loop that I use above states: (*s != 0) is there because I need to have the switch statement execute until char *s produces a null value since my program is suppose to account for multiple commands in char *s. So I was told that I need to move through the characters one by one of char *s until it hits 0, and then it should exit the loop. I thought using the right shift operator: *s = (*s >> 8) would work because it would put me past the two hex values that I already accounted for. However, when I debug, the value of *s after it executes the switch statement is equal to -1 instead of 0. I'm very new to this type of programming, so I'm not sure the best way to move through the characters properly. Any help is appreciated. And thank you to everyone who has put in their input.

  10. #10
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Passing an 8-bit String

    At first glance, your reason for shifting *s by 8 bits .... or indeed, for shifting it at all (after extracting what you need) is unclear to me.

    Your function defines the input as a char *, so all you're gonna get outta each element of the char array is 8 bits. and you've used those 8-bits in extracting the command and command_argument. There's no data left in that byte ... you need to proceed to the next byte.

    I would think what you would want to do is increment 's' by 1 to point to the next byte, and exit when the 'while' statement accesses a NULL byte.

    Also, incidentally, I think *s is a -1 after the shift because you ain't usin' unsigned chars, so you may be getting the result of a signed shift, and since the msbit is a 1 (i.e. 0xF3), the '1' bit is propagated throughout the field as the shift transpires. I think that changing everything to unsigned will result in a zero value after 8 bit shift, HOWEVER .... NOTE WELL, IMHO, shifting is NOT the way he intends to have you terminate the process.

    I'd be willing to bet that he wants you to increment the pointer to get the next command and terminate when a NULL byte is ENCOUNTERED, not when it's MANUFACTURED.

    Bottom line, I suggest you change the "*s = (*s >> 8)" statement to something like "s = s+1;"

    Incidentally, how does the terminating null get placed in the character string 'the_filter'? Is that a null-terminated string? If it isn't, if the compiler isn't placing the NULL byte at the end of that string, then you need to explicitly provide it so that your routine terminates as expected.


    If I were doing it, my code might look like this .......
    #define BITMASK 0xF
    #define NIBBLE 4

    int do_filters(char *s)
    {

    char data_byte;
    int temp_var1;
    int temp_var2;

    while((data_byte = *s++) != 0)
    {

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

    switch (temp_var2)
    {
    case 0xF: filter_dilation(temp_var1); break;
    case 0xA: filter_erosion(temp_var1); break;
    default: return 0;
    }

    }
    return 1;
    }



    Best wishes,

    bill
    Last edited by ThermoSight; August 1st, 2006 at 03:08 PM.

  11. #11
    Join Date
    Jun 2006
    Posts
    194

    Smile Re: Passing an 8-bit String

    changing it to: char *the_filter = "\xF3\x00";

    and adding: s = s + 1;

    worked perfectly. Thank you so much for your help!

  12. #12
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Passing an 8-bit String

    'this is 16 bits not 8 bits..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured