I have written an sample code by following the link to convert ASCII to UTF-16LE using iconv but the output shows only a single charecter and blankspaces . The code is attached below please let me know where i'm going wrong.
#include <iconv.h>
#include <stdio.h>
#include <string.h>
int main()
{
char Input[20];
char Output[100];
size_t insize,out_size;
memset(Input,0,sizeof(Input));
memset(Output,0,sizeof(Output));
int nconv=0;
char *Inptr;
char *outptr;
printf("Input data :");
scanf("%s",Input);
iconv_t cd = iconv_open("UTF-16LE","ASCII");
if(cd==(iconv_t)-1)
{
printf("iconv_open has failed ");
return 0;
}
insize=strlen(Input);
out_size=3*insize;
Inptr =Input;
outptr=(char *)Output;
nconv=iconv(cd,&Inptr,&insize,&outptr,&out_size);
if(nconv!=0)
{
printf("Unable to perform conversion ");
return 0;
}
printf("\n Data After conversion from ASCII to UTF-16 = %s \n ",Output);
}
The output for the same is as given below
Input data :Hello world
Data After conversion from ASCII to UTF-16 = H
When you convert "Hello" to UTF-16LE, you end up with this byte sequence (shown in hex):
The printf call says to print the string as though it's a regular zero-terminated character string. It sees
48
and prints anH
, and then it sees00
and it thinks it's done.You need a print function that can interpret the string as UTF-16LE. There isn't a standard one in C.