0

Is there any way to convert the string into an array of objects of different data types, according to multiple format specifiers?

I have tried initWithFormat:arguments: but I'm not getting the expected results. There is a similar function in Python in the struct module, unpack(fmt,String), where fmt is a format specifier string and String is the string to be converted.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201

1 Answers1

0

Look into sscanf:

void unpackStr(NSString *string, NSString *format, ...)
{
    va_list args;
    va_start(args, format);

    vsscanf([string UTF8String], [format UTF8String], args);

    va_end(args);
}
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Hi, the code that you have provided is not giving expected results. I have done a test to display a string _** 123495885aaaaa_ but the results are _** 2686712268671241008576002686760268671253239441727800024PYõfÀYõf°SõfpRõf Rõf�Qõf Qõf **_ – simplygauz May 08 '12 at 22:24
  • What format are you sending into `args`? Have you read the sscanf docs? – Richard J. Ross III May 08 '12 at 22:32
  • I have added printf command before va_list for format and string and one printf before va_end and the output is as Below
    format: %i%i%i%i%i%i%i%i%i%5s
    string: 123495885aaaaa
    value:2686728 2686728 5 0 1728095392 1727081904 2686728 1728095392 1727996152 PYõfÀYõf°SõfpRõf Rõf�Qõf Qõf
    – simplygauz May 09 '12 at 23:00