+ Reply to Thread
Results 1 to 9 of 9

CLng strange behaviour

Hybrid View

  1. #1
    Registered User
    Join Date
    09-07-2017
    Location
    Cagliari
    MS-Off Ver
    MS Office 2010
    Posts
    5

    CLng strange behaviour

    Hi everybody,

    I am noticing a strange (but maybe I am not using it in the right way ) behaviour of method CLng().
    If I write:

    Dim lngNum As Long
    lngNum = CLng("400118E0")
    MsgBox lngNum
    the result is:
    Cattura.PNG

    wheres if I write

    Dim lngNum As Long
    lngNum = CLng("40011880")
    MsgBox lngNum
    the result is:
    Cattura.PNG

    Why is char 'E' not recognized and displayed on the message box?

    I need to convert 32-bit address (to be used with .bin/.hex file) from string to hex value(i.e. input. "400118E0", output: 0x400118E0).

    Thank you in advance,

    Ziomocci

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CLng strange behaviour

    Why E0?

    If I try this I see 40011800 in the message box.
    Dim lngNum As Long
    lngNum = CLng("400118E2")
    MsgBox lngNum
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-07-2017
    Location
    Cagliari
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: CLng strange behaviour

    Hi Norie,

    Because this is the format of memory address in binary file. I have used a message box as an example.
    Memory locations in a binary file, considering entries of 16 bytes (for example), are 0x0, 0x10, 0x20...0x2000, 0x2010, 0x2020,...0x40000000, 0x40000010, 0x40000020,...0x40011800, 0x40011810, 0x40011820, 0x40011830, 0x40011840,...0x40011890, 0x400118A0, 0x400118B0, 0x400118C0, 0x400118D0 and so on.
    For this reason I have to convert correclty the string.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,437

    Re: CLng strange behaviour

    I don't think I understand. Converting the (decimal) number 400118E0 (scientific/exponential notation meaning 400118*10^0) to a long integer correctly interprets the number as 400118. Is the input supposed to be a hex number (hex 400118E0 = dec 1073813728)? If the inputs are supposed to be hexadecimal, I think you have to do something additional to tell VBA that those are hex numbers. If you don't, then VBA will assume they are decimal numbers. I never deal with hexadecimal numbers, so I am not sure exactly how that should be done. My one real encounter with hexadecimal suggested preceding hex numbers with &H
    lngnum=CLng("&H400118E0")
    or use some kind of Hex2Dec() function.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    09-07-2017
    Location
    Cagliari
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: CLng strange behaviour

    Quote Originally Posted by MrShorty View Post
    I don't think I understand. Converting the (decimal) number 400118E0 (scientific/exponential notation meaning 400118*10^0) to a long integer correctly interprets the number as 400118. Is the input supposed to be a hex number (hex 400118E0 = dec 1073813728)? If the inputs are supposed to be hexadecimal, I think you have to do something additional to tell VBA that those are hex numbers. If you don't, then VBA will assume they are decimal numbers. I never deal with hexadecimal numbers, so I am not sure exactly how that should be done. My one real encounter with hexadecimal suggested preceding hex numbers with &H
    lngnum=CLng("&H400118E0")
    or use some kind of Hex2Dec() function.
    Hi MrShorty,

    I need to take a String from a cell, eg. 400118E0, and convert it in Long 0x400118E0 so that I could use the value in other part of the program.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CLng strange behaviour

    E0 evaluates to 1 so 400118E0 evaluates to 40018.

    Try running this, you'll see the results in the Immediate Window.
    Dim s As String
    Dim I As Long
    
        For I = 0 To 3
            s = "400118E" & I
            Debug.Print s & "=" & CLng(s)
        Next I

  7. #7
    Registered User
    Join Date
    09-07-2017
    Location
    Cagliari
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: CLng strange behaviour

    Quote Originally Posted by Norie View Post
    E0 evaluates to 1 so 400118E0 evaluates to 40018.

    Try running this, you'll see the results in the Immediate Window.
    Dim s As String
    Dim I As Long
    
        For I = 0 To 3
            s = "400118E" & I
            Debug.Print s & "=" & CLng(s)
        Next I
    You have demonstrated that I am not using the method in the right way
    therefore Norie, do you know a way (or more) to convert a String, eg. "400118E0", in Long value 0x400118E0, let me say, "char-by-char"?

    If it was not enough clear, I am not used to work with VBA macros (I am an embedded-C programmer, so....)
    For sure, thanks a lot for your fastest answers!!!

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CLng strange behaviour

    If you just want to convert the string 400118E0 to 0x400118E0 you can use simple conatentaion.
    s ="0x" & Range("A1").Value ' value in A1 is 400118E0
    
    MsgBox  s

  9. #9
    Registered User
    Join Date
    09-07-2017
    Location
    Cagliari
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: CLng strange behaviour

    Problem solved with string concatentaion.
    Thanks everybody!!!

+ 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. [SOLVED] strange behaviour from vba
    By harryv27 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-14-2016, 04:50 AM
  2. Strange Zoom behaviour
    By lgolf in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-27-2014, 09:03 AM
  3. Strange Behaviour
    By Mark@Work in forum Excel General
    Replies: 2
    Last Post: 09-30-2008, 12:22 PM
  4. Strange Texttocolumn behaviour
    By Mbrolass in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2008, 01:17 PM
  5. Strange if(***) behaviour?
    By Excel 2003 - SPB in forum Excel General
    Replies: 6
    Last Post: 08-06-2006, 12:35 PM
  6. [SOLVED] Strange VBA Behaviour
    By Ricko in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2005, 03:05 AM
  7. [SOLVED] Strange behaviour
    By Edgar Thoemmes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-08-2005, 12:06 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