473,395 Members | 1,987 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,395 software developers and data experts.

decimal to BCD for long int

Hello:
Can anybody suggest a code or modify the following chunk of
code for converting a decimal to BCD and viceversa for unsigned long
integers. The following code works good for integers but fails for
unsigned long.

int bcd(int dec)
{
return ((dec/10)<<4)+(dec%10);
}

int decimal(int bcd)
{
return ((bcd>>4)*10)+bcd%16;
}
Thanks
Gopal
Nov 13 '05 #1
8 36992
Gopal wrote:

Hello:
Can anybody suggest a code or modify the following chunk of
code for converting a decimal to BCD and viceversa for unsigned long
integers. The following code works good for integers but fails for
unsigned long.

int bcd(int dec)
{
return ((dec/10)<<4)+(dec%10);
}

int decimal(int bcd)
{
return ((bcd>>4)*10)+bcd%16;
}


How does it fail? Replacing each 'int' with 'unsigned long' in your functions
produces correct results for me. Of course, it only works for integers from 0
to 99.

Please provide a complete minimal program that demonstrates your problem.

--
Tim Hagan
Nov 13 '05 #2
go****@mailcity.com (Gopal) wrote in message news:<1b**************************@posting.google. com>...
Hello:
Can anybody suggest a code or modify the following chunk of
code for converting a decimal to BCD and viceversa for unsigned long
integers. The following code works good for integers but fails for
unsigned long.

int bcd(int dec)
{
return ((dec/10)<<4)+(dec%10);
}
As a stylistic note, don't put tabs in code posted to Usenet. They do
strange things to some newsreaders.

(I fixed tabs.)

int decimal(int bcd)
{
return ((bcd>>4)*10)+bcd%16;
}


The problem might be those bitwise operators in there. Shifts assume a
certain width and so will fail if the width varies (apparently,
unsigned longs on your system are wider than ints). A better (slower
but more portable) solution would be to process the number in a loop,
taking the lowest-order number, converting it, and dividing the result
by 10 (suitably cast to int, which should IIRC truncate the result to
an int), ending the loop when the number is zero.

#include <math.h> /* for pow() */

int bcd(int dec)
{
int result, i;

for(i = 0; dec;) {
result = (dec % 10) * (int) pow(10,i); /* Get and convert
lowest-order number. */
dec = (int) dec / 10; /* moved down here for clarity */
}

return(result);
}

int decimal(int bcd)
{
int result;

while(bcd) {
result += bcd % 10;
bcd = (int) bcd / 10;
}

return(result);
}

/* Note: untested code. I could be a dumbass, but it looks right. */
Nov 13 '05 #3
August Derleth wrote:
Gopal wrote:

Can anybody suggest a code or modify the following chunk of
code for converting a decimal to BCD and viceversa for unsigned long
integers.
<SNIP>
The problem might be those bitwise operators in there. Shifts assume a
certain width and so will fail if the width varies (apparently,
unsigned longs on your system are wider than ints). A better (slower
but more portable) solution would be to process the number in a loop,
taking the lowest-order number, converting it, and dividing the result
by 10 (suitably cast to int, which should IIRC truncate the result to
an int), ending the loop when the number is zero.

#include <math.h> /* for pow() */

