直接转换就行了,例如: Private Sub Form_Load() Dim h As String Dim b As Byte h = &HFF Debug.Print TypeName(h); Tab(3); h b = CByte(Val(h)) Debug.Print TypeName(b); Tab(3); bh = "&H " & Hex(b) Debug.Print TypeName(h); Tab(3); hEnd Sub
Public Function test() Dim strHex As String Dim bytX As Byte
bytX = 207 Debug.Print Hex(bytX) 'Byte>HEX 字符 End Function
6楼的作废,用这个:Public Function Byt2Hex(bytX As Byte) As String Byt2Hex = Hex(bytX) End FunctionPublic Function Hex2Byt(strHex As String) As Byte Debug.Assert Len(strHex) <= 2 Hex2Byt = CByte("&H" & strHex) End Function
10进制转16进制: HEX函数 16进制转10进制: VAL函数
LZ提示下,是什么样的DLL这么古怪。
其实特别简单,根本没有楼上几位说得那么复杂,我给段程序,你单步跟踪看看就明白怎么转换了. Dim B As Byte Dim S As String
B = "&H23"'这个也行 B = &H23'这个也行 S = "&H" & Hex(B) B = S'这也行
{
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 | _b1);
return ret;
}
public static byte[] HexString2Bytes(String src) //十六进制转byte
{
byte[] tmp = src.getBytes();
byte[] ret = new byte[tmp.length/2];
for(int i=0; i<ret.length; i++){
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}
public static String byte2HexString(byte[] b) {
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] newChar = new char[b.length * 2];
for(int i = 0; i < b.length; i++) {
newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
newChar[2 * i + 1] = hex[b[i] & 0xf];
}
return new String(newChar);
}
Private Sub Form_Load()
Dim h As String
Dim b As Byte
h = &HFF
Debug.Print TypeName(h); Tab(3); h
b = CByte(Val(h))
Debug.Print TypeName(b); Tab(3); bh = "&H " & Hex(b)
Debug.Print TypeName(h); Tab(3); hEnd Sub
Dim strHex As String
Dim bytX As Byte
strHex = "CF"
Debug.Print CByte("&H" & strHex) 'HEX字符 > Byte
bytX = 207
Debug.Print Hex(bytX) 'Byte>HEX 字符
End Function
Byt2Hex = Hex(bytX)
End FunctionPublic Function Hex2Byt(strHex As String) As Byte
Debug.Assert Len(strHex) <= 2
Hex2Byt = CByte("&H" & strHex)
End Function
16进制转10进制: VAL函数
LZ提示下,是什么样的DLL这么古怪。
其实特别简单,根本没有楼上几位说得那么复杂,我给段程序,你单步跟踪看看就明白怎么转换了.
Dim B As Byte
Dim S As String
B = "&H23"'这个也行
B = &H23'这个也行
S = "&H" & Hex(B)
B = S'这也行
16进制转10进制: VAL函数
我猜应该就是字符串,字符串保存的是类似“3B4CEEFC”之类的字符吧。
你说得是BCD码和ASCII码之间的转换 那么 byte = Asc(Hex(number)) 当然number从0-15
很多人对进制的理解都是似是而非。实际上,计算机存储和传输的数字,只有二进制,也就是 0 和 1。这大概你诸位读《十万个为什么》的时候就已经读到过,只是没有真正理解。此外,数字有大小端存储的区别,浮点数有特定的格式。这些是后话,跟现在的话题无关。至于什么八进制、十进制、十六进制,那都是为了方便人机交互,依靠编码转换搞出来的。目前,在 VB 中,十六进制就是字符串,它不是真正意义上的数值。如果没有限定条件,“如何在 Byte 和 Hex 之间相互转换”几乎可以写一本书。也因为如此,我一直没有回帖。