LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-27-2012, 10:06 AM   #1
Navjot Arora
LQ Newbie
 
Registered: Apr 2012
Location: Bangalore
Distribution: J ARORA
Posts: 22

Rep: Reputation: Disabled
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.
 
Old 11-27-2012, 10:23 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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?
 
Old 11-27-2012, 12:43 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143
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).
 
Old 11-27-2012, 01:34 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 11-27-2012, 05:05 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,097
Blog Entries: 4

Rep: Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089
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.
 
Old 11-27-2012, 05:37 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 View Post
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.
 
Old 11-27-2012, 07:56 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,811

Rep: Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100Reputation: 2100
Quote:
Originally Posted by johnsfine View Post
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 View Post
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 View Post
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Grub for BCD commands in Win7 aljones15 Linux - Desktop 8 01-03-2012 10:06 PM
MQ LINUX vs MQ Unix Conversion Differences - LINUX Conversion Issues catch93 Linux - Newbie 4 11-03-2011 10:10 PM
Do i need integer to hex conversion, if yes how??? payu21 Linux - Newbie 1 04-30-2009 10:30 AM
Behringer BCD 2000 fleamour Linux - Desktop 0 01-15-2007 07:07 PM
C++ string to integer conversion fatman Programming 6 06-18-2006 09:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration