-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
TetraUtils.cs
160 lines (126 loc) · 4.21 KB
/
TetraUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
namespace SDRSharp.Tetra
{
unsafe class TetraUtils
{
public static byte BitsToByte(byte* bitsBuffer, int offset, int length)
{
var result = (byte)0;
for (int i = 0; i < length; i++)
{
result <<= 1;
result |= (byte)(bitsBuffer[i + offset] & 1);
}
return result;
}
public static byte BitsToChar(byte* bitsBuffer, int offset, int length)
{
var result = (byte)0;
for (int i = 0; i < length; i++)
{
result >>= 1;
result |= (byte)((bitsBuffer[i + offset] & 1) << 7);
}
return result;
}
public static uint BitsToUInt32(byte* bitsBuffer, int offset, int length)
{
var result = (uint)0;
for (int i = 0; i < length; i++)
{
result <<= 1;
result |= (bitsBuffer[i + offset] & (uint)1);
}
return result;
}
public static int BitsToInt32(byte* bitsBuffer, int offset, int length)
{
var result = (int)0;
for (int i = 0; i < length; i++)
{
result <<= 1;
result |= (bitsBuffer[i + offset] & (int)1);
}
return result;
}
public static ulong BitsToULong(byte* bitsBuffer, int offset, int length)
{
var result = (ulong)0;
for (int i = 0; i < length; i++)
{
result <<= 1;
result |= (bitsBuffer[i + offset] & (ulong)1);
}
return result;
}
public static long BitsToLong(byte* bitsBuffer, int offset, int length)
{
var result = (long)0;
for (int i = 0; i < length; i++)
{
result <<= 1;
result |= (bitsBuffer[i + offset] & (long)1);
}
return result;
}
public static string BitsToString(byte* bitsBuffer, int offset, int length)
{
var result = String.Empty;
for (int i = 0; i < length; i++)
{
result += ((bitsBuffer[i + offset]) == 0) ? "0" : "1";
}
return result;
}
public static string BitsToString(sbyte* bitsBuffer, int offset, int length)
{
var result = String.Empty;
for (int i = 0; i < length; i++)
{
result += ((bitsBuffer[i + offset]) == 0) ? "-" : ((bitsBuffer[i + offset]) == -1) ? "0" : "1";
}
return result;
}
public static void ByteToBits(byte data, byte* bitsBuffer, int bufferOffset)
{
var length = 8 * sizeof(byte);
for (int i = 0; i < length; i++)
{
bitsBuffer[bufferOffset + i] = (byte)((data & 0x80) == 0 ? 0 : 1);
data <<= 1;
}
}
public static void IntToBits(int data, byte* bitsBuffer, int bufferOffset)
{
var length = 8 * sizeof(int);
for (int i = 0; i < length; i++)
{
bitsBuffer[bufferOffset + i] = (byte)((data & 0x80000000) == 0 ? 0 : 1);
data <<= 1;
}
}
public static void UIntToBits(UInt32 data, byte* bitsBuffer, int firstBitOffset)
{
var length = 8 * sizeof(int) - firstBitOffset;
var msb = 0x80000000 >> firstBitOffset;
for (int i = 0; i < length; i++)
{
bitsBuffer[i] = (byte)((data & msb) == 0 ? 0 : 1);
data <<= 1;
}
}
public static uint CreateScramblerCode(int mcc, int mnc, int colour)
{
mcc &= 0x3ff;
mnc &= 0x3fff;
colour &= 0x3f;
return (((uint)mcc << 22) | ((uint)mnc << 8) | ((uint)colour << 2) | 3);
}
public static uint CreateScramblerCode(int mnc, int sourceAddress)
{
mnc &= 0x3f;
sourceAddress &= 0xffffff;
return (((uint)mnc << 26) | ((uint)sourceAddress << 2) | 3);
}
}
}