def string(s, encoding='ascii'):
    if isinstance(s, str):
        s = s.encode(encoding)
result = ''
for c in s:
    if not 32 <= ord(c) < 127 or c in ('\\', '"'):
        result += '\\%03o' % ord(c)
else:
    result += c
return '"' + result + '"'
How to escape string for generated C++?