Advertisement
Fhernd

Conversiones.vb

May 20th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.92 KB | None | 0 0
  1. 'Función que convierte de decimal a binario
  2. Public Function ConvertirDecimalABinario(ByVal NumeroDecimal As String) As String
  3.    
  4.     decimalABinario = ""
  5.    
  6.     NumeroDecimal = Int(CDec(NumeroDecimal))
  7.    
  8.     Do While NumeroDecimal <> 0
  9.         decimalABinario = Format$(NumeroDecimal - 2 * Int(NumeroDecimal / 2)) & decimalABinario
  10.         NumeroDecimal = Int(NumeroDecimal / 2)
  11.     Loop
  12.    
  13.     MsgBox (decimalABinario)
  14. End Function
  15.  
  16. 'Función que convierte de binario a decimal
  17. Function ConvertirBinarioADecimal(numeroBinario As String) As Variant
  18.    
  19.     Dim X As Integer
  20.     Dim NumeroDecimal As Integer
  21.    
  22.     For X = 0 To Len(numeroBinario) - 1
  23.         NumeroDecimal = NumeroDecimal + Val(Mid(numeroBinario, Len(numeroBinario) - X, 1)) * 2 ^ X
  24.     Next
  25.    
  26.     MsgBox (NumeroDecimal)
  27. End Function
  28.  
  29.  
  30. 'Función que convierte de decimal a hexadecimal
  31. Public Function ConvertirDecimalAHexadecimal(ByVal Dec As Double) As String
  32.    
  33.     Dim i As Long
  34.     Dim n As Long
  35.     Dim posicionValorHexadecimal As Long
  36.     Dim Hex(1 To 256) As String
  37.     Dim HexTemp As String
  38.     Dim Divisor As Long
  39.      
  40.     Dec = Int(Dec)
  41.      
  42.     For i = 256 To 2 Step -1
  43.         If Dec >= 16 ^ (i - 1) And Dec > 15 Then
  44.             posicionValorHexadecimal = Int(Dec / (16 ^ (i - 1)))
  45.             Dec = Dec - (16 ^ (i - 1)) * posicionValorHexadecimal
  46.            
  47.             Select Case posicionValorHexadecimal
  48.             Case 0 To 9
  49.                 Hex(i) = CStr(posicionValorHexadecimal)
  50.             Case Is = 10
  51.                 Hex(i) = "A"
  52.             Case Is = 11
  53.                 Hex(i) = "B"
  54.             Case Is = 12
  55.                 Hex(i) = "C"
  56.             Case Is = 13
  57.                 Hex(i) = "D"
  58.             Case Is = 14
  59.                 Hex(i) = "E"
  60.             Case Is = 15
  61.                 Hex(i) = "F"
  62.             End Select
  63.            
  64.         Else
  65.             Hex(i) = "0"
  66.         End If
  67.     Next i
  68.    
  69.     posicionValorHexadecimal = Dec
  70.    
  71.     Select Case posicionValorHexadecimal
  72.     Case 0 To 9
  73.         Hex(1) = CStr(posicionValorHexadecimal)
  74.     Case Is = 10
  75.         Hex(1) = "A"
  76.     Case Is = 11
  77.         Hex(1) = "B"
  78.     Case Is = 12
  79.         Hex(1) = "C"
  80.     Case Is = 13
  81.         Hex(1) = "D"
  82.     Case Is = 14
  83.         Hex(1) = "E"
  84.     Case Is = 15
  85.         Hex(1) = "F"
  86.     End Select
  87.    
  88.     For i = 256 To 1 Step -1
  89.         If Hex(i) = "0" Then
  90.         Else
  91.             n = i
  92.             Exit For
  93.         End If
  94.     Next i
  95.    
  96.     For i = n To 1 Step -1
  97.         HexTemp = HexTemp & Hex(i)
  98.     Next i
  99.    
  100.     DecToHex = HexTemp
  101.    
  102.     MsgBox (DecToHex)
  103.    
  104. End Function
  105.  
  106. 'Función que convierte de binario a decimal
  107. Function ConvertirHexadecimalADecimal(ByVal numeroHexadecimal As String) As String
  108.    
  109.     Dim X As Integer
  110.     Dim NumeroDecimal As Long
  111.    
  112.     NumeroDecimal = 0
  113.    
  114.     For X = 0 To Len(numeroHexadecimal) - 1
  115.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "A") = 0 Then
  116.             NumeroDecimal = NumeroDecimal + 10 * 16 ^ X
  117.         End If
  118.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "B") = 0 Then
  119.             NumeroDecimal = NumeroDecimal + 11 * 16 ^ X
  120.         End If
  121.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "C") = 0 Then
  122.             NumeroDecimal = NumeroDecimal + 12 * 16 ^ X
  123.         End If
  124.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "D") = 0 Then
  125.             NumeroDecimal = NumeroDecimal + 13 * 16 ^ X
  126.         End If
  127.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "E") = 0 Then
  128.             NumeroDecimal = NumeroDecimal + 14 * 16 ^ X
  129.         End If
  130.         If StrComp(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1), "F") = 0 Then
  131.             NumeroDecimal = NumeroDecimal + 15 * 16 ^ X
  132.         End If
  133.        
  134.          If IsNumeric(Mid(numeroHexadecimal, Len(numeroHexadecimal) - X, 1)) Then
  135.             NumeroDecimal = 1
  136.         End If
  137.     Next
  138.    
  139.     Dim cadena As String
  140.    
  141.     cadena = CStr(1231235)
  142.    
  143.     MsgBox (CStr(NumeroDecimal))
  144. End Function
  145.  
  146.  
  147. 'Función que convierte de decimal a octal
  148. Public Function ConvertirDecimalAOctal(ByVal NumeroDecimal As String) As String
  149.    
  150.     decimalAOctal = ""
  151.    
  152.     NumeroDecimal = Int(CDec(NumeroDecimal))
  153.    
  154.     Do While NumeroDecimal <> 0
  155.         decimalAOctal = Format$(NumeroDecimal - 8 * Int(NumeroDecimal / 8)) & decimalAOctal
  156.         NumeroDecimal = Int(NumeroDecimal / 8)
  157.     Loop
  158.    
  159.     MsgBox (decimalAOctal)
  160. End Function
  161.  
  162. 'Función que convierte de octal a decimal
  163. Function ConvertirOctalADecimal(numeroOctal As String) As Variant
  164.    
  165.     Dim X As Integer
  166.     Dim NumeroDecimal As Integer
  167.    
  168.     For X = 0 To Len(numeroOctal) - 1
  169.         NumeroDecimal = NumeroDecimal + Val(Mid(numeroOctal, Len(numeroOctal) - X, 1)) * 8 ^ X
  170.     Next
  171.    
  172.     MsgBox (NumeroDecimal)
  173. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement