|
> シングルバイトカタカナと漢字の1バイト目の判断がわかりません。
片仮名は 0xA1〜0xDF。漢字の1バイト目は 0x81〜0x9F、0xE0〜0xFC なので区別でき
ます。できないのは、漢字の2バイト目の、0x40〜0x7E、0x80〜0xFC でしょう。
/* type -- s[n] のタイプを返す */
int type(char *s, int n)
{
int c = s[n] & 0xFF;
if (n > 0 && c >= 0x40 && c != 0x7F && c <= 0xFC && type(s, n-1)=='1')
return '2'; /* 40-7E, 80-FC: 漢字第2バイト */
if (c <= 0x7F) return 'A'; /* 00-7F: ASCII */
if (c == 0x80) return 'U'; /* 80 : 未定義 */
if (c <= 0x9F) return '1'; /* 81-9F: 漢字第1バイト */
if (c == 0xA0) return 'U'; /* A0 : 未定義 */
if (c <= 0xDf) return 'K'; /* A1-DF: 片仮名 */
if (c <= 0xFC) return '1'; /* E0-FC: 漢字第1バイト */
return 'U'; /* FD-FF: 未定義 */
}
/* Test Program */
#include <stdio.h>
#include <string.h>
int main()
{
char buf[1024];
while (fgets(buf, sizeof buf, stdin) && buf[0] != '.') {
int n = strlen(buf)/2;
int t = type(buf, n);
printf("buf[%d]=%02x: %c\n", n, buf[n]&0xFF, t);
}
return 0;
}
|