Quick actions

cmd+k|ctrl+k

Navigation

Languages

Codepoint to utf8

Snippet info

Language

C

Visibility

public

Author

evine

Created

2017-04-03T03:05:27Z

Updated

2017-04-08T03:48:13Z

#include <stdio.h>
#include <stdint.h>
#include <assert.h>

// buffer length must be 4 bytes.
uint8_t code_point_to_utf8(uint8_t *buffer, uint32_t code_point)
{
    if     (code_point < (1 <<  7)){
        buffer[0] = (uint8_t)code_point;
        return 1;
    }
    else if(code_point < (1 << 11)){
        buffer[0] = 0b11000000 | (code_point & 0b11111000000) >> 6;
        buffer[1] = 0b10000000 | (code_point & 0b00000111111) >> 0;
        
        return 2;
    }
    else if(code_point < (1 << 16)){
        buffer[0] = 0b11100000 | (code_point & 0b1111000000000000) >> 12;
        buffer[1] = 0b10000000 | (code_point & 0b0000111111000000) >> 6;
        buffer[2] = 0b10000000 | (code_point & 0b0000000000111111) >> 0;
        
        return 3;
    }
    else if(code_point < (1 << 21)){
        buffer[0] = 0b11110000 | (code_point & 0b111000000000000000000) >> 18;
        buffer[1] = 0b10000000 | (code_point & 0b000111111000000000000) >> 12;
        buffer[2] = 0b10000000 | (code_point & 0b000000000111111000000) >> 6;
        buffer[3] = 0b10000000 | (code_point & 0b000000000000000111111) >> 0;
        
        return 4;
    }
    else
    {
        return 0;
    }
}

void print_utf8(uint32_t code_point)
{
    uint8_t buffer[4] = {};
    uint8_t len = code_point_to_utf8(buffer, code_point);
    printf("%.*s", len, buffer);
}

int32_t main(void)
{
    print_utf8(0xC5);
    
    print_utf8(0x41);
    print_utf8(0x030A);
    
    putchar('\n');
    
    print_utf8('1');
    print_utf8('5');
    print_utf8(0x2044);
    print_utf8('2');
    print_utf8('3');
    
    putchar('\n');
    
    print_utf8(0x092B);
    print_utf8(0x093F);
    
    putchar('\n');
    
    print_utf8(0x2615);
    print_utf8(0x20DF);
    
    putchar('\n');
    return 0;
}



INFO