+ Reply to Thread
Results 1 to 7 of 7

Code to convert from Hexa to Binary Excel VBA a large number

  1. #1
    Registered User
    Join Date
    08-20-2019
    Location
    Fortaleza
    MS-Off Ver
    2013
    Posts
    1

    Code to convert from Hexa to Binary Excel VBA a large number

    I'm trying to convert 6E29210100 (hexa) to binary. With smaller hexa numbers, the code works fine. and I don't know why. I would like it was working to 10 characters at least.


    Code returns: 00101001001000010000000100000000
    Right answer: *0110111000101001001000010000000100000000‬

    Please, have a look in short version code bellow (full in attachment):

    Please Login or Register  to view this content.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: Code to convert from Hexa to Binary Excel VBA a large number

    Hmm I think it's due to limitation of VAL().

    You can use code in text file attached (since Securi is blocking code from being posted as is...), that I found in my reference library. Based on very old post in Mr.Excel by pgc01.

    Basically using list of Hex/Bin conversion. Uses Replace function to replace each hex char with bin string. And checks that nothing other than 0/1 exists in final string.
    Attached Files Attached Files
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    Hi CK76 !

    Tip for Securi issue : when you use two consecutive Replace VBA function use a $ like Replace$(
    or a space between both, no matter as VBE removes automatically any space not necessary :

    PHP Code: 
    Function lgHex2Bin(strHex As String) As String
        Dim hbArr
        Dim i 
    As Long
        hbArr 
    = Array( _
                
    Array("0""0000"), _
                
    Array("1""0001"), _
                
    Array("2""0010"), _
                
    Array("3""0011"), _
                
    Array("4""0100"), _
                
    Array("5""0101"), _
                
    Array("6""0110"), _
                
    Array("7""0111"), _
                
    Array("8""1000"), _
                
    Array("9""1001"), _
                
    Array("A""1010"), _
                
    Array("B""1011"), _
                
    Array("C""1100"), _
                
    Array("D""1101"), _
                
    Array("E""1110"), _
                
    Array("F""1111"))

        For 
    0 To UBound(hbArr) - 1
            strHex 
    Replace(strHexhbArr(i)(0), hbArr(i)(1), , , 1)
        
    Next

        
    If Len(ReplaceReplace(strHex"0"""), "1""")) > 0 Then
            lgHex2Bin 
    "Not a valid Hex"
        
    Else
            
    lgHex2Bin strHex
        End 
    If
    End Function 

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: Code to convert from Hexa to Binary Excel VBA a large number

    Ah thanks Marc didn’t know that one.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    It's not only a Val limitation as with CDec instead that raises a capacity error #6 so it's over the Excel capacity !

    But just using the Hex2Bin worksheet function :

    PHP Code: 
    Function HexBin$(H$)
       
    Dim VC&
           
    Application.Hex2Bin(H)
        If 
    CVErr(2036Then
                V 
    ""
            
    For 1 To Len(H)
                
    Application.Hex2Bin(Mid$(HC1), 4)
            
    Next
        End 
    If
            If 
    IsError(VThen HexBin "#" CLng(V) Else HexBin V
    End 
    Function

    Sub Demo1()
        
    Dim H$
            
    "6E29210100"
            
    MsgBox "Bin = " HexBin(H), , "Hex = " H
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 08-20-2019 at 09:10 PM.

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Code to convert from Hexa to Binary Excel VBA a large number

    Here is a function I developed quite a number of years ago...
    Please Login or Register  to view this content.
    Note 1
    -----------------
    There is no practical limit to the length of the first argument to this function (the actual limit is the binary output string which is limited to the maximum length of a VBA text string).

    Note 2
    -----------------
    The optional WithBlanks argument, if set to True, will return the Binary string with a blank space every four Binary digits.
    Last edited by Rick Rothstein; 08-20-2019 at 10:46 PM.

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Code to convert from Hexa to Binary Excel VBA a large number

    For those who might be interested in the ability of my function above to work with huge numbers, I posted my function along with several other conversion functions (plus Fibonacci and Factorial functions too) over in the Tip and Tutorials sub-forum. Here is the link to the my initial thread message over there...

    https://www.excelforum.com/tips-and-...ml#post5181689

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to I convert the split binary bit to hex number
    By u24c02 in forum Excel General
    Replies: 8
    Last Post: 03-18-2017, 10:01 AM
  2. Convert text to number on large, ambiguous selection
    By bbrunof in forum Excel General
    Replies: 1
    Last Post: 03-04-2014, 01:32 PM
  3. Replies: 2
    Last Post: 05-28-2012, 08:56 PM
  4. Convert Text to Number - Large Database
    By supplychain.dan in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-30-2009, 08:19 PM
  5. hexa code
    By rafiz2000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-19-2007, 01:42 AM
  6. [SOLVED] large binary numbers
    By Barb Reinhardt in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM
  7. converting from lotus 123-is there a way to convert a large number
    By flux blocker in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-15-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1