-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.cs
101 lines (89 loc) · 2.9 KB
/
Util.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
using System;
namespace TPV
{
/// <summary>
/// Summary description for Util.
/// </summary>
public class Util
{
public Util()
{
//
// TODO: Add constructor logic here
//
}
public static Byte[] StringToBytes(String s, int maxLength)
{
Char[] cbuffer ;
if ( s.Length > maxLength )
cbuffer = s.ToCharArray(0, maxLength);
else
cbuffer = s.ToCharArray();
Byte[] bbuffer = new Byte[cbuffer.Length * 2];
for ( int i = 0 ; i < cbuffer.Length ; i++ )
{
Byte []temp = System.BitConverter.GetBytes(cbuffer[i]);
bbuffer[i*2] = temp[0] ;
bbuffer[i*2+1] = temp[1] ;
}
return bbuffer ;
}
public static String BytesToString(Byte[] bbuffer)
{
Char[] cbuffer = new Char[bbuffer.Length/2];
for ( int i = 0 ; i < bbuffer.Length ; i+=2 )
{
cbuffer[i/2] = System.BitConverter.ToChar(bbuffer, i);
}
return new String(cbuffer);
}
public static void Save(System.IO.FileStream file, String s, int maxLength)
{
Byte[] bbuffer = StringToBytes(s, maxLength);
Save(file, bbuffer.Length);
file.Write(bbuffer, 0, bbuffer.Length);
}
public static void Save(System.IO.FileStream file, int i)
{
Byte[] bbuffer = System.BitConverter.GetBytes(i);
file.Write(bbuffer, 0, bbuffer.Length);
}
public static void Save(System.IO.FileStream file, float f)
{
double d = (double)f;
Byte[] bbuffer = System.BitConverter.GetBytes(d);
file.Write(bbuffer, 0, bbuffer.Length);
}
public static void Save(System.IO.FileStream file, bool b)
{
Byte[] bbuffer = new Byte[1] ;
bbuffer[0] = (Byte)(b?1:0) ;
file.Write(bbuffer, 0, bbuffer.Length);
}
public static String LoadString(System.IO.FileStream file)
{
Int32 l = LoadInt(file);
byte[] bbuffer = new Byte[l];
file.Read(bbuffer, 0, l);
return BytesToString(bbuffer);
}
public static int LoadInt(System.IO.FileStream file)
{
byte[] bbuffer = new Byte[4];
file.Read(bbuffer, 0, 4);
return System.BitConverter.ToInt32(bbuffer, 0);
}
public static float LoadFloat(System.IO.FileStream file)
{
byte[] bbuffer = new Byte[8] ;
file.Read(bbuffer, 0, 8);
return (float)System.BitConverter.ToDouble(bbuffer, 0);
}
public static bool LoadBool(System.IO.FileStream file)
{
byte[] bbuffer = new Byte[1] ;
file.Read(bbuffer, 0, 1);
return (bbuffer[0]!=0)?true:false ;
}
}
}