int bcd(int dec)
{
int result, i;

for(i = 0; dec;) {
result = (dec % 10) * (int) pow(10,i); /* Get and convert
Whilst i never changes it's value, (int) pow( 10, i /*==0*/ )
is one of the fanciest ways to yield 1 I've ever seen.
When dealing with BinaryCodedDecimals, using of floating point
math at all seems rather strange to me.
lowest-order number. */
dec = (int) dec / 10; /* moved down here for clarity */
What do you gain in casting the result of an integer division
to int? BTW, didn't the OP write something about troubles to
change his code to work with unsigned long?

<SNIP>
/* Note: untested code. Thought so... :)
I could be a dumbass, but it looks right. */

So could I, but still I have to ask: what do you want to
achieve with this code?

To OP:
Sorry, I cannot provide working code either right now, because
my brain is almost asleep. Maybe in 6 hours or so, if nobody
else is going to reply meanwhile.

Irrwahn
--
Sig. Sic.
Nov 13 '05 #4
Irrwahn Grausewitz wrote:

August Derleth wrote:
int bcd(int dec) dec = (int) dec / 10; /* moved down here for clarity */


What do you gain in casting the result of an integer division
to int?


He casted the numerator, not the result: (int)(dec / 10)

--
pete
Nov 13 '05 #5
pete wrote:
Irrwahn Grausewitz wrote:
August Derleth wrote:
dec = (int) dec / 10; /* moved down here for clarity */


What do you gain in casting the result of an integer division
to int?


He casted the numerator, not the result: (int)(dec / 10)


Oops, right. Which doesn't make it more useful.

--
Zzzzzzzzzzz-Roooooooon
Nov 13 '05 #6
Irrwahn Grausewitz <ir*****@freenet.de> wrote in
news:i1********************************@4ax.com on Thu 04 Sep 2003
05:53:18p:
August Derleth wrote:
Gopal wrote:

Can anybody suggest a code or modify the following chunk of
code for converting a decimal to BCD and viceversa for unsigned long
integers. <SNIP>

The problem might be those bitwise operators in there. Shifts assume a
certain width and so will fail if the width varies (apparently,
unsigned longs on your system are wider than ints). A better (slower
but more portable) solution would be to process the number in a loop,
taking the lowest-order number, converting it, and dividing the result
by 10 (suitably cast to int, which should IIRC truncate the result to
an int), ending the loop when the number is zero.

I still think this is a good way to do it. Am I wrong in that, or is my
basic idea sound?

#include <math.h> /* for pow() */

int bcd(int dec)
{
int result, i;

for(i = 0; dec;) {
result = (dec % 10) * (int) pow(10,i); /* Get and convert


Whilst i never changes it's value, (int) pow( 10, i /*==0*/ )
is one of the fanciest ways to yield 1 I've ever seen.
When dealing with BinaryCodedDecimals, using of floating point
math at all seems rather strange to me.


I am a dumbass. I forgot to increment i in the loop, and I left it out at
the top of the loop. i should be incremented each time the loop goes
through.

I posted it in haste, so I now repent at leisure.
lowest-order number. */
dec = (int) dec / 10; /* moved down here for clarity */
What do you gain in casting the result of an integer division
to int? BTW, didn't the OP write something about troubles to
change his code to work with unsigned long?


I should have made both the inputs and outputs unsigned longs. Damn.

I shouldn't have cast. I'm not looking too good here, am I?

Anyway, the intended result is to strip off the lowest-order number.

<SNIP>
/* Note: untested code. Thought so... :)
I could be a dumbass, but it looks right. */

So could I, but still I have to ask: what do you want to
achieve with this code?


I want to replace code that's sensitively dependent on the width of the
input with code that's slower, but is following a basically good algorithm
to achieve complete portability.

To OP:
Sorry, I cannot provide working code either right now, because
my brain is almost asleep. Maybe in 6 hours or so, if nobody
else is going to reply meanwhile.
Even in a sleep debt, you're better than I.

Irrwahn
--
Sig. Sic.


--
Veni. Vidi. Screwup.

Nov 13 '05 #7
August Derleth <li*****************@onewest.net> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> wrote :

Sorry, I cannot provide working code either right now, because
my brain is almost asleep. Maybe in 6 hours or so, if nobody
else is going to reply meanwhile.


Even in a sleep debt, you're better than I.

Make sure to read some of my other posts before saying so....
;-)
--
6 * 9 = 42 (base 13)
Nov 13 '05 #8
August Derleth <li*****************@onewest.net> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> wrote :

Sorry, I cannot provide working code either right now, because
my brain is almost asleep. Maybe in 6 hours or so, if nobody
else is going to reply meanwhile.


Even in a sleep debt, you're better than I.

Make sure to read some of my other posts before saying so....
;-)
--
6 * 9 = 42 (base 13)
Nov 13 '05 #9

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
9
by: Ali | last post by:
I did the folloing in python shell: >>> x = 5.07e-25 >>> x = long(x) >>> print x 0L Um... I was under the impresion that long numbers had very very long precision. But, it seems that in...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
6
by: Hardy | last post by:
One of my customers have a sql statement totaled more than 400 lines, about 40KB. when excuted, error arrised saying "SQL0101N The statement is too long or too complex". I tried one of his...
4
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
1
by: invinfo | last post by:
keywords: mysql accounting currency decimal fixed "floating point" Quoting the manual: DECIMAL)] If D is omitted, the default is 0. If M is omitted, the default is 10. All basic calculations...
6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
3
by: ajaksu | last post by:
Hello c.l.p.ers :) Running long(Decimal) is pretty slow, and the conversion is based on strings. I'm trying to figure out whether there is a good reason for using strings like in decimal.py...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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,...

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.