+ Reply to Thread
Results 1 to 7 of 7

How to convert sprintf function of C to VBA?

Hybrid View

u24c02 How to convert sprintf... 03-12-2017, 10:12 AM
JBeaucaire Re: How to convert sprintf... 03-12-2017, 11:01 AM
JBeaucaire Re: How to convert sprintf... 03-12-2017, 11:04 AM
u24c02 Re: How to convert sprintf... 03-12-2017, 11:06 AM
u24c02 Re: How to convert sprintf... 03-12-2017, 11:20 AM
JBeaucaire Re: How to convert sprintf... 03-13-2017, 12:20 PM
xlnitwit Re: How to convert sprintf... 03-13-2017, 12:59 PM
  1. #1
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    How to convert sprintf function of C to VBA?

    Hi.

    Now I'm trying to convert C to vba, in especially the below function witch is coming from C.

    char *fmt_hex (long long num, int nbits, char *d);
    
    char *fmt_hex (num, nbits, d)
    long long num;
    int nbits;
    char *d;
    {
    	sprintf (d, "0x%0*llX", (nbits - 1) / 4 + 1, num);
    	return (d);
    }
    and I've converted as the below, but not sure how to convert the "sprintf" of c function.
    can you help me please?

    Function fmt_hex(num, nbits, d) As Characters
    Dim num As Long
    Dim nbits As Integer
    Dim d As Characters
    
        sprintf (d, "0x%0*llX", (nbits - 1) / 4 + 1, num);
       fmt_hex = (d)
    
    End Function

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to convert sprintf function of C to VBA?

    Cross posted here.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to convert sprintf function of C to VBA?

    Does this doe what you need?
    http://www.houkema.net/projects/unca...ntf-vba-macro/

  4. #4
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How to convert sprintf function of C to VBA?

    I've already seen that just before, but that code does not work.

    Especially,

    mask = Replace$(mask, “%s”, tokens(i), , 1) this part is red

  5. #5
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How to convert sprintf function of C to VBA?

    Quote Originally Posted by JBeaucaire View Post

    can you help me how to convert to VBA ?
     sprintf (d, "0x%0*llX", (nbits - 1) / 4 + 1, num);

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to convert sprintf function of C to VBA?

    Setting that code aside, can you describe the result you're after? What I've read online about sprint leads to me to believe it's nothing more than a string concatenation technique.

    Is of the variable value your starting with is nbits, to which you're doing the math: ((nbits - 1) / 4) + 1 that's fine, the same in VBA.

    Now what? What would the typical results of that calculation be?
    Then you are slipping that into the text string: "0x%0*llX" somewhere?

    Give an example of a starting nbits value and the resulting text string you expect to create. VBA has easy ways to concatenate strings.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to convert sprintf function of C to VBA?

    Hi,

    I'm not absolutely certain that I have remembered the formatting options correctly but my best guess would be this
    Function fmt_hex(num, nbits) As String
    
       fmt_hex = "0x" & Right$(String((nbits - 1) / 4 + 1, "0") & Hex(num), nbits)
    
    End Function
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ 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] Convert Worksheet Function to VBA Function
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2017, 11:44 AM
  2. [SOLVED] Function to convert function into text with values?
    By starter. in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-28-2016, 07:16 AM
  3. [SOLVED] Convert to Function?
    By cocacrave in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2015, 03:57 PM
  4. Convert Function
    By staccee in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-11-2013, 10:37 AM
  5. what function to convert convert 34234 to 99999
    By archiles in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-04-2008, 11:26 PM
  6. Need a function to convert value in its name
    By Abraham in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-12-2006, 11:03 PM
  7. [SOLVED] Convert function
    By D in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-05-2006, 01:55 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