Jump to content
tjt

Unicode & C/C++/Java

Recommended Posts

Posted (edited)

Am facut un mic programel in C care primeste ca argument un sir de caractere si il afiseaza intr-un format ce permite folosirea sa in C/C++/Java.

Nu am cunostinte foarte mari in C,probabil putea fi facut si mai eficient sau/si mai scurt.

De exemplu pentru sirul ??? se va afisa \u3088 \u597d \u305d .

#include <stdio.h>
#include <string.h>

int getCodePoint(char *buffer,int ind,int noByte){
if(noByte==1)
return buffer[ind];
int x;
x=buffer[ind]&(0xFF>>(noByte+1));
while(--noByte!=0){
x=(x<<6)|(buffer[++ind]&(0xFF>>2));
}
return x;
}

void check(char *buffer,int dim){
int ind=0;
int codePoint;
while(ind<dim){
if((buffer[ind] & 0x80 ) == 0){
codePoint=getCodePoint(buffer,ind,1);
ind++;
}
else if((buffer[ind] & 0xE0 ) == 0xC0){
codePoint=getCodePoint(buffer,ind,2);
ind+=2;
}
else if((buffer[ind] & 0xF0 ) == 0xE0 ){
codePoint=getCodePoint(buffer,ind,3);
ind+=3;
}
else if((buffer[ind] & 0xF8 )== 0xF0 ){
codePoint=getCodePoint(buffer,ind,1);
ind+=4;
}
printf("\\u%04x ",codePoint);
}
printf("\n");
}

int main(int argc,char **argv){
if(argc!=2){
printf("usage: ./getCode string");
}
check(argv[1],strlen(argv[1]));
return 0;
}

Edited by tjt

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...