Hi, Vignesh.
Glad I could be of some help.
I didn't think about adding the null to the elixir string. Is there a chance that that you will get repeated null terminations unless you check that the string already is null terminated?
For my situation, I do all this work in the c code. Here is what I have done.
char *
alloc_and_copy_to_cstring(ErlNifBinary *string)
{
char *str = (char *) enif_alloc(string->size + 1);
strncpy(str, (char *)string->data, string->size);
str[string->size] = 0;
return str;
}
And it is called with code as below:
ErlNifBinary string;
if (argc != 1 || ! enif_inspect_binary(env, argv[0], &string))
return enif_make_badarg(env);
char *str = alloc_and_copy_to_cstring(&string);