VB将十六制数转换成二进制数需要编写自定义函数解决。
创新互联服务项目包括凤阳网站建设、凤阳网站制作、凤阳网页制作以及凤阳网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,凤阳网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到凤阳省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
Option Explicit
Dim sBin
Dim sHex
Dim sHex1
Dim BinData
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim yTemp() As Byte
Public Function HexToBin(ByVal sHex As String) As String
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
BinData = sBin
End Function
Private Sub Command1_Click()
sHex = Text1
Call HexToBin(sHex)
Text2 = BinData
End Sub
为了表示对你的尊重,我就先不写代码了.
主要有3种思路
1、通用的,把
16进制
字符串转换成对应的进制表示(譬如先用val()转成整形,然后再换进制,换进制的算法你会吧?),然后用mid截取需要的段。
2、只适用于问题1。直接用iif(val(s)
and
2^(i-1),1,0)
3、适用于2与16进制之间的转换:记住4位
2进制数
对应1位16进制数,具体算法自己想咯
==================================
1、er...首先请教hen是有什么功能的函数?
2、关于负数:当最高位(bit)为1时,用
10进制
表示就是负数.你可以注意不要让最高位为1,或者用long型,或者用currency,或者自己写一个类。
3、速度的话我觉得不会太慢吧,就算你有10m的数据也至多不过几秒?未必要另外建立一个数组,这要看你的需求。非要用数组的话,注意不要用redim。你想要速度的话,可以用第2种方法。或者改一下,由于4位2进制对1位16进制,只要抽出16进制中需要判断的那一位加以处理就好了。更发烧一点的话,不要用val,自己写一个有针对性的;iif也不要用,用if代替;
乘方
什么的都用加减代替;实在不够快写汇编。
Function siliuTo2(ByVal su As String) As String
'16进制转换为2进制
For i = 1 To Len(su)
n = Mid(su, i, 1)
Select Case n
Case 0
siliuTo2 = siliuTo2 "0000"
Case 1
siliuTo2 = siliuTo2 "0001"
Case 2
siliuTo2 = siliuTo2 "0010"
Case 3
siliuTo2 = siliuTo2 "0011"
Case 4
siliuTo2 = siliuTo2 "0100"
Case 5
siliuTo2 = siliuTo2 "0101"
Case 6
siliuTo2 = siliuTo2 "0110"
Case 7
siliuTo2 = siliuTo2 "0111"
Case 8
siliuTo2 = siliuTo2 "1000"
Case 9
siliuTo2 = siliuTo2 "1001"
Case "A"
siliuTo2 = siliuTo2 "1010"
Case "B"
siliuTo2 = siliuTo2 "1011"
Case "C"
siliuTo2 = siliuTo2 "1100"
Case "D"
siliuTo2 = siliuTo2 "1101"
Case "E"
siliuTo2 = siliuTo2 "1110"
Case "F"
siliuTo2 = siliuTo2 "1111"
Case Else
siliuTo2 = "数据错误!"
Exit Function
End Select
Next
siliuTo2 = Format(siliuTo2, "0") '去除前面的0
End Function
首先引入System.IO命名空间
Imports System.IO
然后使用文件流来读入数组:
Dim bytes() As Byte
Using fs As New FileStream(文件路径,FileMode.Open)
ReDim bytes(fs.Length-1)
fs.Read(bytes,0,fs.Length)
fs.Close()
End Using
这样bytes就是整个文件的所有字节了
从字节生成Image:
Dim img As Image = Image.FromStream(New MemoryStream(bytes))
img就是图片了
Option Explicit
Dim Jz As Integer, K As Boolean
Private Sub Command1_Click()
Pic.Cls
If K Then Jz = Val(Qtjz.Text)
If Pd(Text1.Text, Jz) Then Pic.Print "选择的进制与数不符": Exit Sub
If Jz = 0 Then Pic.Print "请先输入进制" Else Pic.Print Zh(Text1.Text, Jz)
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
Private Sub Option1_Click(Index As Integer)
K = False
Select Case Index
Case 0
Jz = 2
Case 1
Jz = 8
Case 2
Jz = 10
Case 3
Jz = 16
Case 4
K = True
Qtjz.Text = ""
Qtjz.SetFocus
End Select
If Index 4 Then Command1.SetFocus
End Sub
Private Sub Qtjz_Click()
Qtjz.Text = ""
End Sub
Private Sub Text1_Click()
Text1.Text = ""
End Sub
Private Function Zh(a As String, b As Integer) As Integer
Dim c As String, i, d As Integer
a = LCase(a)
For i = 1 To Len(a)
c = Mid(a, Len(a) + 1 - i, 1)
If Asc(c) 96 Then d = Asc(c) - 87 Else d = Val(c)
Zh = Zh + d * b ^ (i - 1)
Next i
End Function
Private Function Pd(a As String, b As Integer) As Boolean
Dim c As String, i, d As Integer
Pd = False
For i = 1 To Len(a)
c = Mid(a, Len(a) + 1 - i, 1)
If Asc(c) 96 Then d = Asc(c) - 87 Else d = Val(c)
If d b - 1 Then Pd = True: Exit Function
Next i
End Function