-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.c
43 lines (35 loc) · 1014 Bytes
/
helper.c
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
#include <stdlib.h>
int ispoweroftwo(unsigned short int num)
{
return (num != 0) && (num & (num - 1)) == 0;
}
unsigned short int* chararrtointarr(unsigned char* arr, int numBytes)
{
unsigned short int* intarr = (unsigned short int*)calloc((numBytes + 2 - 1) / 2, sizeof(short));
unsigned short int sFlag = 1;
unsigned short int temp;
for (int i = 0; i < numBytes; i++)
{
temp = arr[i];
temp = temp << (8 * sFlag);
intarr[i / 2] |= temp;
sFlag ^= 1;
}
free(arr);
return intarr;
}
unsigned char* intarrtochararr(unsigned short int* arr, int numBytes)
{
unsigned char* chararr = (unsigned char*)calloc(numBytes, sizeof(short));
unsigned short int sFlag = 1;
unsigned short int temp;
for (int i = 0; i < numBytes; i++)
{
temp = arr[i / 2];
temp = temp >> (8 * sFlag);
chararr[i] |= temp;
sFlag ^= 1;
}
free(arr);
return chararr;
}