14 digit Hex to decimal in Excel











up vote
0
down vote

favorite












I have 14 digit Hexadecimal numbers in Excel which I want to convert to a decimal number.



For example in C2 the number is 0438E96A095180 and that has to be 1188475064373632 in decimal.



I have tried a module in VBA but that is not working:



' Force explicit declaration of variables
Option Explicit

' Convert hex to decimal
' In: Hex in string format
' Out: Double
Public Function HexadecimalToDecimal(HexValue As String) As Double

' If hex starts with 0x, replace it with &H to represent Hex that VBA will understand
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = CDec(ModifiedHexValue)

End Function


With that I get the decimal number 1188475064373630 instead of 1188475064373632.



What am I doing wrong?










share|improve this question
























  • You might use the CInt function.
    – harrymc
    Nov 21 at 11:07










  • Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
    – Ben
    Nov 21 at 11:24












  • Try to use the variant datatype instead of Double.
    – harrymc
    Nov 21 at 11:34










  • Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
    – Ron Rosenfeld
    Nov 21 at 12:10















up vote
0
down vote

favorite












I have 14 digit Hexadecimal numbers in Excel which I want to convert to a decimal number.



For example in C2 the number is 0438E96A095180 and that has to be 1188475064373632 in decimal.



I have tried a module in VBA but that is not working:



' Force explicit declaration of variables
Option Explicit

' Convert hex to decimal
' In: Hex in string format
' Out: Double
Public Function HexadecimalToDecimal(HexValue As String) As Double

' If hex starts with 0x, replace it with &H to represent Hex that VBA will understand
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = CDec(ModifiedHexValue)

End Function


With that I get the decimal number 1188475064373630 instead of 1188475064373632.



What am I doing wrong?










share|improve this question
























  • You might use the CInt function.
    – harrymc
    Nov 21 at 11:07










  • Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
    – Ben
    Nov 21 at 11:24












  • Try to use the variant datatype instead of Double.
    – harrymc
    Nov 21 at 11:34










  • Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
    – Ron Rosenfeld
    Nov 21 at 12:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 14 digit Hexadecimal numbers in Excel which I want to convert to a decimal number.



For example in C2 the number is 0438E96A095180 and that has to be 1188475064373632 in decimal.



I have tried a module in VBA but that is not working:



' Force explicit declaration of variables
Option Explicit

' Convert hex to decimal
' In: Hex in string format
' Out: Double
Public Function HexadecimalToDecimal(HexValue As String) As Double

' If hex starts with 0x, replace it with &H to represent Hex that VBA will understand
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = CDec(ModifiedHexValue)

End Function


With that I get the decimal number 1188475064373630 instead of 1188475064373632.



What am I doing wrong?










share|improve this question















I have 14 digit Hexadecimal numbers in Excel which I want to convert to a decimal number.



For example in C2 the number is 0438E96A095180 and that has to be 1188475064373632 in decimal.



I have tried a module in VBA but that is not working:



' Force explicit declaration of variables
Option Explicit

' Convert hex to decimal
' In: Hex in string format
' Out: Double
Public Function HexadecimalToDecimal(HexValue As String) As Double

' If hex starts with 0x, replace it with &H to represent Hex that VBA will understand
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = CDec(ModifiedHexValue)

End Function


With that I get the decimal number 1188475064373630 instead of 1188475064373632.



What am I doing wrong?







microsoft-excel vba hexadecimal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 15:04









phuclv

8,83063788




8,83063788










asked Nov 21 at 9:53









Ben

11




11












  • You might use the CInt function.
    – harrymc
    Nov 21 at 11:07










  • Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
    – Ben
    Nov 21 at 11:24












  • Try to use the variant datatype instead of Double.
    – harrymc
    Nov 21 at 11:34










  • Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
    – Ron Rosenfeld
    Nov 21 at 12:10


















  • You might use the CInt function.
    – harrymc
    Nov 21 at 11:07










  • Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
    – Ben
    Nov 21 at 11:24












  • Try to use the variant datatype instead of Double.
    – harrymc
    Nov 21 at 11:34










  • Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
    – Ron Rosenfeld
    Nov 21 at 12:10
















You might use the CInt function.
– harrymc
Nov 21 at 11:07




You might use the CInt function.
– harrymc
Nov 21 at 11:07












Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
– Ben
Nov 21 at 11:24






Thank you AFH, I have made the changes you suggested. Unfortunatly it still does not work. all the last numbers end with a 0 instead of the number it should be.
– Ben
Nov 21 at 11:24














