登录
首页 串口通信 串口通信
回帖 发帖
正文

主题:請教各位大俠在VB中怎樣將一個十六進制數轉換成十進制數

点击:5292 回复:4

在編寫PC与 PLC上位机通訊時 ﹐ 遇到一個問題 ﹐ 便 是 VB中怎樣將一個十六進制數轉換成十進制數﹐請各位大俠多多指教。
03-08-13 09:12
用clong或cint函数
jhonhwa@sina.com
03-08-13 17:24
Sub Form_Load ()
Dim x As String
Dim y As Variant
x = "fffe"
y = CLng("&H" & x)
If y < 0 Then y = y + 65536    ' returns 65534

MsgBox y
End Sub
* Converting a string to an integer: Cal Stover
Dim SomeVariable as Integer
SomeVariable = CInt(Label2.Caption) + 100
Dim SomeVariable as Single
SomeVariable = CSng(Val(Label2.Caption) + 100)
* convert a number in Hexadecimal to Binary -chris
A very fast conversion from hex to binary can be done with a sixteen
element look-up table - a single hex digit converts to four binary
digits.  So:
Function Hex2Bin$(HexValue$)
 CONST BinTbl ="0000000100100011010001010110011110001001101010111100110111101111"
 Dim X, Work$
 Work$ = ""
 For X = 1 to Len(HexValue$)
   Work$ = Work$ + Mid$(BinTbl, (Val("&h" + Mid$(HexValue$, X, 1) - 1) * 4 + 1, 4)
 Next
 Hex2Bin$ = Work$
End Function
03-08-16 17:48
普通的十六进制 -> 十进制变换(带符号数):
HEX2DEC = Val("&H"  &  "89AB")
强制将一个短整型的十六进制数变为无符号十进制数(声明十六进制数是长整型):
HEX2DEC = Val( "&H"  &  "89AB"  &  "&" )
03-08-16 18:17
謝 謝 樓 上 的 几 位 朋 友 ﹐ 你 們 的 方 法 都 非 常 好 ﹐ 幫 我 解 決 了 不 小 問 題 。 非 常 感 謝 ﹗
03-08-18 12:29

工控新闻

更多新闻资讯