6 Replies - 30797 Views - Last Post: 15 July 2008 - 06:09 PM Rate Topic: -----

#1 maraqa  Icon User is offline

  • New D.I.C Head

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

convert from decimal to hexadecimal

Post icon  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  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#3 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • 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
Was This Post Helpful? 1

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 11:30 AM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#5 maraqa  Icon User is offline

  • New D.I.C Head

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

Re: convert from decimal to hexadecimal

Posted 14 July 2008 - 08:37 PM

View PostAndyH1963, 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?
Was This Post Helpful? 0
  • +
  • -

#6 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: convert from decimal to hexadecimal

Posted 15 July 2008 - 08:38 AM

View Postmaraqa, on 15 Jul, 2008 - 03:37 AM, said:

View PostAndyH1963, 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.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,447
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1