How do I get a UCS code of a 1-byte letter of UTF-8 in C++?

152 Views Asked by At

I need to check, whether a letter (in english and russian languages) is alphabetical. A file is supposed to be encoded with UTF-8 by default. I found out, that the best solution is working with UCS codes. The way to calculate UCS-code of 2-bytes encoded letter is

#include <stdio.h>
#include <stdlib.h>

char utf8len[256] = { 
  // len = utf8len[c] & 0x7  cont = utf8len[c] & 0x8 
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 0  - 15
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 16 - 31
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 32 - 47
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 48 - 63
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 64 - 79
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 80 - 95
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 96 - 111
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, // 112 - 127

  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8, // 80 - 8f
  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8, // 90 - 9f
  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8, // a0 - af
  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8, // b0 - bf

  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, // c0 - cf
  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, // d0 - df

  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, // e0 - ef

  4,  4,  4,  4,  4,  4,  4,  4,  // f0 - f7

  5,  5,  5,  5,  // f8, f9, fa, fb

  6,  6,  // fc, fd

  0,  0   // fe, ff 
};

#define UTF8LEN(c) (utf8len[(unsigned char)(c)] & 0x7)
#define UTF8CONT(c) (utf8len[(unsigned char)(c)] & 0x8)

int main (int argc, char *argv[])
{
  char *s = "Б№1АГД"; //string which contains cyrillic symbols

  while (*s) {
    int ucode;

    printf ("[%s] %d\n", s, UTF8LEN(*s));
    if ((UTF8LEN(*s) == 2) && UTF8CONT(s[1])) {
      ucode = ((*s & 0x1f) << 6) | (s[1] & 0x3f); //! HERE I GET UCS CODE 
      printf ("ucode = 0x%x\n", ucode);
      s++;
    }
    s++;
  }

}

It's a half of the solution I'm looking for. This code alows me to work with cyrillic symbols only (as they're encoded with 2 bytes in UTF-8). The problem is, I need to work with latin alphabet as well. So what should i do to get UCS code for 1-byte symbol (in my case with UTF8LEN(c)=1)?

Upd: Probably, the solution is:

ucode = *s

Will this work?

0

There are 0 best solutions below