Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created March 31, 2011 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/897271 to your computer and use it in GitHub Desktop.
Save bnoordhuis/897271 to your computer and use it in GitHub Desktop.
utf7.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "iconv.h"
int main(void) {
size_t inbytesleft, outbytesleft;
char *inbuf, *outbuf, data[32];
iconv_t iv;
int rv;
iv = iconv_open("UTF-7", "UTF-8");
assert(iv != (iconv_t) -1);
inbuf = "\xC3\xA7"; // "ç"
inbytesleft = 2;
outbuf = data;
outbytesleft = sizeof data;
errno = 0;
rv = iconv(iv, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
assert(errno == 0);
assert(inbytesleft == 0);
data[sizeof data - outbytesleft] = '\0';
printf("Converted to '%s'\n", data);
iconv_close(iv);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment