-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunicode.c
More file actions
130 lines (102 loc) · 1.7 KB
/
Copy pathunicode.c
File metadata and controls
130 lines (102 loc) · 1.7 KB
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
#include "unicode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * asciitou16(char *ascii){
char * ret = calloc(1,(strlen(ascii)*2) + 2);
char v;
int off = 1;
int i = 0;
int z = 1;
for(i=0;i<strlen(ascii);i++){
ret[z] = ascii[i];
z+=2;
}
return ret;
}
void print_u16(char *str){
int i = 0;
for(i=1;i<u16bytes(str);i+=2){
printf("%c",str[i]);
}
printf("\n");
}
int u16bytes(char *str){
int count = 0;
while(*str != *++str){
count++;
}
return count;
}
int tonum(char c){
switch(c){
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
return 10;
case 'A':
return 10;
case 'b':
return 11;
case 'B':
return 11;
case 'c':
return 12;
case 'C':
return 12;
case 'd':
return 13;
case 'D':
return 13;
case 'e':
return 14;
case 'E':
return 14;
case 'f':
return 15;
case 'F':
return 15;
default:
return -1;
}
}
unsigned char * tobytes(char *hex){
unsigned int length = strlen(hex);
// Check if even
if(length & 1 == 1){
return NULL;
}
unsigned char * out = malloc((length/2)+1);
int i = 0;
int v = 0;
for(i=0; i<strlen(hex);i+=2){
int a = tonum(hex[i]);
int b = tonum(hex[i+1]);
if(a == -1 || b == -1){
free(out);
return NULL;
}
int val = (a*16) + (b*1);
out[v] = (unsigned int)val;
v++;
}
return out;
}