# convert from decimal to hexadecimal

Page 1 of 1

## 6 Replies - 36048 Views - Last Post: 15 July 2008 - 06:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=57502&amp;s=f1456e85189d6c8bb0124be0895345d8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 maraqa

Reputation: 0
• Posts: 3
• Joined: 28-April 08

# convert from decimal to hexadecimal

Posted 14 July 2008 - 07:21 AM

hi,im not sure this s the best algorithm for conversions (from decimal to hexa) ,and i've done a lot of changes that the code s already a mess.
when run,the program results no output!
any kind of help appreciated

```	Private Sub BtnHexa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHexa.Click
Dim c As Integer = 0
Dim Remainder As Integer
Dim HexaArray(c) As Integer
Dim result As Integer

Dim IntegerDiv As Integer

Do While Remainder <> 0
Remainder = IntegerDiv Mod 16
HexaArray(c) = Remainder
IntegerDiv = IntegerDiv / 16
result = result & HexaArray(c)
c = c + 1
Loop

For c = 15 To 0 Step -1
If HexaArray(c) >= 10 Then
Select Case HexaArray(c)
Case HexaArray(c) = 10
result = "A"
Case HexaArray(c) = 11
result = "B"
Case HexaArray(c) = 12
result = "C"
Case HexaArray(c) = 13
result = "D"
Case HexaArray(c) = 14
result = "E"
Case HexaArray(c) = 15

result = "F"
TxtBinary.Text = result

End Select
Else
TxtBinary.Text = TxtBinary.Text & HexaArray(c)
End If

Next

End Sub

```

Is This A Good Question/Topic? 0

## Replies To: convert from decimal to hexadecimal

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 07:24 AM

This may not be the exact solution to your problem [as it is in C] but may help you to build your solution in VB.

### #3 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 11:24 AM

Using VB6 to convert from a Hex value to Decimal and vice versa:

```   Dim sHexValue As String
Dim lDecValue as Long

' From Hex to Dec:
lDecValue = CLng("HFFF")

' From Dec to Hex:
sHexValue = "&H" & Hex\$(4095)

```

HTH

### #4 PsychoCoder

Reputation: 1649
• Posts: 19,853
• Joined: 26-July 07

## Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 11:30 AM

Moved to VB.NET

### #5 maraqa

Reputation: 0
• Posts: 3
• Joined: 28-April 08

## Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 08:37 PM

AndyH1963, on 14 Jul, 2008 - 11:24 AM, said:

Using VB6 to convert from a Hex value to Decimal and vice versa:

```   Dim sHexValue As String
Dim lDecValue as Long

' From Hex to Dec:
lDecValue = CLng("HFFF")

' From Dec to Hex:
sHexValue = "&H" & Hex\$(4095)

```

HTH

thanks ,but that s not the case,cant u figure out anything wrong with my code that enables it to work?

### #6 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: convert from decimal to hexadecimal

Posted 15 July 2008 - 08:38 AM

maraqa, on 15 Jul, 2008 - 03:37 AM, said:

AndyH1963, on 14 Jul, 2008 - 11:24 AM, said:

Using VB6 to convert from a Hex value to Decimal and vice versa:

```   Dim sHexValue As String
Dim lDecValue as Long

' From Hex to Dec:
lDecValue = CLng("HFFF")

' From Dec to Hex:
sHexValue = "&H" & Hex\$(4095)

```

HTH

thanks ,but that s not the case,cant u figure out anything wrong with my code that enables it to work?

I thought you wanted code to convert from Hex to Dec and vice versa, so that is what I have provided. Why write a load of code to accomplish somthing that is already provided for you? Anyway, you work out where your code is going wrong.

• MrCupOfT

Reputation: 2292
• Posts: 9,531
• Joined: 29-May 08

## Re: convert from decimal to hexadecimal

Posted 15 July 2008 - 06:09 PM

Your original code need a few tweaks
```	Private Sub BtnHexa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHexa.Click
Dim c As Integer = 0
Dim Remainder As Integer
Dim HexaArray() As Integer
Dim result As Integer

Dim IntegerDiv As Integer '<- Need A value

Do While Remainder <> 0
Remainder = IntegerDiv Mod 16
Array.Resize(HexaArray,c+1)
HexaArray(c) = Remainder
IntegerDiv = IntegerDiv / 16
result = result & HexaArray(c)
c = c + 1
Loop
For c = 15 To 0 Step -1
If HexaArray(c) >= 10 Then
Select Case HexaArray(c)
Case HexaArray(c) = 10
result = "A"
Case HexaArray(c) = 11
result = "B"
Case HexaArray(c) = 12
result = "C"
Case HexaArray(c) = 13
result = "D"
Case HexaArray(c) = 14
result = "E"
Case HexaArray(c) = 15

result = "F"

End Select
TxtBinary.Text = TxtBinary.Text &  result
Else
TxtBinary.Text = TxtBinary.Text & HexaArray(c)
End If
Next
End Sub

```

I think the result maybe back-to-front?