Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:08:00+00:00 2026-06-09T21:08:00+00:00

I want to code vsnprintf function, using NSString. This is my implementation of vsnprintf

  • 0

I want to code vsnprintf function, using NSString.

This is my implementation of vsnprintf :

int my_vsnprintf(char *buffer, size_t count, const char *format, ...)
{
    int iRet;
    va_list ap;
    NSString *pnssBuffer=NULL;
    NSString *pnssFormat;
    const char *pcszBuffer;
    /* Format with NSString */
    pnssFormat=[NSString stringWithCString:format 
    encoding:NSUTF8StringEncoding];
    va_start(ap, format);
    pnssBuffer=[[NSString alloc] initWithFormat:pnssFormat arguments:ap];
    /* Copy to char * */
    pcszBuffer=[pnssBuffer cStringUsingEncoding:NSUTF8StringEncoding];
    memcpy(buffer, pcszBuffer, count);
    iRet=strlen(buffer);
    /* Free */
    [pnssBuffer release];
    va_end(ap);
    return iRet;
}

When I call it with char * param, it works :

    my_vsnprintf(buffer, 256, "hello %s", "world");
NSLog(@"buffer = %s", buffer);

print correctly this : buffer = hello world

But when I call it with wchar_t * param, I only get fist character of world

    my_vsnprintf(buffer, 256, "hello %ls", L"world");
NSLog(@"buffer = %s", buffer);

print this : buffer = hello w

I don’t understand why, any help for my function ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:08:02+00:00Added an answer on June 9, 2026 at 9:08 pm

    According to String Format Specifiers in the “String Programming Guide”, initWithFormat:arguments: and friends do not support the “%ls” format. In fact, the “l” modifier is ignored here.

    There is a “%S” format, but that is for a sequence of 16-bit Unicode characters, so you cannot use it for wchar_t either.

    ADDED

    wchar_t is 4-byte Unicode (little endian) on the iPhone and the Simulator, therefore you can convert it to NSString as follows:

    wchar_t *w = L"Hello world";
    NSString *str = [[NSString alloc] initWithBytes:w length:4*wcslen(w)
        encoding:NSUTF32LittleEndianStringEncoding];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to code this part of a VB6 application in c#. How can
I'll preface this by saying it's a homework assignment. I don't want code written
I am using Aptana 3.07 and I want code assist for ExtJS 3.4. I
I want to code a line like this in my mvc view: <input type=button
see my code #include<stdarg.h> #define DPRINTF(_fmt, ...) debugPrintf(_fmt,__VA_ARGS__) void debugPrintf(const char *fmt, ...) {
I know that someone is going to want code for this issue but the
I am using the same code for both tablets and mobile, and I want
What's the best way to count all elements in an iterator? I want code
I don't want code, just need help finding where to start this project. I
I want to code a function to get parent's children. get_children supposed to return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.