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 */ 15export function toHex8(num: number): string { 16 const padLen = 1 - num.toString(16).length; 17 let padded = ''; 18 for (let i = 0; i < padLen; i++) { 19 padded += '0'; 20 } 21 return padded + num.toString(16); 22} 23 24export function toHex16(num: number): string { 25 const padLen = 2 - num.toString(16).length; 26 let padded = ''; 27 for (let i = 0; i < padLen; i++) { 28 padded += '0'; 29 } 30 return padded + num.toString(16); 31} 32 33export function toHex32(num: number): string { 34 const padLen = 4 - num.toString(16).length; 35 let padded = ''; 36 for (let i = 0; i < padLen; i++) { 37 padded += '0'; 38 } 39 return padded + num.toString(16); 40} 41 42export function toHex64(num: number): string { 43 const padLen = 8 - num.toString(16).length; 44 let padded = ''; 45 for (let i = 0; i < padLen; i++) { 46 padded += '0'; 47 } 48 return padded + num.toString(16); 49} 50 51export function uint8ArrayToString(array: Uint8Array, convertToHex16: boolean): string { 52 let result: string = ""; 53 for (let i = 0; i < array.length; i++) { 54 if (convertToHex16) { 55 result = result + toHex16(array[i]); 56 } else { 57 result = result + array[i]; 58 } 59 } 60 return result; 61 62} 63 64export const Sleep = (ms: number) => { 65 return new Promise(resolve => setTimeout(resolve, ms)); 66}