Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-27-2012, 10:06 AM
|
#1
|
LQ Newbie
Registered: Apr 2012
Location: Bangalore
Distribution: J ARORA
Posts: 22
Rep: 
|
Conversion from Integer to BCD
Hi,
I want to convert the integer (whose maximum value can reach to 99999999) in to BCD and store in to array of 4 characters.
Like for example:
Input is : 12345 (Integer)
Output should be = "00012345" in BCD which is stored in to array of 4 characters.
Here 0 0 0 1 2 3 4 5 stored in BCD format.
I tried in the below manner but didnt work
nt decNum = 12345;
long aux;
aux = (long)decNum;
cout<<" aux = "<<aux<<endl;
char* str = (char*)& aux;
char output[4];
int len = 0;
int i = 3;
while (len < 8)
{
cout <<"str: " << len << " " << (int)str[len] << endl;
unsigned char temp = str[len]%10;
len++;
cout <<"str: " << len << " " << (int)str[len] << endl;
output[i] = ((str[len]) << 4) | temp;
i--;
len++;
}
Last edited by Navjot Arora; 11-27-2012 at 10:27 AM.
|
|
|
11-27-2012, 10:23 AM
|
#2
|
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
|
The issue is not integer to BCD.
BCD is a representation of numbers. So binary to BCD or text to BCD would be valid requests. What have you tried? Can you do a two-digit number?
|
|
|
11-27-2012, 12:43 PM
|
#3
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
I'm really not understanding the request. BCD stands for binary coded decimal, so printing it out in anything other than binary doesn't make sense. What exactly are you trying to do here?
For example, 12 in binary is 1100, but 12 in BCD is 0001 0010. Each decimal digit is encoded in its own 4-digit binary sequence. It's useful when integrating with a 7-seg display, for example.
99999999 in BCD would simply be 1001 repeated 8 times, and CANNOT be stored in an array of 4 characters (you would need 4 characters per decimal digit, or 32 characters for this example).
|
|
|
11-27-2012, 01:34 PM
|
#4
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
It seems like a perfectly reasonable problem statement to me (I don't agree with the hair splitting semantic objection posted by linosaurusroot and even less so with the objection posted by suicidaleggroll). But it also seems like a homework problem, so I don't want to just give the complete program.
In the OP's first post, I can't even guess the point of str. Looking at the original value as if it were any array of bytes (rather than one integer) serves no purpose in this problem. The result should be treated as 4 bytes, but the input is just an integer.
The key step to solving this is knowing that in splitting an integer into decimal digits, it is easiest to work right to left.
The last digit of x is (x%10). All of the digits to the left of that can be gotten by splitting up the digits of x/10.
I stated that as a recursive definition, and it is a slightly more obvious concept if you think of it recursively. But since the recursion is at the end of the description, it is also simple to do that in a loop rather than recursively.
As you split the digits (even if you do so "backwards") it should be easy to store each in the desired place in the output.
Last edited by johnsfine; 11-27-2012 at 01:39 PM.
|
|
|
11-27-2012, 05:05 PM
|
#5
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,097
|
I'm quite confused by this one ...
"BCD" literally means Binary Coded Decimal. This simply means that consecutive four-bit groups represent decimal digits. (One of the groups conveys the sign ... positive or negative.) The format is so well-known that microprocessor architectures implement it directly.
The impetus for BCD is also well-known: accounting!  In the same way that "1/3" cannot precisely be represented in base-ten ("0.333333...."), "1/10" cannot precisely be represented in base-two. So, if you want to process dollars-and-cents without losing a cent (a circumstance which makes bean-counter types very upset ...) one way to do it  is with BCD.
Last edited by sundialsvcs; 11-27-2012 at 05:07 PM.
|
|
|
11-27-2012, 05:37 PM
|
#6
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Looking at the OP's attempt at the code, I get the impression the OP nearly understands the destination format BCD.
It appears the OP is trying to put the high order digit of each pair in the low half of each byte and the low order digit of each pair in the high half of each byte. I think that would be incorrect. But I'm not certain that is even what the OP is doing. The OP's confusion about how to split apart the input overwhelms any details of how to put together the output.
Quote:
Originally Posted by Navjot Arora
Input is : 12345 (Integer)
Output should be = "00012345" in BCD which is stored in to array of 4 characters.
|
That is a confusing way to describe the desired output. A more precise description of that BCD output is:
For input 12345, output should be four bytes whose values in hex are 00 01 23 45
If we had computed output[] correctly and then ran the following code, we would get
00 01 23 45
Code:
printf("%02x %02x %02x %02x\n", output[0], output[1], output[2], output[3]);
Last edited by johnsfine; 11-27-2012 at 05:55 PM.
|
|
|
11-27-2012, 07:56 PM
|
#7
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,811
|
Quote:
Originally Posted by johnsfine
A more precise description of that BCD output is:
For input 12345, output should be four bytes whose values in hex are 00 01 23 45
|
This format is usually known as packed BCD.
Quote:
Originally Posted by johnsfine
It seems like a perfectly reasonable problem statement to me (I don't agree with the hair splitting semantic objection posted by linosaurusroot and even less so with the objection posted by suicidaleggroll).
|
I don't agree with the hair splitting, but only because I think they have split hairs incorrectly. I do think it's important to be pedantic about these kinds of things, otherwise students will have incorrect ideas like "int variables contain decimal numbers" (see OP's code declaring int decNum) even though there is no such thing as a decimal number, only a decimal representation of a number (and also, if you must attach a representation to int variables (which most of the time you shouldn't; just consider them opaque) it would probably be base 2).
Quote:
Originally Posted by linosaurusroot
BCD is a representation of numbers. So binary to BCD or text to BCD would be valid requests.
|
"binary" and "text" are such vague descriptions of number representations that I would hesitate to call those valid requests: you could have with decimal text, binary text (like suicidaleggroll described), roman numeral text (and I haven't even mentioned if "text" means ASCII, EBCDIC, Unicode (UTF-8 (Big Endian, Little Endian), UTF-16, ...), ...), ...
Okay, that's a little overboard, but you need to be a little more detailed if you want describe conversions like "raw" to "binary text string": saying "binary" to "binary" won't do ("raw" or "native" is how I would describe the contents of an int variable).
|
|
|
All times are GMT -5. The time now is 03:47 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|