Try to use the variant datatype instead of Double.
– harrymc
Nov 21 at 11:34




Try to use the variant datatype instead of Double.
– harrymc
Nov 21 at 11:34












Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
– Ron Rosenfeld
Nov 21 at 12:10




Excel only has 15 digit decimal precision, although VBA can have higher precision using the Decimal datatype. To return a value with greater than 15 digits, you will need to return a string. eg: HexadecimalToDecimal = CStr(CDec(ModifiedHexValue)). And you may need to ensure a starting &H in all cases.
– Ron Rosenfeld
Nov 21 at 12:10










1 Answer
1






active

oldest

votes

















up vote
1
down vote













You will need to return the value as a string, at least where the result has more than 15 digit precision.



eg:



Option Explicit
' Convert hex to decimal
' In: Hex in string format
' Out: Decimal in string format
Public Function HexadecimalToDecimal(HexValue As String) As String

' If hex starts with 0x, remove it to represent Hex that VBA will understand
Dim ModifiedHexValue As String

ModifiedHexValue = "&H" & Replace(HexValue, "0x", "")

HexadecimalToDecimal = CDec(ModifiedHexValue)
End Function


I will leave it to you to test the length and decide if you want to return a string or a number; and you will note that I modified your &H adding routine a bit.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377228%2f14-digit-hex-to-decimal-in-excel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You will need to return the value as a string, at least where the result has more than 15 digit precision.



    eg:



    Option Explicit
    ' Convert hex to decimal
    ' In: Hex in string format
    ' Out: Decimal in string format
    Public Function HexadecimalToDecimal(HexValue As String) As String

    ' If hex starts with 0x, remove it to represent Hex that VBA will understand
    Dim ModifiedHexValue As String

    ModifiedHexValue = "&H" & Replace(HexValue, "0x", "")

    HexadecimalToDecimal = CDec(ModifiedHexValue)
    End Function


    I will leave it to you to test the length and decide if you want to return a string or a number; and you will note that I modified your &H adding routine a bit.






    share|improve this answer

























      up vote
      1
      down vote













      You will need to return the value as a string, at least where the result has more than 15 digit precision.



      eg:



      Option Explicit
      ' Convert hex to decimal
      ' In: Hex in string format
      ' Out: Decimal in string format
      Public Function HexadecimalToDecimal(HexValue As String) As String

      ' If hex starts with 0x, remove it to represent Hex that VBA will understand
      Dim ModifiedHexValue As String

      ModifiedHexValue = "&H" & Replace(HexValue, "0x", "")

      HexadecimalToDecimal = CDec(ModifiedHexValue)
      End Function


      I will leave it to you to test the length and decide if you want to return a string or a number; and you will note that I modified your &H adding routine a bit.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You will need to return the value as a string, at least where the result has more than 15 digit precision.



        eg:



        Option Explicit
        ' Convert hex to decimal
        ' In: Hex in string format
        ' Out: Decimal in string format
        Public Function HexadecimalToDecimal(HexValue As String) As String

        ' If hex starts with 0x, remove it to represent Hex that VBA will understand
        Dim ModifiedHexValue As String

        ModifiedHexValue = "&H" & Replace(HexValue, "0x", "")

        HexadecimalToDecimal = CDec(ModifiedHexValue)
        End Function


        I will leave it to you to test the length and decide if you want to return a string or a number; and you will note that I modified your &H adding routine a bit.






        share|improve this answer












        You will need to return the value as a string, at least where the result has more than 15 digit precision.



        eg:



        Option Explicit
        ' Convert hex to decimal
        ' In: Hex in string format
        ' Out: Decimal in string format
        Public Function HexadecimalToDecimal(HexValue As String) As String

        ' If hex starts with 0x, remove it to represent Hex that VBA will understand
        Dim ModifiedHexValue As String

        ModifiedHexValue = "&H" & Replace(HexValue, "0x", "")

        HexadecimalToDecimal = CDec(ModifiedHexValue)
        End Function


        I will leave it to you to test the length and decide if you want to return a string or a number; and you will note that I modified your &H adding routine a bit.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 12:19









        Ron Rosenfeld

        1,9002610




        1,9002610






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377228%2f14-digit-hex-to-decimal-in-excel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AnyDesk - Fatal Program Failure

            How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

            QoS: MAC-Priority for clients behind a repeater