473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Mar 19 '08 #1
9 24451
On Mar 18, 9:08 pm, hari <haricib...@gmail.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
Mar 19 '08 #2
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...@gmail.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
Mar 19 '08 #3
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...@gmail.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
Mar 19 '08 #4
On Tue, 18 Mar 2008 18:08:52 -0700 (PDT), hari <ha********@gmail.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
Mar 20 '08 #5
On Mar 18, 9:08*pm, hari <haricib...@gmail.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]

Mar 20 '08 #6
On Mar 20, 1:52*pm, Gene <gene.ress...@gmail.comwrote:
On Mar 18, 9:08*pm, hari <haricib...@gmail.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..
Mar 20 '08 #7
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
Mar 20 '08 #8
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

Mar 20 '08 #9
On Mar 20, 5:00*pm, CBFalconer <cbfalco...@yahoo.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.com
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.
Mar 21 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Samuel Thomas | last post by:
Hello Friends, I understand(could be wrong) that the smallest chunk of memory is called a word. If that is correct, that means if I am using a 32 bit OS a word is 4 bytes. So that's why the size...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
5
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
7
by: Yama | last post by:
Hi, I have the following binary data: StringData = 800006000000; which is equivalent to the following: byte bytes = new byte; bytes = 0x80;
26
by: John Grandy | last post by:
Is it possible to generate a 20 byte integer from a GUID that is "unique enough" ( just like a GUID is not truly unique , but is "unique enough" ). We identify transactions with GUIDs , but a...
1
by: Benoit | last post by:
Hi, what is the most performing way to convert a byte stream to a string? Byte() to be converted to String. Thanks, iBen. Sorry if it ia a double post in this newsgroup, I cannot set my...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
6
Robbie
by: Robbie | last post by:
Hi again all, here's something I'm stuck on... I'm making a function to convert a unicode character into the kind of code you need to put on a UTF-8 encoded web page (ampersand, hash, digits,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.