Los sistemas de
numeración son conjuntos de dígitos usados para representar cantidades, así se
tienen los sistemas de numeración decimal, binario, octal, hexadecimal, romano,
etc. Los cuatro primeros se caracterizan por tener una base (número de dígitos diferentes:
diez, dos, ocho, dieciséis respectivamente) mientras que el sistema romano no
posee base y resulta más complicado su manejo tanto con números, así como en
las operaciones básicas.
Los sistemas de
numeración que poseen una base tienen la característica de cumplir con la
notación posicional, es decir, la posición de cada número le da un valor o
peso, así el primer dígito de derecha a izquierda después del punto decimal,
tiene un valor igual a b veces el valor del dígito, y así el dígito tiene en la
posición n un valor igual a: (bn)
* A
Dónde:
b = valor de la base del sistema
n = número del dígito o posición del mismo
A = dígito.
b = valor de la base del sistema
n = número del dígito o posición del mismo
A = dígito.
Es un solo formulario el cual tiene el fin de convertir
diferentes sistemas numéricos
Private
Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button1.Click
Dim
bits() As Integer
Dim pos
As Integer = -1
Dim x As Integer =
Val(txtnume.Text)
Dim bit
As Integer
Do While x >= 2
bit = x Mod 2 'se obtiene el bit
x
= Int(x / 2) 'se obtiene el siguiente numero
pos = pos + 1
ReDim Preserve
bits(pos) 'asignar el tamaño del vector
bits(pos) = bit
Loop
'******se almacena el ultimo bit*****************
'**************que se obtiene de lo que halla
quedado************
pos =
pos + 1
ReDim Preserve
bits(pos) 'se crea una nueva posicion
bits(pos)
= x
txtresultado.Text = ""
For i As Integer = pos To 0 Step -1
'MsgBox(bits(i))
txtresultado.Text =
txtresultado.Text & bits(i)
Next
txtnume.Enabled = False
End Sub
El botón de calcula de decimal a hexadecimal se declaran
tres variables, una de ellas es la matriz la otra es la posición en la que se
encuentra y la ultima el valor de elemento que se va a cambiar.
Private
Sub Button5_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button5.Click
Dim
binarios As String
binarios = txtnume.Text()
Dim
potencia As Integer
= -1
Dim bit
As Integer
Dim
base As Integer
Dim
numdecimal As Integer
For i =
(binarios.Length - 1) To 0 Step -1
potencia = potencia + 1
bit = Val(binarios(i)) 'obtengo el bit de la
cadena
base = 2 ^ potencia 'elevo el bit a la
potencia
numdecimal = numdecimal + (bit * base)
txtresultado.Text = ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled = False
End Sub
Primero obtenemos el valor del bit y se le aumenta uno a la
posición para seguir ingresando el siguiente número y se redimensiona el
vector.
Para después solo imprimir el valor del vector ya cambiado.
Este botón sirve para hallar el valor en decimal de un
sistema numérico binario
Solamente declaramos las variables que nos servirán como el
bit que se va a transformar la potencia que va a determinar la posición de cada
número la base que guarda el valor de la
posición y donde guardaremos el resultado, en un ciclo for hacemos las
operaciones para hallar el valor en decimal.
Para los siguientes
botones siguen la misma estructura cambiando únicamente el valor de la base y
el nombre de las variables, más sin embargo todo lleva el mismo proceso.
Private
Sub Button2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button2.Click
Dim
bits() As Integer
Dim pos
As Integer = -1
Dim x As Integer =
Val(txtnume.Text)
Dim bit
As Integer
Do While x >= 8
bit = x Mod 8 'se obtiene el bit
x
= Int(x / 8) 'se obtiene el siguiente numero
pos = pos + 1
ReDim Preserve
bits(pos) 'asignar el tamaño del vector
bits(pos) = bit
Loop
'******se almacena el ultimo bit*****************
'**************que se obtiene de lo que halla
quedado************
pos =
pos + 1
ReDim Preserve
bits(pos) 'se crea una nueva posicion
bits(pos)
= x
txtresultado.Text = ""
For i As Integer = pos To 0 Step -1
'MsgBox(bits(i))
txtresultado.Text =
txtresultado.Text & bits(i)
Next
txtnume.Enabled = False
End Sub
Decimal a
octal
Private
Sub Button6_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button6.Click
Dim
octales As String
octales = txtnume.Text()
Dim
potencia As Integer
= -1
Dim
octal As Integer
Dim
base As Integer
Dim
numdecimal As Integer
For i =
(octales.Length - 1) To 0 Step -1
potencia = potencia + 1
octal = Val(octales(i))
base = 8 ^ potencia
numdecimal = numdecimal + (octal * base)
txtresultado.Text = ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled
= False
End Sub
Octal a
decimal
Para el sistema hexadecimal el proceso es el mismo que los
anteriores pero además de modificar el valor exponencial también hay que
cambiar el valor de un número a una letra o de una letra a un número esto lo
logramos con “CASE” en ambos casos.
Hexadecimal
a decimal
Private
Sub Button7_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button7.Click
Dim
hexadecimales As String
hexadecimales = txtnume.Text
Dim
potencia As Integer
= -1
Dim Hex
As String
Dim
base As Integer
Dim
numdecimal As Integer
Dim
numero As Integer
For i =
(hexadecimales.Length - 1) To 0 Step -1
potencia = potencia + 1
Hex = UCase(hexadecimales(i))
Select Case Hex
Case
"A" : numero = 10
Case
"B" : numero = 11
Case
"C" : numero = 12
Case
"D" : numero = 13
Case
"E" : numero = 14
Case
"F" : numero = 15
Case
Else
numero = Val(Hex)
End Select
base = 16 ^ potencia 'elevo el bit a la
potencia
numdecimal = numdecimal + (numero * base)
txtresultado.Text = ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled = False
End Sub
El case nos ayuda a decir que en caso de algo pase algo en
este caso lo utilizamos para cambiar los valores que necesitamos.
Decimal a
hexadecimal
Private
Sub Button3_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button3.Click
Dim
hexa() As String
Dim pos
As Integer = -1
Dim
hexad As String
Dim x As Integer =
Val(txtnume.Text)
Dim hex
As String
Do While x >= 16
hex = x Mod
16
x = Int(x / 16)
pos = pos + 1
Select
Case hex
Case
"10" : hexad = "A"
Case
"11" : hexad = "B"
Case
"12" : hexad = "C"
Case
"13" : hexad = "D"
Case
"14" : hexad = "E"
Case
"15" : hexad = "F"
Case
Else
hexad = hex
End
Select
ReDim
Preserve hexa(pos)
hexa(pos) = hexad
Loop
pos =
pos + 1
Select Case x
Case
"10" : hexad = "A"
Case
"11" : hexad = "B"
Case
"12" : hexad = "C"
Case
"13" : hexad = "D"
Case
"14" : hexad = "E"
Case
"15" : hexad = "F"
Case Else
hexad = x
End Select
ReDim Preserve hexa(pos)
hexa(pos) = hexad
txtresultado.Text = ""
For i As Integer = pos To 0 Step -1
txtresultado.Text =
txtresultado.Text & hexa(i)
txtnume.Enabled = False
Next
End Sub
Para darle mejor acabado al proyecto se le puede poner un
sistema para permitir algunos valores dependiendo de lo que utilicemos por
ejemplo para el sistema binarios solo se utilizaran los números 1 y 0 todo esto
se puede hacer a través de un módulo.
Código del módulo de validación.
Module
valida
Public Function Validacion(ByVal
Tipo As String,
ByVal Caracter As
String) As Char
' N =
solo numeros (enteros)
' T = Solo texto
' U = alfanumerico
' M = Texto en
mayusculas
'el codigo Ascci, por lo que solamente se puede utiizar en
el evento
'KeyPress. Todos detectan el Enter para pasar al siguiente
Txt
Dim CadenaValida As String
Dim Car
As Integer
Car = AscW(Caracter)
Tipo
= UCase(Tipo)
Select Case Tipo
Case "N"
'Si es numerico
CadenaValida = "0123456789"
Case "CVE"
CadenaValida = "123456"
Case "GPO"
CadenaValida = "123"
Case "T"
CadenaValida = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
Case "M"
'Si es mayuscula
Car = Asc(UCase(Chr(Car)))
CadenaValida = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
GoTo Salida
Case "U"
'Si es un alfanumerico
CadenaValida = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789"
Case "TEL"
CadenaValida = "-0123456789"
Case "BIN"
'Si es numerico
CadenaValida = "01"
Case "OCT"
'Si es numerico
CadenaValida = "01234567"
End Select
If Car
= 13 Then 'Si es un
Enter
SendKeys.Send("{Tab}")
ElseIf Car = 32 And
Tipo <> "N" And Tipo <> "R"
Then 'Si es un espacio y no es un entero o real
Validacion
= ChrW(Car)
GoTo
Salida
End If
If Car > 26 Then ' si no es un caracter de control
If InStr(CadenaValida, Chr(Car)) = 0 Then
Car = 0
End
If
End If
Salida:
Validacion = ChrW(Car)
End Function
End
Module
Esto se implementa a través del tetxbox cambiando el evento
a keypress con la siguiente línea de código, la letra que elijamos dependerá de
la letra que corresponda a la cadena que vayamos a implementar.
Private
Sub txtnume_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtnume.KeyPress
If
rabinario.Checked = True Then
e.KeyChar = Validacion("bin", e.KeyChar)
End If
If
radecimal.Checked = True Then
e.KeyChar = Validacion("N",
e.KeyChar)
End If
If
raOctal.Checked = True Then
e.KeyChar = Validacion("oct", e.KeyChar)
End If
If
rahexa.Checked = True Then
e.KeyChar = Validacion("u", e.KeyChar)
End If
End Sub
Para evitar que nuestro sistema falle podemos bloquear los
botones que no se podrán utilizar al elegir un sistema numérico eso se logra
con la siguiente línea de código
Private Sub raOctal_CheckedChanged(ByVal sender As
System.Object, ByVal
e As System.EventArgs)
Handles raOctal.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = False
Button6.Enabled = True
Button7.Enabled = False
End Sub
Private Sub rabinario_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
rabinario.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = True
Button6.Enabled = False
Button7.Enabled = False
End Sub
Private Sub radecimal_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
radecimal.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
End Sub
Private Sub rahexa_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
rahexa.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = True
End Sub
Public
Class Form1
Private Sub Button4_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button4.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button1.Click
Dim
bits() As Integer
Dim pos
As Integer = -1
Dim x As Integer =
Val(txtnume.Text)
Dim bit
As Integer
Do While x >= 2
bit = x Mod 2 'se obtiene el bit
x
= Int(x / 2) 'se obtiene el siguiente numero
pos = pos + 1
ReDim Preserve
bits(pos) 'asignar el tamaño del vector
bits(pos) = bit
Loop
'******se almacena el ultimo bit*****************
'**************que se obtiene de lo que halla
quedado************
pos =
pos + 1
ReDim Preserve
bits(pos) 'se crea una nueva posicion
bits(pos)
= x
txtresultado.Text = ""
For i As Integer = pos To 0 Step -1
'MsgBox(bits(i))
txtresultado.Text =
txtresultado.Text & bits(i)
Next
txtnume.Enabled = False
End Sub
Private Sub Button5_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button5.Click
Dim
binarios As String
binarios = txtnume.Text()
Dim
potencia As Integer
= -1
Dim bit
As Integer
Dim
base As Integer
Dim
numdecimal As Integer
For i =
(binarios.Length - 1) To 0 Step -1
potencia = potencia + 1
bit = Val(binarios(i)) 'obtengo el bit de la
cadena
base = 2 ^ potencia 'elevo el bit a la
potencia
numdecimal = numdecimal + (bit * base)
txtresultado.Text = ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled
= False
End Sub
Private Sub Button2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button2.Click
Dim
bits() As Integer
Dim pos
As Integer = -1
Dim x As Integer =
Val(txtnume.Text)
Dim bit
As Integer
Do While x >= 8
bit = x Mod 8 'se obtiene el bit
x
= Int(x / 8) 'se obtiene el siguiente numero
pos = pos + 1
ReDim Preserve
bits(pos) 'asignar el tamaño del vector
bits(pos) = bit
Loop
'******se almacena el ultimo bit*****************
'**************que se obtiene de lo que halla
quedado************
pos =
pos + 1
ReDim Preserve
bits(pos) 'se crea una nueva posicion
bits(pos)
= x
txtresultado.Text = ""
For i As Integer = pos To 0 Step -1
'MsgBox(bits(i))
txtresultado.Text =
txtresultado.Text & bits(i)
Next
txtnume.Enabled = False
End Sub
Private Sub Button6_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button6.Click
Dim
octales As String
octales = txtnume.Text()
Dim
potencia As Integer
= -1
Dim
octal As Integer
Dim
base As Integer
Dim numdecimal
As Integer
For i =
(octales.Length - 1) To 0 Step -1
potencia = potencia + 1
octal = Val(octales(i))
base = 8 ^ potencia
numdecimal = numdecimal + (octal * base)
txtresultado.Text
= ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled
= False
End Sub
Private Sub Button3_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button3.Click
Dim
hexa() As String
Dim pos
As Integer = -1
Dim
hexad As String
Dim x As Integer =
Val(txtnume.Text)
Dim hex
As String
Do While x >= 16
hex = x Mod
16
x = Int(x / 16)
pos = pos + 1
Select
Case hex
Case
"10" : hexad = "A"
Case
"11" : hexad = "B"
Case
"12" : hexad = "C"
Case
"13" : hexad = "D"
Case
"14" : hexad = "E"
Case
"15" : hexad = "F"
Case
Else
hexad = hex
End
Select
ReDim Preserve hexa(pos)
hexa(pos)
= hexad
Loop
pos = pos + 1
Select Case x
Case
"10" : hexad = "A"
Case
"11" : hexad = "B"
Case
"12" : hexad = "C"
Case
"13" : hexad = "D"
Case
"14" : hexad = "E"
Case
"15" : hexad = "F"
Case Else
hexad = x
End Select
ReDim Preserve hexa(pos)
hexa(pos)
= hexad
txtresultado.Text
= ""
For i As Integer = pos To 0 Step -1
txtresultado.Text =
txtresultado.Text & hexa(i)
txtnume.Enabled = False
Next
End Sub
Private Sub Button7_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button7.Click
Dim
hexadecimales As String
hexadecimales = txtnume.Text
Dim
potencia As Integer
= -1
Dim Hex
As String
Dim
base As Integer
Dim
numdecimal As Integer
Dim
numero As Integer
For i =
(hexadecimales.Length - 1) To 0 Step -1
potencia = potencia + 1
Hex = UCase(hexadecimales(i))
Select Case Hex
Case
"A" : numero = 10
Case
"B" : numero = 11
Case
"C" : numero = 12
Case
"D" : numero = 13
Case "E" : numero = 14
Case
"F" : numero = 15
Case
Else
numero = Val(Hex)
End Select
base = 16 ^ potencia 'elevo el bit a la
potencia
numdecimal = numdecimal + (numero * base)
txtresultado.Text = ""
Next
'MsgBox(numdecimal)
txtresultado.Text = txtresultado.Text & numdecimal
txtnume.Enabled
= False
End Sub
Private Sub Button8_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button8.Click
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
End Sub
Private Sub rabinario_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
rabinario.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = True
Button6.Enabled = False
Button7.Enabled = False
End Sub
Private Sub radecimal_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
radecimal.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
End Sub
Private Sub rahexa_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
rahexa.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = True
End Sub
Private Sub txtnume_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtnume.KeyPress
If
rabinario.Checked = True Then
e.KeyChar = Validacion("bin", e.KeyChar)
End If
If
radecimal.Checked = True Then
e.KeyChar = Validacion("N",
e.KeyChar)
End If
If
raOctal.Checked = True Then
e.KeyChar = Validacion("oct", e.KeyChar)
End If
If
rahexa.Checked = True Then
e.KeyChar = Validacion("u", e.KeyChar)
End If
End Sub
Private Sub raOctal_CheckedChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
raOctal.CheckedChanged
txtnume.Clear()
txtresultado.Clear()
txtnume.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button5.Enabled = False
Button6.Enabled = True
Button7.Enabled = False
End Sub
End Class
No hay comentarios:
Publicar un comentario