DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
BitConverter.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include <cstring>
4#include <iomanip>
5#include <sstream>
8
9namespace DotNetDupe {
10 namespace System {
11 const bool BitConverter::IsLittleEndian = true; // Most modern systems are little-endian
12
15 Array<byte> result(1);
16 result[0] = bValue ? 1 : 0;
17 return result;
18 }
19
22 Array<byte> result(1);
23 result[0] = (byte)chValue;
24 return result;
25 }
26
29 Array<byte> result(8);
30 std::memcpy(result.GetData(), &value, 8);
31 return result;
32 }
33
36 Array<byte> result(2);
37 std::memcpy(result.GetData(), &iValue, 2);
38 return result;
39 }
40
43 Array<byte> result(4);
44 std::memcpy(result.GetData(), &iValue, 4);
45 return result;
46 }
47
50 Array<byte> result(8);
51 std::memcpy(result.GetData(), &llValue, 8);
52 return result;
53 }
54
57 Array<byte> result(4);
58 std::memcpy(result.GetData(), &value, 4);
59 return result;
60 }
61
62 Array<byte> BitConverter::GetBytes(unsigned short iValue) {
64 Array<byte> result(2);
65 std::memcpy(result.GetData(), &iValue, 2);
66 return result;
67 }
68
69 Array<byte> BitConverter::GetBytes(unsigned int iValue) {
71 Array<byte> result(4);
72 std::memcpy(result.GetData(), &iValue, 4);
73 return result;
74 }
75
76 Array<byte> BitConverter::GetBytes(unsigned long long llValue) {
78 Array<byte> result(8);
79 std::memcpy(result.GetData(), &llValue, 8);
80 return result;
81 }
82
83 bool BitConverter::ToBoolean(Array<byte>& value, int iStartIndex) {
85 if (value.GetLength() == 0) throw ArgumentNullException("value");
86 if (iStartIndex < 0 || iStartIndex >= value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
88 return value[iStartIndex] != 0;
89 }
90
91 char BitConverter::ToChar(Array<byte>& value, int iStartIndex) {
93 if (value.GetLength() == 0) throw ArgumentNullException("value");
94 if (iStartIndex < 0 || iStartIndex >= value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
96 return (char)value[iStartIndex];
97 }
98
99 double BitConverter::ToDouble(Array<byte>& value, int iStartIndex) {
101 if (value.GetLength() == 0) throw ArgumentNullException("value");
102 if (iStartIndex < 0 || iStartIndex + 8 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
104 double result;
105 std::memcpy(&result, value.GetData() + iStartIndex, 8);
106 return result;
107 }
108
109 short BitConverter::ToInt16(Array<byte>& value, int iStartIndex) {
111 if (value.GetLength() == 0) throw ArgumentNullException("value");
112 if (iStartIndex < 0 || iStartIndex + 2 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
114 short iResult;
115 std::memcpy(&iResult, value.GetData() + iStartIndex, 2);
116 return iResult;
117 }
118
119 int BitConverter::ToInt32(Array<byte>& value, int iStartIndex) {
121 if (value.GetLength() == 0) throw ArgumentNullException("value");
122 if (iStartIndex < 0 || iStartIndex + 4 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
124 int iResult;
125 std::memcpy(&iResult, value.GetData() + iStartIndex, 4);
126 return iResult;
127 }
128
129 long long BitConverter::ToInt64(Array<byte>& value, int iStartIndex) {
131 if (value.GetLength() == 0) throw ArgumentNullException("value");
132 if (iStartIndex < 0 || iStartIndex + 8 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
134 long long llResult;
135 std::memcpy(&llResult, value.GetData() + iStartIndex, 8);
136 return llResult;
137 }
138
139 float BitConverter::ToSingle(Array<byte>& value, int iStartIndex) {
141 if (value.GetLength() == 0) throw ArgumentNullException("value");
142 if (iStartIndex < 0 || iStartIndex + 4 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
144 float result;
145 std::memcpy(&result, value.GetData() + iStartIndex, 4);
146 return result;
147 }
148
149 unsigned short BitConverter::ToUInt16(Array<byte>& value, int iStartIndex) {
151 if (value.GetLength() == 0) throw ArgumentNullException("value");
152 if (iStartIndex < 0 || iStartIndex + 2 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
154 unsigned short iResult;
155 std::memcpy(&iResult, value.GetData() + iStartIndex, 2);
156 return iResult;
157 }
158
159 unsigned int BitConverter::ToUInt32(Array<byte>& value, int iStartIndex) {
161 if (value.GetLength() == 0) throw ArgumentNullException("value");
162 if (iStartIndex < 0 || iStartIndex + 4 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
164 unsigned int iResult;
165 std::memcpy(&iResult, value.GetData() + iStartIndex, 4);
166 return iResult;
167 }
168
169 unsigned long long BitConverter::ToUInt64(Array<byte>& value, int iStartIndex) {
171 if (value.GetLength() == 0) throw ArgumentNullException("value");
172 if (iStartIndex < 0 || iStartIndex + 8 > value.GetLength()) throw ArgumentOutOfRangeException("iStartIndex");
174 unsigned long long llResult;
175 std::memcpy(&llResult, value.GetData() + iStartIndex, 8);
176 return llResult;
177 }
178
181 return ToString(value, 0, value.GetLength());
182 }
183
184 String BitConverter::ToString(Array<byte>& value, int iStartIndex) {
186 return ToString(value, iStartIndex, value.GetLength() - iStartIndex);
187 }
188
189 String BitConverter::ToString(Array<byte>& value, int iStartIndex, int nLength) {
191 int nArrayLength = value.GetLength();
192 if (iStartIndex < 0 || (iStartIndex >= nArrayLength && nArrayLength > 0))
193 throw ArgumentOutOfRangeException("iStartIndex");
194 if (nLength < 0)
195 throw ArgumentOutOfRangeException("nLength");
196 if (iStartIndex + nLength > nArrayLength)
197 throw ArgumentException("iStartIndex + nLength > value.Length");
198
199 if (nLength == 0) return String("");
200
202 std::stringstream ss;
203 for (int iIndex = 0; iIndex < nLength; ++iIndex) {
204 if (iIndex > 0) ss << "-";
205 ss << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (int)value [iStartIndex + iIndex];
206 }
207 return String(ss.str().c_str());
208 }
209
210 long long BitConverter::DoubleToInt64Bits(double value) {
212 long long llResult;
213 std::memcpy(&llResult, &value, sizeof(double));
214 return llResult;
215 }
216
217 double BitConverter::Int64BitsToDouble(long long llValue) {
219 double result;
220 std::memcpy(&result, &llValue, sizeof(long long));
221 return result;
222 }
223
226 int iResult;
227 std::memcpy(&iResult, &value, sizeof(float));
228 return iResult;
229 }
230
233 float result;
234 std::memcpy(&result, &iValue, sizeof(int));
235 return result;
236 }
237 }
238}
Defines the exception thrown when a null reference is passed to a method that does not accept it.
Defines the exception thrown when an argument value is outside the acceptable range of values.
Converts base data types to arrays of bytes, and arrays of bytes to base data types.
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
ArgumentNullException(const String &sMessage)
Initializes a new instance of the ArgumentNullException class with a specified error message.
ArgumentOutOfRangeException(const String &sMessage)
Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
T * GetData()
Gets a pointer to the contiguous internal element buffer.
Definition Array.h:146
static int ToInt32(Array< byte > &arrValue, int iStartIndex)
static const bool IsLittleEndian
Indicates whether this computer architecture is little-endian.
static double ToDouble(Array< byte > &arrValue, int iStartIndex)
static float Int32BitsToSingle(int iValue)
Reinterprets the specified 32-bit signed integer bit pattern as a 32-bit single-precision floating po...
static unsigned short ToUInt16(Array< byte > &arrValue, int iStartIndex)
static unsigned int ToUInt32(Array< byte > &arrValue, int iStartIndex)
static float ToSingle(Array< byte > &arrValue, int iStartIndex)
static String ToString(Array< byte > &arrValue)
Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecima...
static bool ToBoolean(Array< byte > &arrValue, int iStartIndex)
Returns a Boolean value converted from one byte at a specified position in a byte array.
static unsigned long long ToUInt64(Array< byte > &arrValue, int iStartIndex)
static long long DoubleToInt64Bits(double dValue)
Converts the specified 64-bit double-precision floating point number to a 64-bit signed integer bit p...
static int SingleToInt32Bits(float fValue)
Converts the specified 32-bit single-precision floating point number to a 32-bit signed integer bit p...
static short ToInt16(Array< byte > &arrValue, int iStartIndex)
static double Int64BitsToDouble(long long llValue)
Reinterprets the specified 64-bit signed integer bit pattern as a 64-bit double-precision floating po...
static char ToChar(Array< byte > &arrValue, int iStartIndex)
static long long ToInt64(Array< byte > &arrValue, int iStartIndex)
static Array< byte > GetBytes(bool bValue)
Returns the specified Boolean value as a byte array.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
unsigned char byte
Definition Buffer.h:18