Results 1 to 10 of 10

Thread: vb6 Convert a value beyond the long range to a hexadecimal number?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    vb6 Convert a value beyond the long range to a hexadecimal number?

    I want to convert a decimal number to a hexadecimal number program, to convert a value beyond the long range to a hexadecimal number, is there a way, 20200430110900, how to become 125F477608B4

    Code:
    Public Function DEC_to_HEX(Dec As Currency) As String
        Dim a As String
        DEC_to_HEX = ""
        Do While Dec > 0
            a = CStr(Dec - 16 * Int(Dec / 16))
            Select Case a
            Case "10": a = "A"
            Case "11": a = "B"
            Case "12": a = "C"
            Case "13": a = "D"
            Case "14": a = "E"
            Case "15": a = "F"
            End Select
            DEC_to_HEX = a & DEC_to_HEX
            Dec = Int(Dec / 16)
        Loop
    End Function
    Code:
    Function LongToHexStr(LongV As String) As String
        Dim js As Object
        'dim Js As ScriptControl :Set js = New ScriptControl
        Set js = CreateObject("ScriptControl")
        js.Language = "javascript"
        js.AddCode "var num = " & LongV & ";"
        LongToHexStr = js.Eval("num.toString(16).toUpperCase()")
        Set js = Nothing
    End Function
    
    Function LongToHexStr(LongV As String) As String
        Dim js As Object: Set js = CreateObject("ScriptControl"): js.Language = "javascript"
        LongToHexStr = js.Eval("var num=" & LongV & ";num.toString(16).toUpperCase()")
    End Function
    Last edited by xiaoyao; Apr 30th, 2021 at 01:29 AM.

  2. #2

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    How to use snwprintf1 can be found in the AddIn thread by The Trick:
    https://www.vbforums.com/showthread....=1#post5509861

    This seems to work too:
    Code:
    Private Sub Form_Load()
      Dim c As Currency
      c = 20200430110900#
      Debug.Print CurToHex(c)
    End Sub
    
    Private Function CurToHex(ByVal cValue As Currency) As String
      Dim c As Currency, r As Long
      
      Do While cValue > 0
        c = cValue / 16
        r = 16 * (c - Int(c))
        CurToHex = Hex$(r) & CurToHex
        cValue = Int(c)
      Loop
    End Function
    Based on the explanation on this site:
    https://www.rapidtables.com/convert/...al-to-hex.html
    Last edited by Arnoutdv; Apr 30th, 2021 at 04:57 AM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,057

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    once you start working with 64 bit numbers its nice to have a complete lib

    https://github.com/dzzie/libs/tree/master/vb6_utypes

    Code:
    Private Sub Form_Load()
        Dim u As New ULong64
        u.fromString "20200430110900", mUnsigned
        Debug.Print u.toString() ' 125F`477608B4
    End Sub
    Code:
    'ULong64
    
    'Variables
    Dim hLib As Long
    Public use0x As Boolean
    Public useTick As Boolean
    Public padLeft As Boolean
    Public rawValue As Currency   'this is the default property of the class
    Public mode As modes
    
    'Properties
    Property lo() As Long
    Property hi() As Long
    Property isNegBitSet() As Boolean
    Property is32BitSafe() As Boolean
    Property MAX_UNSIGNED64() As String
    Property MIN_SIGNED64() As String
    Property MAX_SIGNED64() As String
    Property Value()
    
    'Methods
    Function rol32(operand2) As ULong64
    Function lessThanEqual(operand2) As Boolean
    Function greaterThanEqual(operand2) As Boolean
    Function lessThan(operand2) As Boolean
    Function greaterThan(operand2) As Boolean
    Function lshift(operand2) As ULong64
    Function rshift(operand2) As ULong64
    Function or_(operand2) As ULong64
    Function and_(operand2) As ULong64
    Function xor_(operand2) As ULong64
    Function modulus(operand2) As ULong64
    Function multiply(operand2) As ULong64
    Function divide(operand2) As ULong64
    Function subtract(operand2) As ULong64
    Function add(operand2) As ULong64
    Function raw_op(operand2, operation As op) As ULong64
    Sub GetLongs(Optional ByRef hi As Long, Optional ByRef lo As Long)
    Sub SetLongs(Optional hi As Long, Optional lo As Long)
    Function fromString(ByVal s As String, Optional m As modes = mHex) As Boolean
    Function toString(Optional m As modes = mHex) As String
    Function setVal(v) As ULong64
    
    'Variables hLib use0x useTick padLeft rawValue mode 
    'Properties lo hi isNegBitSet is32BitSafe MAX_UNSIGNED64 MIN_SIGNED64 MAX_SIGNED64 Value 
    'Methods rol32 lessThanEqual greaterThanEqual lessThan greaterThan lshift rshift or_ and_ xor_ modulus multiply divide subtract add raw_op GetLongs SetLongs fromString toString setVal

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    Except that a Currency is not "decimal" but instead a signed binary integer scaled by 10000. Why do people imagine binary values to be "decimal?" Or is this just more language imprecision?

    Even a VT_DECIMAL is not "decimal" but more like a Currency which carries a power of 10 ("digits") scaling factor.

    There is no "decimal" type at all.

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    dilettante, your post is useless. who cares about semantics, in the end its about the practical use of the type.
    otherwise everything is just binary, 0 and 1.
    also you can use the type to be octal if you wish. so you decide what the type is.

    and talking about binary. you can convert a decimal (0-9) into binary (0-1) and after that convert it into hexadecimal (00-0F)

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    I think Dilettante means the correct terms should be a representation in Base 10 or in Base 16.
    But like many others, I'm also not a native English speaker, so we make to best of it to understand each other.

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    I get that Arnoutdv, and if you are discussing technicalities, he's correct about that.
    but he's also wrong, as in the end its binary and if we are going even deeper its about circuits, transistors and electronic signals, and if we go even deeper, its atoms and elementary particles and mass, charge and 'spin and if we go deeper well... now its just theory.
    for me its about context, and nobody asked for a lesson of what is a type, but how to convert decimal (base 10) into hex (base 16) for values above what long type can hold.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    It isn't semantics, it's about understanding computers.

    This sort of "I think it is decimal, it's my party and I'll cry if I want to" is why people go so wrong. For example getting wrapped around the axle by the limitations of floating-point data types and operations.

    There are numeric data types and operations that really are decimal, we just don't have them in most Windows programming languages.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: vb6 Convert a value beyond the long range to a hexadecimal number?

    Quote Originally Posted by dz32 View Post
    once you start working with 64 bit numbers its nice to have a complete lib

    https://github.com/dzzie/libs/tree/master/vb6_utypes

    Code:
    Private Sub Form_Load()
        Dim u As New ULong64
        u.fromString "20200430110900", mUnsigned
        Debug.Print u.toString() ' 125F`477608B4
    End Sub
    Code:
    'ULong64
    
    'Variables
    Dim hLib As Long
    Public use0x As Boolean
    Public useTick As Boolean
    Public padLeft As Boolean
    Public rawValue As Currency   'this is the default property of the class
    Public mode As modes
    
    'Properties
    Property lo() As Long
    Property hi() As Long
    Property isNegBitSet() As Boolean
    Property is32BitSafe() As Boolean
    Property MAX_UNSIGNED64() As String
    Property MIN_SIGNED64() As String
    Property MAX_SIGNED64() As String
    Property Value()
    
    'Methods
    Function rol32(operand2) As ULong64
    Function lessThanEqual(operand2) As Boolean
    Function greaterThanEqual(operand2) As Boolean
    Function lessThan(operand2) As Boolean
    Function greaterThan(operand2) As Boolean
    Function lshift(operand2) As ULong64
    Function rshift(operand2) As ULong64
    Function or_(operand2) As ULong64
    Function and_(operand2) As ULong64
    Function xor_(operand2) As ULong64
    Function modulus(operand2) As ULong64
    Function multiply(operand2) As ULong64
    Function divide(operand2) As ULong64
    Function subtract(operand2) As ULong64
    Function add(operand2) As ULong64
    Function raw_op(operand2, operation As op) As ULong64
    Sub GetLongs(Optional ByRef hi As Long, Optional ByRef lo As Long)
    Sub SetLongs(Optional hi As Long, Optional lo As Long)
    Function fromString(ByVal s As String, Optional m As modes = mHex) As Boolean
    Function toString(Optional m As modes = mHex) As String
    Function setVal(v) As ULong64
    
    'Variables hLib use0x useTick padLeft rawValue mode 
    'Properties lo hi isNegBitSet is32BitSafe MAX_UNSIGNED64 MIN_SIGNED64 MAX_SIGNED64 Value 
    'Methods rol32 lessThanEqual greaterThanEqual lessThan greaterThan lshift rshift or_ and_ xor_ modulus multiply divide subtract add raw_op GetLongs SetLongs fromString toString setVal
    Very good, I will test it when I have time, thank you for your warm help
    Learn a lot in this forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width