Need Help With Math Prolem Please

  • Thread starter Thread starter RFleming
  • Start date Start date
R

RFleming

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
 
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
 
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
 
Back
Top