0

I have tried to convert the wchar_t string to NSString using initWithBytes but it always returns null.

i have tried the following code where my API returns NSString, but it is returning null always.

    typedef struct CamList__Int_t
    {
        wchar_t DevicePath[MAX_CAM_DEV_SUPPORTED_INT][MAX_PATH];
        UINT8 No_of_Cam_Devices;

    }
    CamList_Int;


    -(NSString *)ConvertWcharStringToNSString :(const wchar_t *)inputString {
        if (inputString == NULL) {

            printf("inputString is Null, So there is No String Conversion taken Place!!!\r\n");
            return NULL;
        }
        else {
            printf("inputString: %s\r\n", inputString); //printing inputString for Debuging
            return [ [NSString alloc] initWithBytes:inputString length:(wcslen(inputString) * sizeof(wchar_t)) encoding:NSUTF32LittleEndianStringEncoding ];
        }
    }


    -(void) getConnectedDeviceName :(inout NSString *)deviceName {
        CamList_Int CamDeviceListInt;
        if(CreateCamDevicesLists(&CamDeviceListInt) == true) {

            deviceName = [self ConvertWcharStringToNSString: CamDeviceListInt.DevicePath[0]];
        }
    }

So kindly help me out to resolve this issue.

Thanks in Advance

  • 2
    Possible duplicate of [How to convert wchar\_t to NSString?](http://stackoverflow.com/questions/7753262/how-to-convert-wchar-t-to-nsstring) – Larme Oct 03 '16 at 13:13

1 Answers1

0

Hi i have found there is a problem with the encoding flag and after changing this it is working fine.

Given below the re edited API call which will return the NSString without null.

-(NSString *)ConvertWcharStringToNSString :(const wchar_t *)inputString {
            if (inputString == NULL) {

            printf("inputString is Null, So there is No String Conversion taken Place!!!\r\n");
            return NULL;
            }
            else {
            printf("inputString: %s\r\n", inputString); //printing inputString for Debuging
            return [ [NSString alloc] initWithBytes:inputString length:(wcslen(inputString) * sizeof(wchar_t)) encoding: NSUTF8StringEncoding ];
        }
    }

Thanks