Converting between Unicode and UTF8

This tutorial describes the steps to convert between Unicode and UTF8.

Introduction

EscapeUtils escape encodes and decodes unsafe data in URI. It also supports converting of Unicode data (16-bit descriptor) into UTF8 data (8-bit descriptor) and vice-versa.

Procedure

Converting to UTF8

EscapeUtils::ConvertFromUnicodeToUtf8L() converts the Unicode data into UTF8 format.

_LIT16(KUnicode, "Unicode string"); //data to be converted
    
HBufC8* utf8  = EscapeUtils::ConvertFromUnicodeToUtf8L(KUnicode);

utf8 contains the UTF8 form of the string.

Converting to Unicode

EscapeUtils::ConvertToUnicodeFromUtf8L() converts the data from UTF8 format to Unicode.

_LIT8(KUtf8, "UTF-8 string"); // UTF8 string to be converted
    
HBufC16* unicode = EscapeUtils::ConvertToUnicodeFromUtf8L(KUtf8); // convert the srting to Unicode
          

unicode contains the Unicode form of the string.

Call EscapeUtils::IsEscapeTriple to check if the input data contains an escape triple. For example, %2a. If there is a triple, its value is calculated and returned through the output argument HexVal. If there is no escape triple, then this argument is left unchanged.

_LIT(KEscapeTriple1, "%2a"); // input data containing escape triple
TInt KEscapeTriple1_value = 0x2a;
TInt HexVal;
EscapeUtils::IsEscapeTriple(KEscapeTriple1,HexVal); // escape triple value
//variable HexVal contains value 0x2a

The code above returns '42' , the value of escape triple.

Related concepts