utf8sjis.c

/* */ /* utf8sjis.c */ /* %+ UTF-8 with % Decoder */ /* copyright (C) 2008 thinking-j, All Rights Reserved. */ /* */ #include #include "jis2uni.h" int hex2dec(char c) { if ((c > '/') && (c < ':')) return (int)c - 0x30; else { c &= 0xDF; if ((c > '@') && (c < 'G')) return (int)c - 0x41 + 10; } return -1; } unsigned char *utf8str2sjis(unsigned char *sjis, unsigned char *utf8) { char *p; char c; unsigned long int unicode; unsigned int tmp; int i, byte; p = sjis; while((c = *utf8++) != '\0') { if (c < 0x80) *sjis++ = c ; else { byte = 0; while (c & 0x80) { byte++; c <<= 1;} unicode = (c & 0xFF) >> byte; byte--; for (i=0; i < byte; i++) { unicode <<= 6; unicode += 0x3f & *utf8++; } tmp = unicode2sjis(unicode); *sjis++ = (0xFF00 & tmp) >> 8; *sjis++ = (0x00FF & tmp); } } *sjis = '\0'; return p; } unsigned char *percent2str(unsigned char *str, unsigned char *ps) { char *p; char c; p = str; while((c = *ps++) != '\0') { if (c != '%') *str++ = c ; else { c = hex2dec(*ps++) << 4; c += hex2dec(*ps++); *str++ = c; } } *str = '\0'; return p; } main(int argc, char *argv[]) { char *sample = "http://blogsearch.google.co.jp/blogsearch?q=%E3%82%B3%E3%83%B3%E3%83%94%E3%83%A5%E3%83%BC%E3%82%BF%E3%81%AF%E3%80%81%E3%82%80%E3%81%9A%E3%81%8B%E3%81%97%E3%81%99%E3%81%8E%E3%81%A6%E4%BD%BF%E3%81%88%E3%81%AA%E3%81%84!+&hl=ja&num=100&newwindow=1&lr=lang_ja&sourceid=mozilla-search&um=1&ie=UTF-8&sa=N&tab=wb"; char string[512]; printf("%s\n", sample); percent2str(string, sample); utf8str2sjis(string, string); printf("%s\n", string); }