Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ binary and decimal conversion

Posted on 2013-06-23
7
Medium Priority
?
450 Views
Last Modified: 2013-06-24
I am looking for a good way for the following conversion in C++:

41283 is binary digit “1010000101000011”
From first bit,
Structure ID:101¿5 (decimal)
Item No.:0000101¿5 (decimal)
Route No:000011¿3 (decimal)

Thank you.
0
Comment
Question by:cytello
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:cytello
ID: 39269979
Sorry about the strange characters

1. I need to convert 41283 to binary as 1010000101000011
2. convert the first 3 bits 101 to decimal
3. convert the next 7 bits 0000101 to decimal
4. convert the next 6 bits 000011 to decimal

Is there a way to get the decimal values for the 3 bits, 7 bits, and 6 bits?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39270179
If you only want to read that data (and not write it), a simple bit shifting/masking approach will do - otherwise I'd point you to 'bitsets' (http://www.cplusplus.com/reference/bitset/bitset/) or 'bitfields' (http://en.wikipedia.org/wiki/Bit_field). But, in your case, that can be done like

short int val = 41283; // 16 bits

short int structID = (val & 0x1c00) >> 13;
short int itemNo = (val & 0x01fc0) >> 6;
short int routeNo = val & 0x003f;

Open in new window

0
 
LVL 8

Expert Comment

by:lomo74
ID: 39270533
the opposite procedure would be:
unsigned short int val1, val2, val3, num;
val1 = 5;
val2 = 5;
val3 = 3;
num = (val1 & 0x07) << 13 |
	(val2 & 0x7F) << 6 |
	(val3 & 0x3F);

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Accepted Solution

by:
frankhelk earned 2000 total points
ID: 39270740
And here the elegant (bitfields) way to let the compiler sort the stuff out:
#include <stdio.h>

typedef union 
{
	short value;
	struct
	{   // stacked backwards !
		unsigned int RouteNo:6;
		unsigned int itemNo:7;
		unsigned int structId:3;
	} fields;
} bitfield;


int main()
{
	bitfield val;

	val.value = 41283;

	printf("StructID : %d\n", val.fields.structId);
	printf("ItemNo   : %d\n", val.fields.itemNo);
	printf("RouteNo  : %d\n", val.fields.RouteNo);

	return 0;
}

Open in new window

Easier to understand, and more simple to use thruout the code. And more easy to handle if the coding of the bitfield changes.

Output would be
StructID : 5
ItemNo   : 5
RouteNo  : 3

Open in new window

0
 
LVL 8

Expert Comment

by:lomo74
ID: 39270770
0
 
LVL 35

Expert Comment

by:sarabande
ID: 39271832
cytello, is it homework?

if yes, you may post the complete assignment such that we could help you to writing your own code.

generally, converting a string number from one base to another (in your sample it is from base 2 to base 10), you easily could do it by extracting each digit from right to left, convert the digit to number, multiply it with the base^n  (where n is a loop counter beginning at 0) and add the result to the total.

for '110' you would get 0*2^0 + 1*2^1 + 1*2^2 = 0 + 2 + 4 = 6.

Sara
0
 

Author Closing Comment

by:cytello
ID: 39271927
Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question