SV* my_xs_function(SV* buf_sv) { STRLEN buf_len; const char* buf = SvPVutf8(buf_sv, buf_len); if (buf_len == 0) return newSVpvs(""); { STRLEN hex_len = buf_len * 3 - 1; SV* hex_sv = sv_2mortal(newSV(hex_len)); SvPOK_on(hex_sv); SvCUR_set(hex_sv, hex_len); SvUTF8_on(hex_sv); my_c_function(SvPVX(hex_sv), buf, buf_len); return hex_sv; } }