-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversion.cpp
71 lines (63 loc) · 1.12 KB
/
conversion.cpp
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
#include "conversion.h"
#include <cstdlib>
int chartoint (char* c) {
int x = 0;
for(int i = 0; i < 4; i++) {
x <<= 8;
x += (unsigned char) c[i];
}
return x;
}
char* inttochar (int x) {
char* c = (char*)malloc(4);
for(int i = 3; i>= 0; i--) {
c[i] = (char)x;
x = x >> 8;
}
return c;
}
float chartofloat (char* c) {
int x = chartoint(c);
float f = *(float*)&x;
return f;
}
char* floattochar (float f) {
int x = *(int*)&f;
return inttochar(x);
}
double chartodouble (char* c) {
long long x;
for(int i=0; i<8; i++) {
x = x << 8;
x += (unsigned char)c[i];
}
return *(double*)&x;
}
char* longtochar (long long l) {
char* c = (char*)malloc(8);
for(int i=7; i>=0; i--) {
c[i] = (char)l;
l = l >> 8;
}
return c;
}
char* doubletochar (double d) {
long long l = *(long long*)&d;
return longtochar(l);
}
int chartodir (char* c) {
int x = 0;
for(int i = 0; i < 2; i++) {
x <<= 8;
x += (unsigned char)c[i];
}
return x;
}
char* dirtochar (int x) {
char* c = (char*)malloc(2);
for(int i = 1; i>= 0; i--) {
c[i] = (char)x;
x = x >> 8;
}
return c;
}