我想把一个长整形变量表示为十六进制数 并且格式为 00 00 00 00 这样strCardData = Format(Hex(TempCardNum), "00 00 00 00 ")我这样写的话 ,0到9都没有问题但是到10的时候就不对了本来应该显示00 00 00 0A的 但是实际只显示A谁能告诉我应该怎么做? 

解决方案 »

  1.   

    用代码自己补0,format不是这样用的
      

  2.   

        a = 100
        b = Format(Right$(String$(8, Asc(0)), 8 - Len(Hex$(a))) & Hex$(a), "00 00 00 00")
        Debug.Print b
      

  3.   

    strCardData = Right("0000000" & Hex(TempCardNum),8)
      

  4.   

    测试代码:(可以写在窗体里)
    Text1.Text = LongHEX(65535)下列代码必须放在“模块”里,而不能写在窗体里。
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Public Function LongHEX(ByRef pValue As Long) As Byte()
      Dim tOutBytes() As Byte
      Dim tOutBytes_Index As Byte
      
      Dim tValueBytes() As Byte
      Dim tValueBytes_Index As Byte
      
      Dim tHEX_Bit As Byte
      Dim tHEX_Hex As Boolean
      
      ReDim tOutBytes(0 To 23)
      ReDim tValueBytes(0 To 3)
      
      CopyMemory tValueBytes(0), pValue, 4
      
      For tValueBytes_Index = 0 To 3
        tOutBytes_Index = tValueBytes_Index * 6
        
        tHEX_Bit = tValueBytes(tValueBytes_Index) \ 16: tHEX_Hex = tHEX_Bit > 9
        
        tOutBytes(tOutBytes_Index) = tHEX_Bit + 48 + (tHEX_Hex And 7)
        
        tHEX_Bit = tValueBytes(tValueBytes_Index) Mod 16: tHEX_Hex = tHEX_Bit > 9
        
        tOutBytes(tOutBytes_Index + 2) = tHEX_Bit + 48 + (tHEX_Hex And 7)
        
        tOutBytes(tOutBytes_Index + 4) = &H20
      Next
      
      LongHEX = tOutBytes()
    End Function
      

  5.   

    专门为你写了一个代码:http://download.csdn.net/source/1681799
    不要见笑:)
      

  6.   

    在10的HEX表示应该是0A 00 00 00,因为这是内存中实际的存储形式。如果你想把它显示为00 00 00 0A,那么你要这样做:
    将函数For循环里的:tOutBytes_Index = tValueBytes_Index * 6
    改为:tOutBytes_Index = (3 - tValueBytes_Index) * 6
      

  7.   

    vb中的十六进制转换方式不象vc那么灵活,提供的函数有时候觉得不是很好用,最好的办法是自己基于已经了解的vb函数写自己的函数。0~9可以就只用那个,超过就自己写。
      

  8.   

    '是这个意思吗?
    Option Explicit
    Private Sub Command1_Click()
        Debug.Print FormatDisplayHex(10)
    End SubFunction FormatDisplayHex(n As Long)
        Dim i As Integer, Temp As String
        For i = 1 To Len(CStr(Hex(n)))
            Temp = Temp & "0" & Mid(CStr(Hex(n)), i, 1) & Space(1)
        Next
        For i = 1 To 4 - Len(CStr(Hex(n))) '不足4位
            Temp = "00 " & Temp
        Next
        FormatDisplayHex = Temp
    End Function
      

  9.   

    大家真是太强大了
    那么我把我最后使用的方法也跟大家分享一下啦!'''Function GeShiHua(St As String) As String 
    '''    Dim Str1 As String, Str2 As String, Str3 As String 
    ''' 
    '''    If Len(St) > 8 Then 
    '''        GeShiHua = "" 
    '''        Exit Function 
    '''    End If 
    '''    Str1 = "00000000" 
    '''    Str2 = Left(Str1, 8 - Len(St)) & St 
    '''    For i = 1 To 8 Step 2 
    '''        Str3 = Str3 & Mid(Str2, i, 2) & " " 
    '''    Next 
    '''    GeShiHua = Trim(Str3) 
    '''End Function 调用这个方法就可以了。