1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciexport function toHex8(num: number): string {
17fb726d48Sopenharmony_ci  const padLen = 1 - num.toString(16).length;
18fb726d48Sopenharmony_ci  let padded = '';
19fb726d48Sopenharmony_ci  for (let i = 0; i < padLen; i++) {
20fb726d48Sopenharmony_ci    padded += '0';
21fb726d48Sopenharmony_ci  }
22fb726d48Sopenharmony_ci  return padded + num.toString(16);
23fb726d48Sopenharmony_ci}
24fb726d48Sopenharmony_ci
25fb726d48Sopenharmony_ciexport function toHex16(num: number): string {
26fb726d48Sopenharmony_ci  const padLen = 2 - num.toString(16).length;
27fb726d48Sopenharmony_ci  let padded = '';
28fb726d48Sopenharmony_ci  for (let i = 0; i < padLen; i++) {
29fb726d48Sopenharmony_ci    padded += '0';
30fb726d48Sopenharmony_ci  }
31fb726d48Sopenharmony_ci  return padded + num.toString(16);
32fb726d48Sopenharmony_ci}
33fb726d48Sopenharmony_ci
34fb726d48Sopenharmony_ciexport function toHex32(num: number): string {
35fb726d48Sopenharmony_ci  const padLen = 4 - num.toString(16).length;
36fb726d48Sopenharmony_ci  let padded = '';
37fb726d48Sopenharmony_ci  for (let i = 0; i < padLen; i++) {
38fb726d48Sopenharmony_ci    padded += '0';
39fb726d48Sopenharmony_ci  }
40fb726d48Sopenharmony_ci  return padded + num.toString(16);
41fb726d48Sopenharmony_ci}
42fb726d48Sopenharmony_ci
43fb726d48Sopenharmony_ciexport function toHex64(num: number): string {
44fb726d48Sopenharmony_ci  const padLen = 8 - num.toString(16).length;
45fb726d48Sopenharmony_ci  let padded = '';
46fb726d48Sopenharmony_ci  for (let i = 0; i < padLen; i++) {
47fb726d48Sopenharmony_ci    padded += '0';
48fb726d48Sopenharmony_ci  }
49fb726d48Sopenharmony_ci  return padded + num.toString(16);
50fb726d48Sopenharmony_ci}
51fb726d48Sopenharmony_ci
52fb726d48Sopenharmony_ciexport function uint8ArrayToString(array: Uint8Array, convertToHex16: boolean): string {
53fb726d48Sopenharmony_ci  let result: string = '';
54fb726d48Sopenharmony_ci  for (let i = 0; i < array.length; i++) {
55fb726d48Sopenharmony_ci    if (convertToHex16) {
56fb726d48Sopenharmony_ci      result = result + toHex16(array[i]);
57fb726d48Sopenharmony_ci    } else {
58fb726d48Sopenharmony_ci      result = result + array[i];
59fb726d48Sopenharmony_ci    }
60fb726d48Sopenharmony_ci  }
61fb726d48Sopenharmony_ci  return result;
62fb726d48Sopenharmony_ci}
63fb726d48Sopenharmony_ci
64fb726d48Sopenharmony_ciexport const Sleep = (ms: number): Promise<unknown> => {
65fb726d48Sopenharmony_ci  return new Promise((resolve) => setTimeout(resolve, ms));
66fb726d48Sopenharmony_ci};
67