OpenASIP 2.2
Loading...
Searching...
No Matches
Conversion.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file Conversion.cc
26 *
27 * Conversion static class contains functions for converting between
28 * different types, e.g. from any (possible) type to a string.
29 *
30 * @author Pekka Jääskeläinen 2003 (pjaaskel-no.spam-cs.tut.fi)
31 *
32 */
33#include <string>
34using std::string;
35
36#include <iostream>
37using std::istream;
38using std::hex;
39
40#include <sstream>
41using std::stringstream;
42
43#include <cstdio>
44
45#include "Conversion.hh"
46
47/**
48 * Returns true if rest of the stream is only white space.
49 *
50 * Given stream is read, that is, it is not in the same state as it
51 * was before the function call.
52 *
53 * @param str The stream to check.
54 * @return True if there's no garbage in stream.
55 */
56bool
57Conversion::restWhiteSpace(std::istream& str) {
58
59 string temp = "";
60 str >> temp;
61
62 if (!str.fail()) {
63 // if the stream contains non-white-space characters, the
64 // stream will not fail in the next read
65 return false;
66 }
67
68 return true;
69}
70
71
72/**
73 * Converts and integer to the string which is in binary format.
74 *
75 * Binary format includes a 'b' character at the end of the string.
76 *
77 * @param source An integer to be converted into a binary string.
78 * @return Returns the binary string.
79 */
80std::string
82 string result = toBinary(source);
83 result.append("b");
84 return result;
85}
86
87
88/**
89 * Converts a double to binary representation.
90 *
91 * @param source Double to be converted.
92 * @return Double as binary string.
93 */
94string
96
97 union castUnion {
98 double d;
99 struct {int w1; int w2;} s;
100 };
101
102 castUnion cast;
103 cast.d = source;
104
105 std::string result = "";
106
107 for (unsigned int i = 0; i < sizeof(int) * 8; i++) {
108
109 if ((cast.s.w1 & 0x80000000) != 0) {
110 result.append("1");
111 } else {
112 result.append("0");
113 }
114 cast.s.w1 <<= 1;
115 }
116
117 for (unsigned int i = 0; i < sizeof(int) * 8; i++) {
118
119 if ((cast.s.w2 & 0x80000000) != 0) {
120 result.append("1");
121 } else {
122 result.append("0");
123 }
124 cast.s.w2 <<= 1;
125 }
126
127 string::size_type firstOne = result.find("1");
128
129 if (firstOne != string::npos) {
130 result = result.substr(firstOne, string::npos);
131 } else {
132 result = "0";
133 }
134
135 result.append("b");
136
137 return result;
138}
139
140
141/**
142 * Converts an integer to the string which is in binary format.
143 *
144 * The width of the returned string is given too. If the width is
145 * greater than required, the number is zero-extended. If the width is
146 * smaller than required, the most significant bits are reduced. If '0' is
147 * given in stringWidth, the binary string is not extended, nor reduced. This
148 * is the default value.
149 *
150 * @param source An integer to be converted into a binary string.
151 * @param stringWidth The width of the returned string.
152 * @return The binary string.
153 */
154std::string
155Conversion::toBinary(unsigned int source, unsigned int stringWidth) {
156
157 std::string result = "";
158 for (unsigned int i = 0; i < sizeof(unsigned int) * 8; i++) {
159
160 if ((source & 0x80000000) != 0) {
161 result.append("1");
162 } else {
163 result.append("0");
164 }
165 source <<= 1;
166 }
167
168 if (stringWidth == 0) {
169 string::size_type firstOne = result.find("1");
170 if (firstOne != string::npos) {
171 result = result.substr(firstOne, string::npos);
172 } else {
173 result = "0";
174 }
175 } else {
176 if (result.length() > stringWidth) {
177 result = result.substr(
178 result.length() - stringWidth, string::npos);
179 } else if (result.length() < stringWidth) {
180 int zerosToAdd = stringWidth - result.length();
181 for (int i = 0; i < zerosToAdd; i++) {
182 result.insert(0, "0");
183 }
184 }
185 }
186
187 return result;
188}
189
190/**
191 * Converts bytes of a string hexadecimal value to target buffer.
192 *
193 * Hex value can be in "0x" format or without the prefix. Caller is
194 * responsible for checking that the target buffer has enough allocated
195 * memory for conversion.
196 *
197 * @param source Hex value.
198 * @param target Buffer where hex value bits are converted to.
199 */
200void
201Conversion::toRawData(const std::string& hexSource, unsigned char* target) {
202 std::string hexValue = hexSource;
203 std::stringstream hexStream;
204 hexStream << hexSource;
205 char first = hexStream.get();
206 char second = hexStream.get();
207
208 if (first == '0' && second == 'x') {
209 hexValue = hexSource.substr(2); // remove "0x"
210 }
211
212 // if number of hex numbers is uneven (e.g "12345FA"), insert additional
213 // 0 to the front so that the hex numbers form N full bytes
214 if (hexValue.size() % 2 == 1) {
215 hexValue.insert(0, "0");
216 }
217
218 // start filling the input parameter 2 hex numbers (byte) at a time to
219 // the data buffer
220 for (size_t i = 0; i < hexValue.size(); i=i+2) {
221 *target = toInt("0x" + hexValue.substr(i, 2));
222 ++target;
223 }
224}
225
226/**
227 * Specialized version of toHexString() for float type with fixed
228 * digit count (8).
229 */
230std::string
232 float source, bool include0x) {
233
234 union {
235 float f;
236 int i;
237 } conv;
238
239 conv.f = source;
240
241 std::stringstream str;
242 str << std::setw(8) << std::setfill('0') << std::right << std::hex
243 << conv.i;
244
245 std::string result = "";
246 str >> result;
247
248 if (include0x) {
249 result.insert(0, "0x");
250 }
251
252 return result;
253}
254
255/**
256 * Specialized version of toHexString() for double type with fixed
257 * digit count (16).
258 */
259std::string
260Conversion::doubleToHexString(double source, bool include0x) {
261
262 unsigned char* bytes = (unsigned char*) &source;
263 std::stringstream str;
264 if (include0x) {
265 str << "0x";
266 }
267 for (size_t i = 0; i < sizeof(double); i++) {
268#if HOST_BIGENDIAN == 1
269 unsigned int value = (unsigned int)bytes[i];
270#else
271 unsigned int value = (unsigned int)bytes[sizeof(double)-i-1];
272#endif
273 value &= 0xff;
274 str << std::setfill('0') << std::setw(2) << std::hex << value;
275 }
276 return std::string(str.str());
277}
278
static std::string toBinString(int source)
Definition Conversion.cc:81
static std::string floatToHexString(float source, bool include0x=true)
static void toRawData(const std::string &hexSource, unsigned char *target)
static std::string doubleToHexString(double source, bool include0x=true)
static std::string toBinary(unsigned int source, unsigned int stringWidth=0)
static int toInt(const T &source)
static bool restWhiteSpace(std::istream &str)
Definition Conversion.cc:57