-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertEndianess.cpp
More file actions
136 lines (113 loc) · 3.44 KB
/
Copy pathConvertEndianess.cpp
File metadata and controls
136 lines (113 loc) · 3.44 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
130
131
132
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned short swapByteOrder(const unsigned short& us)
{
return (us >> 8) |
(us << 8);
}
unsigned int swapByteOrder(const unsigned int& ui)
{
return (ui >> 24) |
((ui<<8) && 0x00FF0000) |
((ui>>8) && 0x0000FF00) |
(ui << 24);
}
unsigned long long swapByteOrder(const unsigned long long& ull)
{
return (ull >> 56) |
((ull<<40) && 0x00FF000000000000) |
((ull<<24) && 0x0000FF0000000000) |
((ull<<8) && 0x000000FF00000000) |
((ull>>8) && 0x00000000FF000000) |
((ull>>24) && 0x0000000000FF0000) |
((ull>>40) && 0x000000000000FF00) |
(ull <<56);
}
unsigned int change_endian(unsigned int x)
{
unsigned char *ptr = (unsigned char *)&x;
return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
}
int main (int argc, char**argv)
{
enum tType
{
eUndefined =0,
eInt,
eShort,
eLongLong
};
ifstream stream;
ofstream oStream;
unsigned int ch =0;
string filename;
string outfilename;
string Type = "int";
//show usage if no parameter given
if(1==argc)
{
cout << "Usage: ConvertEndianess <src> <target> [<byte type>] "
<< endl <<
" where <src>: Source binary file" << endl <<
" <target>: target binary file" << endl <<
" <byte type> the optional byte type. Must be one of [int, short, long] where int the the default"
<< endl;
}
//first parameter is the file to read
if(2<=argc)
{
filename = argv[1];
}
if(3<=argc)
{
outfilename = argv[2];
}
if(4<=argc)
{
Type = argv[3];
}
enum tType MyType = eUndefined;
if(0 == Type.compare("int"))
MyType = eInt;
else if(0 == Type.compare("short"))
MyType = eShort;
else if(0 == Type.compare("long"))
MyType = eLongLong;
if(eUndefined == MyType)
{
cout << "Could not determine correct type out of " << Type << endl;
return 3;
}
stream.open(filename.c_str(), ios_base::binary);
oStream.open(outfilename.c_str(), ios_base::binary);
if (stream.bad())
{
cout << "Kann Datei " << filename << " nicht öffnen" << endl;
return 1;
}
if (oStream.bad())
{
cout << "Kann Datei " << outfilename << " nicht öffnen" << endl;
return 2;
}
cout << endl << "Converting byte order from File " << filename << " and writing it to " << outfilename << " using type "<< Type << endl;
unsigned int newChUi = 0;
unsigned short newChUs = 0;
unsigned long long newChUll = 0;
while(stream.good())
{
ch = stream.get();
switch(MyType)
{
case eInt: newChUi = swapByteOrder(ch); oStream << newChUi;break;
case eShort: newChUs = swapByteOrder(ch); oStream << newChUs;break;
case eLongLong: newChUll = swapByteOrder(ch); oStream << newChUll;break;
default: cout << "Unknown Type Enum: " << MyType << endl;
}
}
cout << endl;
return 0;
}