1e41f4b71Sopenharmony_ci# Working with BigInt Using Node-API
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciBigInt is a data type used to represent integers of any precision in ArkTS, with values greater than the value range of the Number type. You can use Node-API to create, obtain, and operate ArkTS BigInt values.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Basic Concepts
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciBefore using Node-API to operate BigInt values, you need to understand the following basic concepts:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci- BigInt: a data type used to represent integers of any precision in ArkTS. Different from the Number type, BigInt can accurately represent very large integers without losing precision or causing overflows.
12e41f4b71Sopenharmony_ci- BigInt creation: You can use Node-API to create a ArkTS BigInt object from a C **Int64** or **Uint64** value. This makes it easy to create BigInt values using C/C++.
13e41f4b71Sopenharmony_ci- BigInt operation: Node-API provides APIs for operating BigInt values. You can use these APIs to obtain and convert BigInt values and perform arithmetic and bitwise operations.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Available APIs
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| API| Description|
18e41f4b71Sopenharmony_ci| -------- | -------- |
19e41f4b71Sopenharmony_ci| napi_create_bigint_int64 | Creates an ArkTS BigInt object from a signed 64-bit integer in C/C++.|
20e41f4b71Sopenharmony_ci| napi_create_bigint_uint64 | Creates an ArkTS BigInt object from an unsigned 64-bit integer in C/C++.|
21e41f4b71Sopenharmony_ci| napi_create_bigint_words | Creates an ArkTS BigInt object from an array of unsigned 64-bit byte data in C/C++.|
22e41f4b71Sopenharmony_ci| napi_get_value_bigint_int64 | Obtains a signed 64-bit integer from an ArkTS BigInt object.|
23e41f4b71Sopenharmony_ci| napi_get_value_bigint_uint64 | Obtains an unsigned 64-bit integer from an ArkTS BigInt object.|
24e41f4b71Sopenharmony_ci| napi_get_value_bigint_words | Obtains the underlying 64-bit unsigned (uint64) byte data from an ArkTS BigInt object.|
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci## Example
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciIf 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 related to BigInt conversions.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci### napi_create_bigint_int64
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ciUse **napi_create_bigint_int64** to create an ArkTS BigInt object from a signed 64-bit integer in C/C++.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciCPP code:
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci```cpp
38e41f4b71Sopenharmony_ci#include "napi/native_api.h"
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_cistatic napi_value CreateBigintInt64t(napi_env env, napi_callback_info info)
41e41f4b71Sopenharmony_ci{
42e41f4b71Sopenharmony_ci    // Declare the int64_t variable value.
43e41f4b71Sopenharmony_ci    int64_t value = -5555555555555555555;
44e41f4b71Sopenharmony_ci    // Convert the value to the napi_value type and return napi_value.
45e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
46e41f4b71Sopenharmony_ci    napi_create_bigint_int64(env, value, &returnValue);
47e41f4b71Sopenharmony_ci    return returnValue;
48e41f4b71Sopenharmony_ci}
49e41f4b71Sopenharmony_ci```
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciAPI declaration:
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci```ts
54e41f4b71Sopenharmony_ci// index.d.ts
55e41f4b71Sopenharmony_ciexport const createBigintInt64t: () => bigint;
56e41f4b71Sopenharmony_ci```
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciArkTS code:
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci```ts
61e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
62e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_cihilog.info(0x0000, 'testTag', 'Test Node-API napi_create_bigint_int64: %{public}d', testNapi.createBigintInt64t());
65e41f4b71Sopenharmony_ci```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci### napi_create_bigint_uint64
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ciUse **napi_create_bigint_uint64** to create an ArkTS BigInt object from an unsigned 64-bit integer in C/C++.
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ciCPP code:
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci```cpp
74e41f4b71Sopenharmony_ci#include "napi/native_api.h"
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_cistatic napi_value CreateBigintUint64t(napi_env env, napi_callback_info info)
77e41f4b71Sopenharmony_ci{
78e41f4b71Sopenharmony_ci    // Declare the uint64_t variable value.
79e41f4b71Sopenharmony_ci    uint64_t value = 5555555555555555555;
80e41f4b71Sopenharmony_ci    // Convert the value to the napi_value type and return napi_value.
81e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
82e41f4b71Sopenharmony_ci    napi_create_bigint_uint64(env, value, &returnValue);
83e41f4b71Sopenharmony_ci    return returnValue;
84e41f4b71Sopenharmony_ci}
85e41f4b71Sopenharmony_ci```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ciAPI declaration:
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci```ts
90e41f4b71Sopenharmony_ci// index.d.ts
91e41f4b71Sopenharmony_ciexport const createBigintUint64t: () => bigint;
92e41f4b71Sopenharmony_ci```
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ciArkTS code:
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci```ts
97e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
98e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_cihilog.info(0x0000, 'testTag', 'Test Node-API napi_create_bigint_uint64: %{public}d', testNapi.createBigintUint64t());
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci### napi_create_bigint_words
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ciUse **napi_create_bigint_words** to create an ArkTS BigInt object based on the given byte data.
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciCPP code:
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci```cpp
110e41f4b71Sopenharmony_ci#include "napi/native_api.h"
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_cistatic napi_value CreateBigintWords(napi_env env, napi_callback_info info)
113e41f4b71Sopenharmony_ci{
114e41f4b71Sopenharmony_ci    // Call napi_create_bigint_words to create a BigInt object.
115e41f4b71Sopenharmony_ci    int signBit = 0;
116e41f4b71Sopenharmony_ci    size_t wordCount = 3;
117e41f4b71Sopenharmony_ci    uint64_t words[] = {12ULL, 34ULL, 56ULL};
118e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
119e41f4b71Sopenharmony_ci    napi_status status = napi_create_bigint_words(env, signBit, wordCount, words, &returnValue);
120e41f4b71Sopenharmony_ci    if (status != napi_ok) {
121e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_create_bigint_words fail");
122e41f4b71Sopenharmony_ci        return nullptr;
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci    return returnValue;
125e41f4b71Sopenharmony_ci}
126e41f4b71Sopenharmony_ci```
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciAPI declaration:
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci```ts
131e41f4b71Sopenharmony_ci// index.d.ts
132e41f4b71Sopenharmony_ciexport const createBigintWords: () => bigint | void;
133e41f4b71Sopenharmony_ci```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ciArkTS code:
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci```ts
138e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
139e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
140e41f4b71Sopenharmony_citry {
141e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_bigint_words: %{public}d', testNapi.createBigintWords());
142e41f4b71Sopenharmony_ci} catch (error) {
143e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API NapiGetValueBigint: %{public}s', error.message);
144e41f4b71Sopenharmony_ci}
145e41f4b71Sopenharmony_ci```
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci### napi_get_value_bigint_int64
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ciUse **napi_get_value_bigint_int64** to obtain a signed 64-bit integer from an ArkTS BigInt object.
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ciCPP code:
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci```cpp
154e41f4b71Sopenharmony_ci#include "napi/native_api.h"
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_cistatic napi_value GetValueBigintInt64t(napi_env env, napi_callback_info info)
157e41f4b71Sopenharmony_ci{
158e41f4b71Sopenharmony_ci    size_t argc = 1;
159e41f4b71Sopenharmony_ci    napi_value args[1] = {nullptr};
160e41f4b71Sopenharmony_ci    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
161e41f4b71Sopenharmony_ci    // Obtain the 64-bit big integer from the input parameter.
162e41f4b71Sopenharmony_ci    int64_t value = 0;
163e41f4b71Sopenharmony_ci    bool lossLess = false;
164e41f4b71Sopenharmony_ci    napi_status status = napi_get_value_bigint_int64(env, args[0], &value, &lossLess);
165e41f4b71Sopenharmony_ci    // Check whether the BigInt value obtained is a product of lossless conversion. If no, throw an exception.
166e41f4b71Sopenharmony_ci    if (!lossLess) {
167e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "BigInt values have not been lossless converted");
168e41f4b71Sopenharmony_ci        return nullptr;
169e41f4b71Sopenharmony_ci    }
170e41f4b71Sopenharmony_ci    // If the API is successfully called, return true to ArkTS.
171e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
172e41f4b71Sopenharmony_ci    napi_get_boolean(env, status == napi_ok, &returnValue);
173e41f4b71Sopenharmony_ci    return returnValue;
174e41f4b71Sopenharmony_ci}
175e41f4b71Sopenharmony_ci```
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ciAPI declaration:
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci```ts
180e41f4b71Sopenharmony_ci// index.d.ts
181e41f4b71Sopenharmony_ciexport const getValueBigintInt64t: (bigInt64: bigint) => boolean | void;
182e41f4b71Sopenharmony_ci```
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ciArkTS code:
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci```ts
187e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
188e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
189e41f4b71Sopenharmony_cilet bigInt = BigInt(-5555555555555555);
190e41f4b71Sopenharmony_citry {
191e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_value_bigint_int64: %{public}s',
192e41f4b71Sopenharmony_ci             JSON.stringify(testNapi.getValueBigintInt64t(bigInt)));
193e41f4b71Sopenharmony_ci} catch (error) {
194e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API NapiGetValueBigint: %{public}s', error.message);
195e41f4b71Sopenharmony_ci}
196e41f4b71Sopenharmony_ci```
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci### napi_get_value_bigint_uint64
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ciUse **napi_get_value_bigint_uint64** to obtain an unsigned 64-bit integer from an ArkTS BigInt object.
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ciCPP code:
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci```cpp
205e41f4b71Sopenharmony_ci#include "napi/native_api.h"
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_cistatic napi_value GetValueBigintUint64t(napi_env env, napi_callback_info info)
208e41f4b71Sopenharmony_ci{
209e41f4b71Sopenharmony_ci    size_t argc = 1;
210e41f4b71Sopenharmony_ci    napi_value args[1] = {nullptr};
211e41f4b71Sopenharmony_ci    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
212e41f4b71Sopenharmony_ci    // Obtain the BigInt value.
213e41f4b71Sopenharmony_ci    uint64_t value = 0;
214e41f4b71Sopenharmony_ci    bool lossLess = false;
215e41f4b71Sopenharmony_ci    napi_status status = napi_get_value_bigint_uint64(env, args[0], &value, &lossLess);
216e41f4b71Sopenharmony_ci    // Check whether the BigInt value obtained is a product of lossless conversion. If no, throw an exception.
217e41f4b71Sopenharmony_ci    if (!lossLess) {
218e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "BigInt values have no lossless converted");
219e41f4b71Sopenharmony_ci        return nullptr;
220e41f4b71Sopenharmony_ci    }
221e41f4b71Sopenharmony_ci    // If the API is successfully called, return true to ArkTS.
222e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
223e41f4b71Sopenharmony_ci    napi_get_boolean(env, status == napi_ok, &returnValue);
224e41f4b71Sopenharmony_ci    return returnValue;
225e41f4b71Sopenharmony_ci}
226e41f4b71Sopenharmony_ci```
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ciAPI declaration:
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci```ts
231e41f4b71Sopenharmony_ci// index.d.ts
232e41f4b71Sopenharmony_ciexport const getValueBigintUint64t: (bigUint64: bigint) => boolean | void;
233e41f4b71Sopenharmony_ci```
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ciArkTS code:
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci```ts
238e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
239e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
240e41f4b71Sopenharmony_cilet bigUint = BigInt(5555555555555555);
241e41f4b71Sopenharmony_citry {
242e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_value_bigint_uint64: %{public}s',
243e41f4b71Sopenharmony_ci             JSON.stringify(testNapi.getValueBigintUint64t(bigUint)));
244e41f4b71Sopenharmony_ci} catch (error) {
245e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API NapiGetValueBigint: %{public}s', error.message);
246e41f4b71Sopenharmony_ci}
247e41f4b71Sopenharmony_ci```
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci### napi_get_value_bigint_words
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ciUse **napi_get_value_bigint_words** to obtain the underlying unsigned 64-bit (uint64) binary byte data from an ArkTS BigInt object.
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ciCPP code:
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci```cpp
256e41f4b71Sopenharmony_ci#include "hilog/log.h"
257e41f4b71Sopenharmony_ci#include "napi/native_api.h"
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_cistatic napi_value GetValueBigintWords(napi_env env, napi_callback_info info)
260e41f4b71Sopenharmony_ci{
261e41f4b71Sopenharmony_ci    size_t argc = 1;
262e41f4b71Sopenharmony_ci    napi_value args[1] = {nullptr};
263e41f4b71Sopenharmony_ci    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
264e41f4b71Sopenharmony_ci    int signBit = 0;
265e41f4b71Sopenharmony_ci    size_t wordCount = 0;
266e41f4b71Sopenharmony_ci    uint64_t words = 0;
267e41f4b71Sopenharmony_ci    // Call napi_get_value_bigint_words to obtain wordCount.
268e41f4b71Sopenharmony_ci    napi_status status = napi_get_value_bigint_words(env, args[0], nullptr, &wordCount, nullptr);
269e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "Node-API , wordCount:%{public}d.", wordCount);
270e41f4b71Sopenharmony_ci    // Call napi_get_value_bigint_words to obtain BigInt information, such as whether the value passed by signBit is a positive or negative number.
271e41f4b71Sopenharmony_ci    status = napi_get_value_bigint_words(env, args[0], &signBit, &wordCount, &words);
272e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "Node-API , signBit: %{public}d.", signBit);
273e41f4b71Sopenharmony_ci    if (status != napi_ok) {
274e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "Node-API , reason:%{public}d.", status);
275e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_get_date_value fail");
276e41f4b71Sopenharmony_ci        return nullptr;
277e41f4b71Sopenharmony_ci    }
278e41f4b71Sopenharmony_ci    // Convert the sign bit into a value of Int type and pass it.
279e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
280e41f4b71Sopenharmony_ci    napi_create_int32(env, signBit, &returnValue);
281e41f4b71Sopenharmony_ci    return returnValue;
282e41f4b71Sopenharmony_ci}
283e41f4b71Sopenharmony_ci```
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ciAPI declaration:
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci```ts
288e41f4b71Sopenharmony_ci// index.d.ts
289e41f4b71Sopenharmony_ciexport const getValueBigintWords: (bigIntWords: bigint) => bigint | void;
290e41f4b71Sopenharmony_ci```
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ciArkTS code:
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci```ts
295e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
296e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
297e41f4b71Sopenharmony_cilet bigInt = BigInt(-5555555555555555);
298e41f4b71Sopenharmony_cilet bigUint = BigInt(5555555555555555);
299e41f4b71Sopenharmony_citry {
300e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_value_bigint_words signBit is: %{public}d', testNapi.getValueBigintWords(bigInt));
301e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_value_bigint_words signBit is: %{public}d', testNapi.getValueBigintWords(bigUint));
302e41f4b71Sopenharmony_ci} catch (error) {
303e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API NapiGetValueBigint: %{public}s', error.message);
304e41f4b71Sopenharmony_ci}
305e41f4b71Sopenharmony_ci```
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ciTo 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"**.
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci```text
310e41f4b71Sopenharmony_ci// CMakeLists.txt
311e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_DOMAIN=0xd0d0" )
312e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_TAG=\"testTag\"" )
313e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so)
314e41f4b71Sopenharmony_ci```
315