### Function

# GLib convert_with_fallback

#### Declaration

    gchar*
    g_convert_with_fallback (
      const gchar* str,
      gssize len,
      const gchar* to_codeset,
      const gchar* from_codeset,
      const gchar* fallback,
      gsize* bytes_read,
      gsize* bytes_written,
      GError** error
    )

#### Description

Converts a string from one character set to another, possibly including fallback sequences for characters not representable in the output. Note that it is not guaranteed that the specification for the fallback sequences in `fallback` will be honored. Some systems may do an approximate conversion from `from_codeset` to `to_codeset` in their `iconv()` functions, in which case GLib will simply return that approximate conversion.

Note that you should use `g_iconv()` for streaming conversions. Despite the fact that `bytes_read` can return information about partial characters, the g_convert_… functions are not generally suitable for streaming. If the underlying converter maintains internal state, then this won’t be preserved across successive calls to g_convert(), `g_convert_with_iconv()` or g_convert_with_fallback(). (An example of this is the GNU C converter for CP1255 which does not emit a base character until it knows that the next character is not a mark that could combine with the base character.).

#### Parameters

`str`

  Type: An array of `guint8`

                 the string to convert.

  The length of the array is specified in the `len` argument.
  The data is owned by the caller of the function.

`len`

  Type: `gssize`

  The length of the string in bytes, or -1 if the string is nul-terminated (Note that some encodings may allow nul bytes to occur inside strings. In that case, using -1 for the `len` parameter is unsafe).

`to_codeset`

  Type: `const gchar*`

  Name of character set into which to convert `str`.

  The data is owned by the caller of the function.
  The value is a NUL terminated UTF-8 string.

`from_codeset`

  Type: `const gchar*`

  Character set of `str`.

  The data is owned by the caller of the function.
  The value is a NUL terminated UTF-8 string.

`fallback`

  Type: `const gchar*`

  UTF-8 string to use in place of characters not present in the target encoding. (The string must be representable in the target encoding). If `NULL`, characters not in the target encoding will be represented as Unicode escapes \uxxxx or \Uxxxxyyyy.

  The data is owned by the caller of the function.
  The value is a NUL terminated UTF-8 string.

`bytes_read`

  Type: `gsize*`

  Location to store the number of bytes in the input string that were successfully converted, or `NULL`. Even if the conversion was successful, this may be less than `len` if there were partial characters at the end of the input.

  The argument will be set by the function.
  The argument can be `NULL`.

`bytes_written`

  Type: `gsize*`

  The number of bytes stored in the output buffer (not including the terminating nul).

  The argument will be set by the function.
  The argument can be `NULL`.

`error`

  Type: `GError **`

  The return location for a recoverable error.

  The argument can be `NULL`.
  If the return location is not `NULL`, then you must initialize it to a `NULL` `GError*`.
  The argument will be left initialized to `NULL` by the function if there are no errors.
  In case of error, the argument will be set to a newly allocated `GError`; the caller will take ownership of the data, and be responsible for freeing it.

#### Return value

Type: An array of `guint8`

         If the conversion was successful, a newly allocated buffer
         containing the converted string, which must be freed with g_free().
         Otherwise `NULL` and `error` will be set.

The length of the array is in the `bytes_written` argument.
The caller of the function takes ownership of the data, and is responsible for freeing it.