473,406 Members | 2,336 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,406 software developers and data experts.

Need Help With Math Prolem Please

I am using a program that converts decimal to BCD and Back Again.
While running code I noticed that somehow the conversion is not
handled correctly. for example run the code below:

For i As Int16 = 10 To 99
Dim str1 As String =
Microsoft.VisualBasic.Left(i.ToString, 1)
Dim str2 As String =
Microsoft.VisualBasic.Right(i.ToString, 1)
Dim aByte As Byte

aByte = (CInt(str1 * 16) Or
CInt(str2)) 'Put variable i into aByte in
BCD
Debug.Print(((aByte / 16 And &HF) * 10 + (aByte And
&HF)).ToString) 'Convert Back To Decimal and Print
Next i

For some reason numbers ending in 8 and 9 for the most part get
multiplied by 10, (19 comes back 29, 99 comes back 109) Anyone now
what I am doing wrong? Or is this a problem with the compiler?

Thanks

Ryan
Aug 22 '08 #1
2 1022
You're not correctly packing the number or unpacking it for that matter.
Try this instead:

dim aByte as Byte
dim str1 as String = ""
dim str2 as String = ""
dim i as Int16

for i=10 to 99
str1 = i.ToString().Substring(0,1)
str2 = i.ToString().Substring(1,1)
aByte = CInt(str1)*16 + CInt(str2)
Debug.Print(((aByte And &HF0) / 16).ToString & (aByte And
&HF).ToString)
next i

- bruce

<RF******@nationalsteel.comwrote in message
news:63**********************************@j22g2000 hsf.googlegroups.com...
>I am using a program that converts decimal to BCD and Back Again.
While running code I noticed that somehow the conversion is not
handled correctly. for example run the code below:

For i As Int16 = 10 To 99
Dim str1 As String =
Microsoft.VisualBasic.Left(i.ToString, 1)
Dim str2 As String =
Microsoft.VisualBasic.Right(i.ToString, 1)
Dim aByte As Byte

aByte = (CInt(str1 * 16) Or
CInt(str2)) 'Put variable i into aByte in
BCD
Debug.Print(((aByte / 16 And &HF) * 10 + (aByte And
&HF)).ToString) 'Convert Back To Decimal and Print
Next i

For some reason numbers ending in 8 and 9 for the most part get
multiplied by 10, (19 comes back 29, 99 comes back 109) Anyone now
what I am doing wrong? Or is this a problem with the compiler?

Thanks

Ryan

Aug 23 '08 #2
You do a lot of conversions that aren't necessary, as well as floating
point division when integer division would be better. I also strongly
urge you to use OPTION STRICT ON.

For i As Integer = 10 To 99
Dim byt as Byte = Convert.ToByte(((i \ 10) * 16) + (i Mod 10))

Debug.Print( (((byt \ 16) * 10) + (byt And &HF)).ToString() )
Next


On Fri, 22 Aug 2008 13:50:51 -0700 (PDT), RF******@nationalsteel.com
wrote:
>I am using a program that converts decimal to BCD and Back Again.
While running code I noticed that somehow the conversion is not
handled correctly. for example run the code below:

For i As Int16 = 10 To 99
Dim str1 As String =
Microsoft.VisualBasic.Left(i.ToString, 1)
Dim str2 As String =
Microsoft.VisualBasic.Right(i.ToString, 1)
Dim aByte As Byte

aByte = (CInt(str1 * 16) Or
CInt(str2)) 'Put variable i into aByte in
BCD
Debug.Print(((aByte / 16 And &HF) * 10 + (aByte And
&HF)).ToString) 'Convert Back To Decimal and Print
Next i

For some reason numbers ending in 8 and 9 for the most part get
multiplied by 10, (19 comes back 29, 99 comes back 109) Anyone now
what I am doing wrong? Or is this a problem with the compiler?

Thanks

Ryan
Aug 23 '08 #3

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

Similar topics

2
by: TSK | last post by:
I cannot get my focus() fn to work in my validateInput(userInput) fn. It will not allow the user to go back and correct invalid input like it should. Instead it goes right on through with the...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
11
by: frankie_85 | last post by:
Hi everyone, I just made a simple code which is part of my assignment but I can't figure it out what's wrong with it. (always give me error messages) What the code basically does is: a...
1
by: bylum | last post by:
prolem exception org.apache.jasper.JasperException at jsp the list exception : org.apache.jasper.JasperException...
1
by: feathers75 | last post by:
-------------------------------------------------------------------------------- First, Hello eveyone and I am new to Java. I am trying to create a Java program that will calculate a person...
4
by: sigkill9 | last post by:
I'm trying to create a script that will accept a user entered integer and find only one set of prime numbers that add up to that integer but need some help. The script is supposed to only accept...
3
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
16
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, I have a neat little script that calculates price based on quantity without refreshing the page.. the script is - <script type="text/javascript"> function OpenWin(url) {...
1
by: Adrienne Boswell | last post by:
Gazing into my crystal ball I observed =?Utf-8?B?R1ROMTcwNzc3?= <GTN170777@discussions.microsoft.comwriting in news:443E2509-7F0E-4CEC-B243-D6EDC931DB7F@microsoft.com: ASP has no knowledge of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.