473,804 Members | 3,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)+b cd%16;
}
Thanks
Gopal
Nov 13 '05 #1
8 37029
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)+b cd%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.go ogle.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)+b cd%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 BinaryCodedDeci mals, 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*****@freene t.de> wrote in
news:i1******** *************** *********@4ax.c om 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 BinaryCodedDeci mals, 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.n et> wrote:
Irrwahn Grausewitz <ir*****@freene t.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.n et> wrote:
Irrwahn Grausewitz <ir*****@freene t.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
4543
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
1971
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 this case it rounded it to zero :(
7
3611
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
6
5513
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 functions, create function xxxx(x1 decimal(20,2),x2 decimal(20,2),x3 decimal(20,2),x4 decimal(20,2),x5 decimal(20,2),x6 deci mal(20,2)) returns decimal(20,2)
4
9736
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 even after I have change the Fieldsize propert to Decimal with Scale = 2, the digits after the decimal are not seen. For eg. If the text was 16.27. After I changed to long integer, it
1
3695
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 (+, -, *, /) with DECIMAL columns are done with a precision of 64 decimal digits. Re: Floating Point limitations and using IEEE format floating point many numbers cannot be stored exactly in a FP format
6
35677
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
3
2381
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 (that reason would be bound to bite me down the road). This converts Decimal to long and is much faster in my test system (PIII 650MHz, but was written on a P133 a year ago :)). def dec2long(number):
23
9809
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
5332
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 number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions displayed in VB, so that's no big deal. I'm able to simplify most numbers with some code I've written,...
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10331
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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 we have to send another system
2
3831
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.