473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert a binary into decimal

QQ
Hello

unsigned char a;

the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary.

I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
5

My program is like

if((a && 0x00)==1) return 0;
else if (a&& 0x01)==1) return 1;
.....

Is there any simpler way for it?

Thanks a lot!

Mar 22 '06 #1
4 3549
QQ wrote:
Hello

unsigned char a;

the a[0-3] represents a 4-bit binary,
return a & 0xf;
a[4-6] represents another 3-bit binary.


return (a >> 4) & 0x7;
Mar 22 '06 #2
QQ wrote:
Hello

unsigned char a;

the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary.

I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should
be 5

My program is like

if((a && 0x00)==1) return 0;
else if (a&& 0x01)==1) return 1;
....

Is there any simpler way for it?

Thanks a lot!


0-3 = a & 0x0F;
4-6 = (a & 0x70) >> 4;

--
==============
Not a pedant
==============
Mar 22 '06 #3
QQ wrote:
unsigned char a; the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary. I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
5

My program is like

if((a && 0x00)==1) return 0;
else if (a&& 0x01)==1) return 1;
I don't understand how the above can do what you want.
Is there any simpler way for it?


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

int main(void)
{
unsigned char a;
int i;
srand(time(0));
for (i = 0; i < 4; i++) {
a = (unsigned) rand();
printf
("a = %3u (%#03o, %#02x), first 4 bits=%u,"
" next 3 bits=%u\n",
a, a, a, a >> (CHAR_BIT - 4), 07 & (a >> (CHAR_BIT - 7)));
}
return 0;
}
a = 186 (0272, 0xba), first 4 bits=11, next 3 bits=5
a = 171 (0253, 0xab), first 4 bits=10, next 3 bits=5
a = 19 (023, 0x13), first 4 bits=1, next 3 bits=1
a = 98 (0142, 0x62), first 4 bits=6, next 3 bits=1
Mar 22 '06 #4
On 22 Mar 2006 09:45:53 -0800, "QQ" <ju****@yahoo.com> wrote:
Hello

unsigned char a;

the a[0-3] represents a 4-bit binary,
a[4-6] represents another 3-bit binary.

I'd like to convert a[0-3] into a decimal.
For instance if {a[3] a[2] a[1] a[0}}= {0 1 0 1] the decimal should be
5

My program is like

if((a && 0x00)==1) return 0;
The if is guaranteed to always be false. Anything && 0 is always
false.
else if (a&& 0x01)==1) return 1;


This is no different than if(a). Did you perhaps mean (a & 0x01)?

Apparently your unsigned char holds 0 or 1 as opposed to '0' and '1'.
In this case, you can compute
num1 = a[3]*8 + a[2]*4 + a[1]*2 + a[0];
and
num2 = a[6]*4 + a[5]*2 + a[4];
Remove del for email
Mar 23 '06 #5

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

Similar topics

1
2559
by: ferran | last post by:
Hi, does anybody know how to convert in C++ from base 10 to any other base without loosing the decimal part of the actual value? I came up with this algorithm to convert from decimal to any base...
1
11715
by: Adam | last post by:
Hello, I'm trying to decifer the data in the table that stores the data in the binary format. All numbers are placed in varbinary fields. All I know is the MS SQL 2000 database useing collation...
13
41768
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
7
7070
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
13
9194
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the...
7
19191
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
28
5802
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
5
1289
by: sweeet_addiction16 | last post by:
im coding in c....i need to accept an integer value(decimal) and then after converting it into hexadecimal value i need to write it into a file.i do not need to print it..so using fprintf along...
9
3454
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
10
4907
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6873
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
7367
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
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.