1# Working with String Using Node-API 2 3## Introduction 4 5This topic walks you through on how to use Node-API to convert data between native strings and ArkTS strings. 6 7## Basic Concepts 8 9As a common data type in programming, string is a sequence of characters used to represent text. It can also be used to build user interface (UI) elements such as labels, buttons, and text boxes, process user input, and validate and format input data. Different encodings support different character sets and languages. Major encoding schemes include the following: 10 11- ASCII<br>ASCII is one of the earliest character encoding schemes. It uses 7 bits to represent English letters, digits, and some basic symbols. It serves as the foundation for encoding schemes. 12- UTF-8<br>UTF-8 is a variable-length encoding scheme that can represent any Unicode character. It uses 8 bits per character and uses byte sequences of different lengths depending on the range of the character. UTF-8 is widely used for web content. 13- UTF-16<br>UTF-16 is a fixed-length or variable-length encoding scheme that uses 16 bits per character. It can represent all Unicode characters and is suitable for larger character sets. 14- ISO-8859-1 (Latin-1)<br>ISO-8859-1 is a single-byte coding scheme that uses 8 bits per character. It is mainly used to represent Latin alphabet characters and commonly used in European languages. 15 16## Available APIs 17 18The following table lists the APIs provided by the Node-API module for creating and obtaining strings. 19| API| Description| 20| -------- | -------- | 21| napi_get_value_string_utf8 | Obtains a UTF8-encoded string from an ArkTS value.| 22| napi_create_string_utf8 | Creates an ArkTS string from a UTF8-encoded C string.| 23| napi_get_value_string_utf16 | Obtains a UTF16-encoded string from an ArkTS value.| 24| napi_create_string_utf16 | Creates an ArkTS string from a UTF16-encoded C string.| 25| napi_get_value_string_latin1 | Obtains an ISO-8859-1-encoded string from an ArkTS value.| 26| napi_create_string_latin1 | Creates an ArkTS string from an ISO-8859-1-encoded tring.| 27 28## Example 29 30If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code involved in the string-related APIs. 31 32### napi_get_value_string_utf8 33 34Use **napi_get_value_string_utf8** to convert an ArkTS string to a UTF-8-encoded string. 35 36CPP code: 37 38```cpp 39#include "napi/native_api.h" 40#include <string> 41 42static napi_value GetValueStringUtf8(napi_env env, napi_callback_info info) 43{ 44 size_t argc = 1; 45 napi_value args[1] = {nullptr}; 46 47 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 48 std::string buf; 49 size_t length = 0; 50 napi_status status = napi_get_value_string_utf8(env, args[0], (char*)buf.c_str(), NAPI_AUTO_LENGTH, &length); 51 // napi_string_expected will be returned for non-string inputs. 52 if (status == napi_string_expected) { 53 return nullptr; 54 } 55 napi_value result; 56 napi_create_string_utf8(env, buf.c_str(), length, &result); 57 return result; 58} 59``` 60 61API declaration: 62 63```ts 64// index.d.ts 65export const getValueStringUtf8: (param: string | number) => string | void; 66``` 67 68ArkTS code: 69 70```ts 71import hilog from '@ohos.hilog' 72import testNapi from 'libentry.so' 73// Pass in a string and a number respectively. If the input is a string, the string will be returned. If the input is not a string, 'undefined' will be returned. 74hilog.info(0x0000, 'testTag','Test Node-API get_value_string_utf8_string %{public}s', testNapi.getValueStringUtf8 ('aaBC+-$%^Hello 123'); 75hilog.info(0x0000, 'testTag', 'Test Node-API get_value_string_utf8_not_string %{public}s', testNapi.getValueStringUtf8(50)); 76``` 77 78### napi_create_string_utf8 79 80Use **napi_create_string_utf8** to create an ArkTS string from a UTF8-encoded C string. 81 82CPP code: 83 84```cpp 85#include "napi/native_api.h" 86#include <string> 87 88static napi_value CreateStringUtf8(napi_env env, napi_callback_info info) 89{ 90 const char *str = u8"Hello, World!, successes to create UTF-8 string! 111"; 91 size_t length = strlen(str); 92 napi_value result = nullptr; 93 napi_status status = napi_create_string_utf8(env, str, length, &result); 94 if (status != napi_ok) { 95 napi_throw_error(env, nullptr, "Failed to create UTF-8 string"); 96 return nullptr; 97 } 98 return result; 99} 100``` 101 102API declaration: 103 104```ts 105// index.d.ts 106export const createStringUtf8: () => string | void; 107``` 108 109ArkTS code: 110 111```ts 112import hilog from '@ohos.hilog' 113import testNapi from 'libentry.so' 114 115hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_string_utf8:%{public}s', testNapi.createStringUtf8()); 116``` 117 118### napi_get_value_string_utf16 119 120Use **napi_get_value_string_utf16** to convert an ArkTS string to a UTF-16-encoded string. 121 122CPP code: 123 124```cpp 125#include "napi/native_api.h" 126 127// Set the maximum length of the string buffer. 128static const int MAX_BUFFER_SIZE = 128; 129 130static napi_value GetValueStringUtf16(napi_env env, napi_callback_info info) 131{ 132 size_t argc = 1; 133 napi_value args[1]; 134 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 135 napi_value result = nullptr; 136 // Buffer of the string. 137 char16_t buffer[MAX_BUFFER_SIZE]; 138 // Size of the buffer for storing the string. 139 size_t bufferSize = MAX_BUFFER_SIZE; 140 // Length of the string. 141 size_t stringLen; 142 // Obtain the value and length of the string. 143 napi_get_value_string_utf16(env, args[0], buffer, bufferSize, &stringLen); 144 // Obtain the string. 145 napi_create_string_utf16(env, buffer, stringLen, &result); 146 // Return the result. 147 return result; 148} 149``` 150 151API declaration: 152 153```ts 154// index.d.ts 155export const getValueStringUtf16: (data: string) => string; 156``` 157 158ArkTS code: 159 160```ts 161import hilog from '@ohos.hilog' 162import testNapi from 'libentry.so' 163 164let result = testNapi.getValueStringUtf16('hello,'); 165hilog.info(0x0000,'testTag','Node-API napi_get_value_string_utf16:%{public}s', result); 166``` 167 168### napi_create_string_utf16 169 170Use **napi_create_string_utf16** to create an ArkTS string from a UTF16-encoded C string. 171 172CPP code: 173 174```cpp 175#include "napi/native_api.h" 176 177static napi_value CreateStringUtf16(napi_env env, napi_callback_info info) 178{ 179 const char16_t *str = u"Hello, World!, successes to create UTF-16 string! 111"; 180 size_t length = NAPI_AUTO_LENGTH; 181 napi_value result = nullptr; 182 napi_status status = napi_create_string_utf16(env, str, length, &result); 183 if (status != napi_ok) { 184 napi_throw_error(env, nullptr, "Failed to create UTF-16 string"); 185 return nullptr; 186 } 187 return result; 188} 189``` 190 191API declaration: 192 193```ts 194// index.d.ts 195export const createStringUtf16: () => string | void; 196``` 197 198ArkTS code: 199 200```ts 201import hilog from '@ohos.hilog' 202import testNapi from 'libentry.so' 203 204hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_string_utf16:%{public}s ', testNapi.createStringUtf16()); 205``` 206 207### napi_get_value_string_latin1 208 209Use **napi_get_value_string_latin1** to convert an ArkTS string into an ISO-8859-1-encoded string. 210 211CPP code: 212 213```cpp 214#include "napi/native_api.h" 215 216static const int MAX_BUFFER_SIZE = 128; 217 218static napi_value GetValueStringLatin1(napi_env env, napi_callback_info info) 219{ 220 size_t argc = 1; 221 napi_value args[1] = {nullptr}; 222 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 223 char buf[MAX_BUFFER_SIZE]; 224 size_t length = 0; 225 napi_value napi_Res = nullptr; 226 napi_status status = napi_get_value_string_latin1(env, args[0], buf, MAX_BUFFER_SIZE, &length); 227 // If the value passed in is not a string, napi_string_expected will be returned. 228 if (status == napi_string_expected) { 229 return nullptr; 230 } 231 napi_create_string_latin1(env, buf, length, &napi_Res); 232 return napi_Res; 233} 234``` 235 236API declaration: 237 238```ts 239// index.d.ts 240export const getValueStringLatin1: (param: number | string) => string | void; 241``` 242 243ArkTS code: 244 245```ts 246import hilog from '@ohos.hilog' 247import testNapi from 'libentry.so' 248// If non-character data is passed in, undefined will be returned. 249hilog.info(0x0000, 'testTag', 'Test Node-API get_value_string_latin1_not_string %{public}s', testNapi.getValueStringLatin1(10)); 250// The ISO-8859-1 encoding does not support Chinese characters. If Chinese characters are passed in, garbled characters will be displayed. 251hilog.info(0x0000, 'testTag','Test Node-API get_value_string_latin1_string_chinese %{public}s', testNapi.getValueStringLatin1 ('中文')); 252// Passing in characters of other languages will not cause garbled characters. 253hilog.info(0x0000, 'testTag', 'Test Node-API get_value_string_latin1_string %{public}s', testNapi.getValueStringLatin1('abo ABP=-&*/')); 254``` 255 256### napi_create_string_latin1 257 258Use **napi_create_string_latin1** to create an ArkTS string from an ISO-8859-1-encoded C string. 259 260CPP code: 261 262```cpp 263#include "napi/native_api.h" 264 265static napi_value CreateStringLatin1(napi_env env, napi_callback_info info) 266{ 267 const char *str = "Hello, World! éçñ, successes to create Latin1 string! 111"; 268 size_t length = NAPI_AUTO_LENGTH; 269 napi_value result = nullptr; 270 napi_status status = napi_create_string_latin1(env, str, length, &result); 271 if (status != napi_ok) { 272 // Error handling. 273 napi_throw_error(env, nullptr, "Failed to create Latin1 string"); 274 return nullptr; 275 } 276 return result; 277} 278``` 279 280API declaration: 281 282```ts 283// index.d.ts 284export const createStringLatin1: () => string | void; 285``` 286 287ArkTS code: 288 289```ts 290import hilog from '@ohos.hilog' 291import testNapi from 'libentry.so' 292 293hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_string_latin1:%{public}s', testNapi.createStringLatin1()); 294``` 295 296To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**. 297 298```text 299// CMakeLists.txt 300add_definitions( "-DLOG_DOMAIN=0xd0d0" ) 301add_definitions( "-DLOG_TAG=\"testTag\"" ) 302target_link_libraries(entry PUBLIC libhilog_ndk.z.so) 303``` 304<!--no_check-->