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