#include<stdio.h> #include<string.h> int unicode_to_utf( unsigned long unicode, unsigned char *utf ) { //assert( utf ); int size = 0; if ( unicode <= 0x7F ) { *( utf + size++ ) = unicode & 0x7F; } else if ( unicode >= 0x80 && unicode <= 0x7FF ) { *( utf + size++ ) = ( ( unicode >> 6 ) & 0x1F ) | 0xC0; *( utf + size++ ) = ( unicode & 0x3F ) | 0x80; } else if ( unicode >= 0x800 && unicode <= 0xFFFF ) { *( utf + size++ ) = ( ( unicode >> 12 ) & 0x0F ) | 0xE0; *( utf + size++ ) = ( ( unicode >> 6 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( unicode & 0x3F ) | 0x80; } else if ( unicode >= 0x10000 && unicode <= 0x10FFFF ) { *( utf + size++ ) = ( (unicode >> 18 ) & 0x7 ) | 0xF0; *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 6 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( unicode & 0x3F ) | 0x80; } else if ( unicode >= 0x200000 && unicode <= 0x3FFFFFF ) { *( utf + size++ ) = ( (unicode >> 24 ) & 0x3 ) | 0xF8; *( utf + size++ ) = ( (unicode >> 18 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 6 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( unicode & 0x3F ) | 0x80; } else if ( unicode >= 0x4000000 && unicode <= 0x7FFFFFFF ) { *( utf + size++ ) = ( (unicode >> 30 ) & 0x1 ) | 0xFC; *( utf + size++ ) = ( (unicode >> 24 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 18 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 12 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( (unicode >> 6 ) & 0x3F ) | 0x80; *( utf + size++ ) = ( unicode & 0x3F ) | 0x80; } else { printf( "Error : unknow scope\n" ); return -1; } *( utf + size ) = ‘\0‘; return size; } void utf_print( unsigned char *utf, int size ) { if ( size == -1 ) { printf( "unknow scope\n" ); return; } int index = 0; for ( ; index < size; index += 1 ) { printf( "%X", *( utf + index) ); } printf( "\n" ); } int main( int argc, char *argv[] ) { unsigned long unicode1 = 55; unsigned char utf[7] = { 0 }; int size = unicode_to_utf( unicode1, utf ); utf_print( utf, size ); unsigned long unicode2 = 123; size = unicode_to_utf( unicode2, utf ); utf_print( utf, size ); memset( utf, 0x00, sizeof( utf ) ); unsigned long unicode3 = 0x4E25; size = unicode_to_utf( unicode3, utf ); utf_print( utf, size ); memset( utf, 0x00, sizeof( utf ) ); unsigned long unicode4 = 23456; size = unicode_to_utf( unicode4, utf ); utf_print( utf, size ); memset( utf, 0x00, sizeof( utf ) ); unsigned long unicode5 = 234567; size = unicode_to_utf( unicode5, utf ); utf_print( utf, size ); memset( utf, 0x00, sizeof( utf ) ); unsigned long unicode6 = 34561234; size = unicode_to_utf( unicode6, utf ); utf_print( utf, size ); /* error */ memset( utf, 0x00, sizeof( utf ) ); unsigned long unicode7 = 0x8FFFFFFF; size = unicode_to_utf( unicode7, utf ); utf_print( utf, size ); return 0; }
[email protected]:/fdsk# gcc -o utf0 utf0.c [email protected]:/fdsk# ./utf0 37 7B E4B8A5 E5AEA0 F0B99187 FA83B5B392 Error : unknow scope unknow scope
原文地址:https://www.cnblogs.com/abc36725612/p/11175726.html
时间: 2024-10-31 23:16:03