converting byte to nibble and converted nibble to bytes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hari

    converting byte to nibble and converted nibble to bytes

    hi all,

    Im writing a code in which the bytes,needed to be splitted in to
    nibbles.each nibble needs to be made as byte. for example:
    consider 3B as byte, on splitting this I will get 3 and B.The binary
    value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.

    What exactly need to do is as folowws

    3B----byte
    0011 1011----->binary value for 3B

    I need to expand(make it as bytes) 3 and B as follows )

    3 ->0011 as 00001111
    B->1011 as 11001111.

    logic is all the bit position values should be filled with adjacent
    bits(that is for eg 1010 is the value ,it should be made as
    11001100 ).

    How can I do this?

    Thanks
    Hari
  • Amandil

    #2
    Re: converting byte to nibble and converted nibble to bytes

    On Mar 18, 9:08 pm, hari <haricib...@gma il.comwrote:
    hi all,
    >
    Im writing a code in which the bytes,needed to be splitted in to
    nibbles.each nibble needs to be made as byte. for example:
    consider 3B as byte, on splitting this I will get 3 and B.The binary
    value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
    "Splitting into nibbles" usually means 0x3B >4 (to get the upper
    nibble) and 0x3B & 0xF (to get the lower nibble). What you describe
    below is something totally different.
    What exactly need to do is as folowws
    >
    3B----byte
    0011 1011----->binary value for 3B
    >
    I need to expand(make it as bytes) 3 and B as follows )
    >
    3 ->0011 as 00001111
    B->1011 as 11001111.
    >
    logic is all the bit position values should be filled with adjacent
    bits(that is for eg 1010 is the value ,it should be made as
    11001100 ).
    >
    How can I do this?
    >
    (
    A brute force method would be to use a series of ifs:
    unsigned short s = 0x3b;
    uningned short high = 0, low = 0;
    if (s & 0x80) high |= 0xC0; /* 0x80 is 1000 0000; 0xC0 is 1100
    0000 */
    if (s & 0x40) high |= 0x30; /* 0x40 is 0100 0000; 0x30 is 0011
    0000 */
    if (s & 0x20) high |= 0xC; /* 0x20 is 0010 0000; 0x0C is 0000
    1100 */
    if (s & 0x10) high |= 0x3; /* 0x10 is 0001 0000; 0x03 is 0000
    0011 */
    if (s & 0x8) low |= 0xC0; /* 0x08 is 0000 0100; 0xC0 is 1100
    0000 */
    if (s & 0x4) low |= 0x30; /* 0x04 is 0000 0100; 0x30 is 0011
    0000 */
    if (s & 0x2) low |= 0xC; /* 0x02 is 0000 0010; 0x0C is 0000
    1100 */
    if (s & 0x1) low |= 0x3; /* 0x01 is 0000 0001; 0x03 is 0000
    0011 */

    This can be put into a loop, testing against 0x1 and setting 0x3, then
    shifting:
    unsigned test = 0x1, set = 0x3; /* s, high, low have been
    previously init */
    for (i = 0; i <= 4; i++) {
    if (s & test)
    low |= set;
    test <<= 1;
    set <<= 2;
    }
    for (; i <= 8; i++) {
    if (s & test)
    high |= set;
    test <<= 1;
    set <<= 2;
    }

    Out of morbid curiosity, why do you need this code? I'd like to know
    if this is a real-world application or a programming exercise.

    -- Marty Amandil

    Comment

    • Nick Keighley

      #3
      Re: converting byte to nibble and converted nibble to bytes

      google problems. I can see two other people have posted
      but I cannot see their posts. Apologies if I am repeating
      stuff.


      On 19 Mar, 01:08, hari <haricib...@gma il.comwrote:
      Im writing a code in which the bytes,needed to be splitted in to
      nibbles.
      bottom_nibble = byte & 0xf;
      top_nibble = (byte >4) & 0xf;

      each nibble needs to be made as byte. for example:
      consider 3B as byte, on splitting this I will get 3 and B.The binary
      value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
      >
      What exactly need to do is as folowws
      >
       3B----byte
      0011   1011----->binary value for 3B
      >
      I need to expand(make it as bytes) 3 and B as follows   )
      >
      3 ->0011 as 00001111
      B->1011 as 11001111.
      >
      logic is all the bit position values should be filled with adjacent
      bits(that is for eg 1010 is the value ,it should be made as
      11001100 ).
      >
      How can I do this?
      well with only 16 possible nibble values why not use a lookup
      table?

      unsigned char nib2byt[] = {0x00, 0x03, 0xC0, 0xC3...};

      expanded_byte = nib2byt [nibble];


      --
      Nick Keighley


      Comment

      • hari

        #4
        Re: converting byte to nibble and converted nibble to bytes

        On Mar 19, 7:16 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
        wrote:
        google problems. I can see two other people have posted
        but I cannot see their posts. Apologies if I am repeating
        stuff.
        >
        On 19 Mar, 01:08, hari <haricib...@gma il.comwrote:
        >
        Im writing a code in which the bytes,needed to be splitted in to
        nibbles.
        >
        bottom_nibble = byte & 0xf;
        top_nibble    = (byte >4) & 0xf;
        >
        >
        >
        >
        >
        each nibble needs to be made as byte. for example:
        consider 3B as byte, on splitting this I will get 3 and B.The binary
        value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
        >
        What exactly need to do is as folowws
        >
         3B----byte
        0011   1011----->binary value for 3B
        >
        I need to expand(make it as bytes) 3 and B as follows   )
        >
        3 ->0011 as 00001111
        B->1011 as 11001111.
        >
        logic is all the bit position values should be filled with adjacent
        bits(that is for eg 1010 is the value ,it should be made as
        11001100 ).
        >
        How can I do this?
        >
        well with only 16 possible nibble values why not use a lookup
        table?
        >
        unsigned char nib2byt[] = {0x00, 0x03, 0xC0, 0xC3...};
        >
        expanded_byte = nib2byt [nibble];
        >
        --
        Nick Keighley- Hide quoted text -
        >
        - Show quoted text -
        I m not able to view the replies.Can you please give your suggestion
        again.
        Nick,
        I need to make 1010 as 11001100 that is each bit should be rePlicated
        two times

        Comment

        • Barry Schwarz

          #5
          Re: converting byte to nibble and converted nibble to bytes

          On Tue, 18 Mar 2008 18:08:52 -0700 (PDT), hari <haricibi83@gma il.com>
          wrote:
          >hi all,
          >
          >Im writing a code in which the bytes,needed to be splitted in to
          >nibbles.each nibble needs to be made as byte. for example:
          >consider 3B as byte, on splitting this I will get 3 and B.The binary
          >value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
          >
          >What exactly need to do is as folowws
          >
          3B----byte
          >0011 1011----->binary value for 3B
          >
          >I need to expand(make it as bytes) 3 and B as follows )
          >
          >3 ->0011 as 00001111
          >B->1011 as 11001111.
          >
          >logic is all the bit position values should be filled with adjacent
          >bits(that is for eg 1010 is the value ,it should be made as
          >11001100 ).
          >
          If I understand correctly, you want to take a byte (limited to systems
          where with 8 bits) whose bits are abcdefgh and expand it to two bytes
          aabbccdd eeffgghh.

          I suggest you use the bit-wise "and" operator to determine if a
          particular bit is 1 or 0. If you initialize your output bytes
          judiciously, you only have to process the case where the input bit is
          1 and use the bit-wise "or" operator to set the appropriate two output
          bits to 1.

          The best way to get help here will be to show your code and ask
          specific questions about what is not working.


          Remove del for email

          Comment

          • Gene

            #6
            Re: converting byte to nibble and converted nibble to bytes

            On Mar 18, 9:08 pm, hari <haricib...@gma il.comwrote:
            hi all,
            >
            Im writing a code in which the bytes,needed to be splitted in to
            nibbles.each nibble needs to be made as byte. for example:
            consider 3B as byte, on splitting this I will get 3 and B.The binary
            value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
            >
            What exactly need to do is as folowws
            >
             3B----byte
            0011   1011----->binary value for 3B
            >
            I need to expand(make it as bytes) 3 and B as follows   )
            >
            3 ->0011 as 00001111
            B->1011 as 11001111.
            >
            logic is all the bit position values should be filled with adjacent
            bits(that is for eg 1010 is the value ,it should be made as
            11001100 ).
            >
            How can I do this?
            Sorry I sent this a few minutes after you posted, but it seems lost in
            the Google bit bucket.

            This will do what you want. There are many other possibilities:

            b = ((((((n & 8) << 1) | (n & 4)) << 1) | (n & 2)) << 1) | (n &
            1);
            b |= (b << 1);

            But as has been pointed out, tables are probably quicker. You can use
            the code above to get the table:

            unsigned char tbl[] = {
            0x00,0x03,0x0c, 0x0f,0x30,0x33, 0x3c,0x3f,
            0xc0,0xc3,0xcc, 0xcf,0xf0,0xf3, 0xfc,0xff
            };

            Now the two bytes you want are

            tbl[n & 0xf]

            and

            tbl[(n >4) & 0xf]

            Comment

            • hari

              #7
              Re: converting byte to nibble and converted nibble to bytes

              On Mar 20, 1:52 pm, Gene <gene.ress...@g mail.comwrote:
              On Mar 18, 9:08 pm, hari <haricib...@gma il.comwrote:
              >
              >
              >
              >
              >
              hi all,
              >
              Im writing a code in which the bytes,needed to be splitted in to
              nibbles.each nibble needs to be made as byte. for example:
              consider 3B as byte, on splitting this I will get 3 and B.The binary
              value for 3 is 0011 and B is 1011?I need to make this3 and B as byte.
              >
              What exactly need to do is as folowws
              >
               3B----byte
              0011   1011----->binary value for 3B
              >
              I need to expand(make it as bytes) 3 and B as follows   )
              >
              3 ->0011 as 00001111
              B->1011 as 11001111.
              >
              logic is all the bit position values should be filled with adjacent
              bits(that is for eg 1010 is the value ,it should be made as
              11001100 ).
              >
              How can I do this?
              >
              Sorry I sent this a few minutes after you posted, but it seems lost in
              the Google bit bucket.
              >
              This will do what you want.  There are many other possibilities:
              >
                  b  = ((((((n & 8) << 1) | (n & 4)) << 1) | (n & 2)) << 1) | (n&
              1);
                  b |= (b << 1);
              >
              But as has been pointed out, tables are probably quicker.  You can use
              the code above to get the table:
              >
                  unsigned char tbl[] = {
                    0x00,0x03,0x0c, 0x0f,0x30,0x33, 0x3c,0x3f,
                    0xc0,0xc3,0xcc, 0xcf,0xf0,0xf3, 0xfc,0xff
                  };
              >
              Now the two bytes you want are
              >
              tbl[n & 0xf]
              >
              and
              >
              tbl[(n >4) & 0xf]- Hide quoted text -
              >
              - Show quoted text -
              Hi all,
              Reason to expand this is
              i m having a PRINT FILE whose values are in hex.(say 3B).When it is
              send for printing it will be expanded as binary form as 0011 1011,
              where binary value 1 reprsents black data and 0 represents white
              data.if the data needs to be expanded by two times, then 3B should
              become 0000 1111 1100 1111.This value is expansion of 3B(0011 1100).
              If I need to expand 3B by 4 times then 0000 0000 1111 1111 1111 0000
              1111 1111.So I need to expand like this.

              Now currently the printing is happening for only 3B value,it should be
              expanded as 0F CF.

              To summarize .

              values is ABCDEFGH(8 bits) then if it is expanded by 2 times then it
              should be AABB CCDD EEFF GGHH.
              If it is expanded by 3 times then , AAAB BBCC CDDD EEEF FFGG GHHH.

              Hope the explantioon is clear.Can I do this in easy wy..

              Comment

              • Richard Heathfield

                #8
                Re: converting byte to nibble and converted nibble to bytes

                hari said:

                <snip>
                To summarize .
                >
                values is ABCDEFGH(8 bits) then if it is expanded by 2 times then it
                should be AABB CCDD EEFF GGHH.
                If it is expanded by 3 times then , AAAB BBCC CDDD EEEF FFGG GHHH.
                >
                Hope the explantioon is clear.
                Yes, it is.
                Can I do this in easy wy..
                Apparently not. But it can be done in an easy way, yes.

                #include <limits.h>
                #include <stdio.h>

                #define BYTE(x) ((x) / CHAR_BIT)
                #define BIT(x) ((x) % CHAR_BIT)
                #define SET_BIT(a, b) \
                (a)[BYTE(b)] |= (1 << BIT(b))
                #define CLEAR_BIT(a, b) \
                (a)[BYTE(b)] &= ~(1 << BIT(b))
                #define TEST_BIT(a, b) \
                (!!(((a)[BYTE(b)]) & (1 << BIT(b))))

                void doit(char x, int repeats)
                {
                unsigned char *p = (unsigned char *)&x;
                size_t i = 0;
                int r = 0;
                while(i < sizeof x)
                {
                size_t b = 0;
                while(b < CHAR_BIT)
                {
                for(r = 0; r < repeats; r++)
                {
                putchar('0' + TEST_BIT(p, b));
                }
                ++b;
                }
                ++i;
                }
                }

                int main(void)
                {
                doit(6, 2); putchar('\n');
                doit(42, 3); putchar('\n');
                doit('Z', 4); putchar('\n');
                return 0;
                }



                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • CBFalconer

                  #9
                  Re: converting byte to nibble and converted nibble to bytes

                  hari wrote:
                  >
                  .... snip ...
                  >
                  Reason to expand this is i m having a PRINT FILE whose values
                  are in hex.(say 3B).When it is send for printing it will be
                  expanded as binary form as 0011 1011, where binary value 1
                  reprsents black data and 0 represents white data.if the data
                  needs to be expanded by two times, then 3B should become 0000
                  1111 1100 1111.This value is expansion of 3B(0011 1100). If I
                  need to expand 3B by 4 times then 0000 0000 1111 1111 1111 0000
                  1111 1111.So I need to expand like this.
                  >
                  Now currently the printing is happening for only 3B value,it
                  should be expanded as 0F CF.
                  Doesn't work. You also need to expand in the vertical direction.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.


                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • hari

                    #10
                    Re: converting byte to nibble and converted nibble to bytes

                    On Mar 20, 5:00 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                    hari wrote:
                    >
                    ... snip ...
                    >
                    Reason  to expand this is i m having a PRINT FILE whose values
                    are in hex.(say 3B).When it is send for printing it will be
                    expanded as binary form  as 0011 1011, where binary value 1
                    reprsents black data and 0 represents white data.if the data
                    needs to be expanded by two times, then 3B  should become 0000
                    1111 1100 1111.This value is expansion of 3B(0011 1100). If I
                    need to expand 3B by 4 times then 0000 0000 1111 1111 1111 0000
                    1111 1111.So I need to expand like this.
                    >
                    Now currently the printing is happening for only 3B value,it
                    should be expanded as 0F  CF.
                    >
                    Doesn't work.  You also need to expand in the vertical direction.
                    >
                    --
                     [mail]: Chuck F (cbfalconer at maineline dot net)
                     [page]: <http://cbfalconer.home .att.net>
                                Try the download section.
                    >
                    --
                    Posted via a free Usenet account fromhttp://www.teranews.co m
                    yes, I accept .it will increase only the width,to increase in height
                    ths same binary values should be repeated.But initially I need to do
                    the byte expansion.

                    Comment

                    Working...