Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > converting byte to nibble and converted nibble to bytes

Reply
Thread Tools

converting byte to nibble and converted nibble to bytes

 
 
hari
Guest
Posts: n/a
 
      03-19-2008
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
 
Reply With Quote
 
 
 
 
Gene
Guest
Posts: n/a
 
      03-19-2008
On Mar 18, 9:08*pm, hari <(E-Mail Removed)> wrote:
> hi all,
>
> 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 ).
>


Well if this is homework, then stop reading.

There are lots of ways to do this. If n holds the nibble in its low
order bits, then you can for example say

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

It's simple to use this to build a table, which probably gives a
faster code. For example,

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

The bytes you want are then just

tbl[n & 0xf)

and

tbl[(n >> 4) & 0xf]

 
Reply With Quote
 
 
 
 
Amandil
Guest
Posts: n/a
 
      03-19-2008
On Mar 18, 9:08 pm, hari <(E-Mail Removed)> 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.


"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 & 0x 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-19-2008
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 <(E-Mail Removed)> wrote:

> 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


 
Reply With Quote
 
hari
Guest
Posts: n/a
 
      03-19-2008
On Mar 19, 7:16*pm, Nick Keighley <(E-Mail Removed)>
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 <(E-Mail Removed)> wrote:
>
> > 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
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      03-19-2008
On Mar 18, 9:08 pm, hari <(E-Mail Removed)> 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.


Most people would understand "separating into nibbles" as asking for
0x3B >> 4
to get the upper nibble and
0x3B & 0xF
to isolate the lower nibble. What you are asking for is something
totally different.

> What exactly need to do is as follows
>
> 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?
>


I tried to post this yesterday, I don't know why it didn't work.
One method is to use a series of ifs:
unsigned short u = 0x3B;
unsigned short high = 0, low = 0;
if (u & 0x80) high |= 0xC0; /* 0x80 = 1000 0000B, 0xC0 = 1100
0000B */
if (u & 0x40) high |= 0x30; /* 0x40 = 0100 0000B, 0x30 = 0011
0000B */
if (u & 0x20) high |= 0xC; /* 0x20 = 0010 0000B, 0x0C = 0000
1100B */
if (u & 0x10) high |= 0x3; /* 0x10 = 0001 0000B, 0x03 = 0000
0011B */
if (u & 0x low |= 0xC0; /* 0x08 = 0000 1000B, 0xC0 = 1100
0000B */
if (u & 0x4) low |= 0x30; /* 0x04 = 0000 0100B, 0x30 = 0011
0000B */
if (u & 0x2) low |= 0xC; /* 0x02 = 0000 0010B, 0x0C = 0000
1100B */
if (u & 0x1) low |= 0x3; /* 0x01 = 0000 0001B, 0x03 = 0000
0011B */

Alternatively, you could use a loop:
unsigned short u = 0x3B;
unsigned short high = 0, low = 0;
unsigned short test = 0x1, set = 0x3;
int i;
for (i = 0; i < 4; i++) {
if (u & test)
low |= set;
test <<= 1; /* Shift left and assign */
set <<= 2;
}
set = 0x3;
for ( ; i < 8; i++) {
if (u & test)
high |= set;
test <<= 1;
set <= 2;
}

Out of morbid curiosity, why do you want this? Some kind of
programming exercise? Or is it actually a real-world application?

-- Marty Amandil

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      03-20-2008
On Tue, 18 Mar 2008 18:08:52 -0700 (PDT), hari <(E-Mail Removed)>
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
 
Reply With Quote
 
Gene
Guest
Posts: n/a
 
      03-20-2008
On Mar 18, 9:08*pm, hari <(E-Mail Removed)> 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 ).
>
> 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 & << 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]

 
Reply With Quote
 
hari
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 1:52*pm, Gene <(E-Mail Removed)> wrote:
> On Mar 18, 9:08*pm, hari <(E-Mail Removed)> 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 ).

>
> > 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 & << 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..
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-20-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode: Strings marked 'utf8'. Can they be converted to 'byte' without going the vec() route? sln@netherlands.com Perl Misc 0 08-03-2009 08:35 PM
Nibble as variable.. krisworld C Programming 5 05-22-2007 10:31 PM
Nibble as variable.. Chris Dollin C Programming 6 05-22-2007 07:21 PM
How to extract x amount of bytes from a byte object and store into another byte obj. DaBeef Java 1 07-21-2006 05:20 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM



Advertisments