blob: c684fb90fb48da08e5d58e11b650f6552d9cc6b8 [file] [log] [blame]
Neels Hofmeyrad4531a2020-02-25 03:19:26 +01001/* Copyright 2020 sysmocom s.f.m.c. GmbH
2 * SPDX-License-Identifier: Apache-2.0 */
3package org.osmocom.IMSIPseudo;
4
5public class Bytes {
6 public static byte nibble2hex(byte nibble)
7 {
8 nibble = (byte)(nibble & 0xf);
9 if (nibble < 0xa)
10 return (byte)('0' + nibble);
11 else
12 return (byte)('a' + nibble - 0xa);
13 }
14
15 public static byte[] hexdump(byte data[])
16 {
17 byte res[] = new byte[(byte)(data.length*2)];
18 for (byte i = 0; i < data.length; i++) {
19 res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
20 res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
21 }
22 return res;
23 }
24
25 public static boolean equals(byte a[], byte b[])
26 {
27 if (a.length != b.length)
28 return false;
29 for (short i = 0; i < (short)a.length; i++) {
30 if (a[i] != b[i])
31 return false;
32 }
33 return true;
34 }
35
36 public static boolean isDigit(byte digits[])
37 {
38 for (short i = 0; i < (short)digits.length; i++) {
39 if (digits[i] < '0' || digits[i] > '9')
40 return false;
41 }
42 return true;
43 }
44
45 public static byte[] toStr(byte byte_nr)
46 {
47 byte str[];
48 short nr = byte_nr;
49 byte d;
50 byte l = 0;
51 if (nr < 0) {
52 l = 1;
53 nr = (short)-nr;
54 }
55
56 if (nr > 99) {
57 l += 3;
58 d = 100;
59 }
60 else if (nr > 9) {
61 l += 2;
62 d = 10;
63 }
64 else {
65 str = new byte[1];
66 l += 1;
67 d = 1;
68 }
69
70 byte i = 0;
71 str = new byte[l];
72 if (byte_nr < 0)
73 str[i++] = '-';
74
75 while (d > 0) {
76 str[i++] = (byte)('0' + (nr / d));
77 nr %= d;
78 d /= 10;
79 }
80 return str;
81 }
82}