# I need help "Overflow" binary to decimal converter

Page 1 of 1

## 6 Replies - 2631 Views - Last Post: 17 February 2011 - 03:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=214896&amp;s=841356a1dc50b16c9c0327ac6615f265&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 levin

Reputation: 1
• Posts: 24
• Joined: 08-February 11

# I need help "Overflow" binary to decimal converter

Posted 08 February 2011 - 09:39 PM

i have codes here converter decimal to binary and vice versa..but after i convert decimal to binary then convert it again to decimal if the converted number is higher than 1023 it says "Overflow"? how will i fix this? i really need help with this...

```
Option Explicit

Private Function DecToBin(ByVal x As Long) As String
Dim Y As Long
Dim Num As String

While (x \ 2) > 0
Y = x \ 2
If x > 1 Then
Num = Val(x - (Y * 2)) & Num
End If
x = Y
Wend
Num = x & Num
DecToBin = Num
End Function
Private Function Pow(Number As Long, Power As Integer) As Long
Dim x As Integer
If Power > 1 Then
For x = 2 To Power
Number = Number * 2
Next
Pow = Number
Else
If Power = 0 Then Pow = 1
If Power = 1 Then Pow = Number
End If
End Function
Private Function BinToDec(ByVal Num As Long) As Long
Dim sngNumber As Long
Dim x As Integer
Dim Tmp As Long
Dim Output As Long
sngNumber = Num

For x = 0 To Len(CStr(sngNumber)) - 1
Tmp = CLng(Right(CStr(sngNumber), 1))
If Tmp = 1 Then
Tmp = Tmp * Pow(2, x)
End If
Output = Output + Tmp
Tmp = 1

If Len(CStr(sngNumber)) > 1 Then
sngNumber = CLng(Left\$(CStr(sngNumber), Len(CStr(sngNumber)) - 1))
Else
sngNumber = 0
End If
Next
BinToDec = Output
End Function

Private Sub cmdBintoDec_Click()
txtDecimal.Text = BinToDec(txtBinary.Text)
txtBinary.Text = ""
End Sub

Private Sub cmdDectoBin_Click()
txtBinary.Text = DecToBin(txtDecimal.Text)
txtDecimal.Text = ""
End Sub

```

Is This A Good Question/Topic? 0

## Replies To: I need help "Overflow" binary to decimal converter

### #2 maj3091

• D.I.C Lover

Reputation: 330
• Posts: 1,998
• Joined: 26-March 09

## Re: I need help "Overflow" binary to decimal converter

Posted 09 February 2011 - 08:01 AM

If you have debugged this, can you identify which line the error occurs on?

I'm guessing that when you've converted to binary, your textbox has a binary representation of a number in it.

You're then trying to pass that back to function to convert it back and the overflow is occuring somewhere around that point??

I may be completely wrong, but until you tell us, it's a first guess.

This post has been edited by maj3091: 09 February 2011 - 08:05 AM

Reputation:

## Re: I need help "Overflow" binary to decimal converter

Posted 09 February 2011 - 09:32 AM

Your "BinToDec" function should take a string as input and return a long. You need to be thinking about other changes within that function that will be compatible with this change to your input. . . .

Jim

### #4 levin

Reputation: 1
• Posts: 24
• Joined: 08-February 11

## Re: I need help "Overflow" binary to decimal converter

Posted 09 February 2011 - 01:15 PM

maj3091, on 09 February 2011 - 08:01 AM, said:

If you have debugged this, can you identify which line the error occurs on?

I'm guessing that when you've converted to binary, your textbox has a binary representation of a number in it.

You're then trying to pass that back to function to convert it back and the overflow is occuring somewhere around that point??

I may be completely wrong, but until you tell us, it's a first guess.

here:

```Private Sub Option2_Click()
[color="#FFFF00"]txtDecimal.Text = BinToDec(txtBinary.Text)[/color]
txtBinary.Text = ""
End Sub

```

### #5 levin

Reputation: 1
• Posts: 24
• Joined: 08-February 11

## Re: I need help "Overflow" binary to decimal converter

Posted 09 February 2011 - 01:26 PM

Jim, on 09 February 2011 - 09:32 AM, said:

Your "BinToDec" function should take a string as input and return a long. You need to be thinking about other changes within that function that will be compatible with this change to your input. . . .

Jim

tnx alot it worked =)
now all i need to do is to make a msgbox alert if the user inputed letters.

### #6 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: I need help "Overflow" binary to decimal converter

Posted 14 February 2011 - 12:44 AM

Quote

now all i need to do is to make a msgbox alert if the user inputed letters.

Or you can prevent the user putting in letters altogether using the KeyPress event:
```Sub txtMyText_KeyPress(KeyAscii as Integer)
Select Case KeyAscii
Case 8, 48 To 57
Case Else
Beep
KeyAscii = 0
End Select
End Sub

```
Most of the time, you won't want to stop the user from entering keys, but this time is an exception. A single character is either valid or invalid independently of any other characters, and you don't want the user to waste time entering invalid data.

### #7 Jupiter 2

Reputation: 0
• Posts: 40
• Joined: 05-June 09

## Re: I need help "Overflow" binary to decimal converter

Posted 17 February 2011 - 03:39 PM

It has nothing to do with the code but the environment.

In the File menu, there is an option (if not the Student version) to make "exe". Click it. When the dialog appears to save the file, click the "Options" button instead (under the "Help" button).

Click the "Compile" tab then "Advanced Optimizations" button then there are options to choose. Click Help button for more information on choosing which option the checkmark but I found that checkmarking the "Allow Unrounded Floating Pont Operations" suited me when I designed a Colour conversion package. Click OK, Ok then Cancel. You don't need to compile the Project from making the changes in options.