vb 编程将十进制数转换成十六进制数

如题所述

1、运行“Microsoft Visual Studio 2010”。

2、”vs“的窗口弹出后,找到菜单栏,鼠标左键单击标题为”文件(f)“的选项。

3、在弹出的列表中鼠标左键单击标题为”新建项目(p)“的项。

4、在弹出的标题为”新建项目“,鼠标左键选择标题为”Visual Basic“项,在选择标题为”WIndows“的项。再在右边的列表中选择标题为”Windows 窗体应用程序“。

5、在一个背景为”白色"的窗口,且标题为“ForM1”的窗口中的空白处鼠标左键双击。

6、在新切换到的“代码编辑页面”。

7、再在 “Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load”下写代码。就可以了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-03

Private Sub Form_Click()

Dim Dec As Integer,Base As Integer

Dim Decr(30)As Integer

Dim strDecR As String*30

Dim strBase As String*16

Dim b As Integer,n As Integer

strBase="0123456789ABCDE"

Dec=Val(Text1.Text)

Base=Val(Text2.Text)

If Base<2 Or Base>16 Then

res=MsgBox("进制超出范围",vbRetryCancel)

If res=vbRetry Then

Text1.Text=""

Text1.SetFocus

Else

End

End If

End If

n=0

Do While Dec<>0

Decr(n)=Dec Mod Base

Dec=Dec\Base

n=n+1

Loop

strDecR=""

n=n-1

Do While n>=0

b=Decr(n)

strDecR=RTrim(strDecR)+Mid(strBase,b+1,1)

n=n-1

Loop

Label3.Caption=Text1.Text&"转换为"&Text2.Text&"进制后为:"

Text3.Text=strDecR

End Sub

Private Sub Form_Load()

Label1.Caption="十进制数"

Label2.Caption="进制"

Label3.Caption="以下是转换结果:"

Text1.Text=""

Text2.Text=""

Text3.Text=""

End Sub

扩展资料:

while语句若一直满足条件,则会不断的重复下去。但有时,需要停止循环,则可以用下面的三种方式:

一、在while语句中设定条件语句,条件不满足,则循环自动停止。

如:只输出3的倍数的循环;可以设置范围为:0到20。

二、在循环结构中加入流程控制语句,可以使用户退出循环。

1、break流程控制:强制中断该运行区内的语句,跳出该运行区,继续运行区域外的语句。

2、continue流程控制:也是中断循环内的运行操作,并且从头开始运行。

三、利用标识来控制while语句的结束时间。

本回答被网友采纳
第2个回答  2018-06-21

此题,重点测试的是算法设计,不宜使用VB系统定义的函数Hex()。再说Hex()函数只能将十进制数转化为十六进制整数,而忽略十进制数的小数部分,例如Hex(123.652)=7B。

以下程序解决了Hex()函数不能处理小数部分的问题。但我们知道很多十进制小数不能转化为有限位十六进制小数,此时,本程序保留8位十六进制小数,简单修改本程序,即可改变保留的位数。

以下是程序代码:

Dim Flag As Boolean

Private Sub Command1_Click()
  Dim strArray(2) As String, valHexIntegerPart() As Integer, valHexDecimalPart() As Integer
  Text2.Text = ""
  intarray = Split(Text1.Text, ".")
  intIntegerPart = Val(intarray(0))
  If UBound(intarray) = 1 Then
    intDecimalPart = Val("0." + intarray(1))
  Else
    intDecimalPart = 0
  End If
  ReDim valHexIntegerPart(Len(intarray(0))), valHexDecimalPart(8)
  Debug.Print intIntegerPart, intDecimalPart
  Debug.Print Hex(intIntegerPart)
  Do Until intIntegerPart = 0
    valHexIntegerPart(k) = intIntegerPart Mod 16
    intIntegerPart = intIntegerPart \ 16
    k = k + 1
  Loop
  For i = k - 1 To 0 Step -1
    Select Case valHexIntegerPart(i)
      Case 0 To 9
        Text2.Text = Text2.Text + CStr(valHexIntegerPart(i))
      Case 10 To 15
        Text2.Text = Text2.Text + Chr(valHexIntegerPart(i) + 55)
    End Select
  Next i
  k = 0
  Do Until intDecimalPart = 0
    valHexDecimalPart(k) = Int(intDecimalPart * 16)
    intDecimalPart = intDecimalPart * 16 - Int(intDecimalPart * 16)
    If k >= 8 Then Exit Do
    k = k + 1
  Loop
  Text2.Text = Text2.Text + "."
  For i = 0 To k - 1
    Select Case valHexDecimalPart(i)
      Case 0 To 9
        Text2.Text = Text2.Text + CStr(valHexDecimalPart(i))
      Case 10 To 15
        Text2.Text = Text2.Text + Chr(valHexDecimalPart(i) + 55)
    End Select
  Next i
End Sub

Private Sub Command2_Click()
  Text1.Text = ""
  Text2.Text = ""
  Text1.SetFocus
  Flag = False
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
  If Not Flag Then
    If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) And (KeyAscii <> 8) And (KeyAscii <> 46) Then
      KeyAscii = 0
      Beep
    End If
  Else
    If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) And KeyAscii <> 8 Then
      KeyAscii = 0
      Beep
    End If
  End If
  If KeyAscii = 46 Then
    Flag = True
  End If
End Sub

以下是运行界面:

第3个回答  2017-08-28
Private Sub Command1_Click()
n = Val(InputBox("输入要转换的十进制整数:", "十进制转十六进制", "16"))
m = n
x = ""
Do While n >= 16
a = n Mod 16
Select Case a
Case Is < 10
a = a
x = a & x
Case Is = 10
a = "A"
x = a & x
Case Is = 11
a = "B"
x = a & x
Case Is = 12
a = "C"
x = a & x
Case Is = 13
a = "D"
x = a & x
Case Is = 14
a = "E"
x = a & x
Case Is = 15
a = "F"
x = a & x
End Select
n = n \ 16
Loop
Select Case n
Case Is < 10
n = n
Case Is = 10
n = "A"
Case Is = 11
n = "B"
Case Is = 12
n = "C"
Case Is = 13
n = "D"
Case Is = 14
n = "E"
Case Is = 15
n = "F"
End Select
b = x
x = n & x
MsgBox m & "换成十六进制数字是:" & x & " " & Chr(10) & "a为:" & a & " " & Chr(10) & "n为:" & n & " " & Chr(10) & "x为:" & b '输出a,n,b是为了检测方便
End Sub

第4个回答  2016-06-30
使用hex()函数即可:
x=hex(100)