BitConverter.cpp


SUBMITTED BY: Guest

DATE: Nov. 12, 2013, 2:50 a.m.

FORMAT: Text only

SIZE: 6.8 kB

HITS: 1509

  1. /*
  2. Copyright (C) 2012 Jonas Penno
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License,
  6. or any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see http://www.gnu.org/licenses.
  13. The Boost C++ Libraries are distributed under the Boost Software License, Version 1.0.
  14. See also http://www.boost.org/LICENSE_1_0.txt.
  15. ***********************************************************
  16. ** Codename Visual Dragon++ **
  17. ** Version: 1.0 Beta **
  18. ** Copyright (c) 2012 Jonas Penno **
  19. ** GNU General Public License **
  20. ** Boost C++ Libraries **
  21. ** http://cvd-project.org **
  22. ***********************************************************
  23. */
  24. #include "BitConverter.hpp"
  25. namespace Codename_Visual_Dragon
  26. {
  27. char BitConverter::GetHexValue(boost::int32_t Value)
  28. {
  29. if (Value < 10)
  30. return (char)(Value + 0x30);
  31. return (char)((Value - 10) + 0x41);
  32. };
  33. boost::int32_t BitConverter::FloatToInt32Bits(float Value)
  34. {
  35. return *(((boost::int32_t*)&Value));
  36. };
  37. boost::int64_t BitConverter::DoubleToInt64Bits(double Value)
  38. {
  39. return *(((boost::int64_t*)&Value));
  40. };
  41. char* BitConverter::GetBytes(bool Value)
  42. {
  43. char BoolAlpha[1] = { (Value ? ((char)1) : ((char)0)) };
  44. return BoolAlpha;
  45. };
  46. char* BitConverter::GetBytes(float Value)
  47. {
  48. return GetBytes(*((boost::int32_t*)&Value));
  49. };
  50. char* BitConverter::GetBytes(double Value)
  51. {
  52. return GetBytes(*((boost::int64_t*)&Value));
  53. };
  54. char* BitConverter::GetBytes(boost::int16_t Value)
  55. {
  56. char* Buffer = new char[2];
  57. *((boost::int16_t*)Buffer) = Value;
  58. return Buffer;
  59. };
  60. char* BitConverter::GetBytes(boost::int32_t Value)
  61. {
  62. char* Buffer = new char[4];
  63. *((boost::int32_t*)Buffer) = Value;
  64. return Buffer;
  65. };
  66. char* BitConverter::GetBytes(boost::int64_t Value)
  67. {
  68. char* Buffer = new char[8];
  69. *((boost::int64_t*)Buffer) = Value;
  70. return Buffer;
  71. };
  72. char* BitConverter::GetBytes(boost::uint16_t Value)
  73. {
  74. return GetBytes((boost::int16_t)Value);
  75. };
  76. char* BitConverter::GetBytes(boost::uint32_t Value)
  77. {
  78. return GetBytes((boost::int32_t)Value);
  79. };
  80. char* BitConverter::GetBytes(boost::uint64_t Value)
  81. {
  82. return GetBytes((boost::int64_t)Value);
  83. };
  84. float BitConverter::Int32BitsToFloat(boost::int32_t Value)
  85. {
  86. return *(((float*)&Value));
  87. };
  88. double BitConverter::Int64BitsToDouble(boost::int64_t Value)
  89. {
  90. return *(((double*)&Value));
  91. };
  92. bool BitConverter::ToBoolean(char* Value, boost::uint32_t StartIndex)
  93. {
  94. if (Value == nullptr)
  95. return 0;
  96. if (StartIndex < 0)
  97. return false;
  98. if (StartIndex > (sizeof(Value) - 1))
  99. return false;
  100. return (Value[StartIndex] != 0);
  101. };
  102. float BitConverter::ToFloat(char* Value, boost::uint32_t StartIndex)
  103. {
  104. if (Value == nullptr)
  105. return 0;
  106. boost::int32_t IValue = ToInt32(Value, StartIndex);
  107. return *(((float*)&IValue));
  108. };
  109. double BitConverter::ToDouble(char* Value, boost::uint32_t StartIndex)
  110. {
  111. if (Value == nullptr)
  112. return 0;
  113. boost::int64_t IValue = ToInt64(Value, StartIndex);
  114. return *(((double*)&IValue));
  115. };
  116. boost::int16_t BitConverter::ToInt16(char* Value, boost::uint32_t StartIndex)
  117. {
  118. if (Value == nullptr)
  119. return 0;
  120. if (StartIndex >= sizeof(Value))
  121. return 0;
  122. if (StartIndex > (sizeof(Value) - 2))
  123. return 0;
  124. char* Buffer = &(Value[StartIndex]);
  125. if ((StartIndex % 2) == 0)
  126. return *(((boost::int16_t*)Buffer));
  127. #if defined(LITTLE_ENDIAN)
  128. return (boost::int16_t)(Buffer[0] | (Buffer[1] << 8));
  129. #else
  130. return (boost::int16_t)((Buffer[0] << 8) | Buffer[1]);
  131. #endif
  132. };
  133. boost::int32_t BitConverter::ToInt32(char* Value, boost::uint32_t StartIndex)
  134. {
  135. if (Value == nullptr)
  136. return 0;
  137. if (StartIndex >= sizeof(Value))
  138. return 0;
  139. if (StartIndex > (sizeof(Value) - 4))
  140. return 0;
  141. char* Buffer = &(Value[StartIndex]);
  142. if ((StartIndex % 4) == 0)
  143. return *(((boost::int32_t*)Buffer));
  144. #if defined(LITTLE_ENDIAN)
  145. return (boost::int32_t)(((Buffer[0] | (Buffer[1] << 8)) | (Buffer[2] << 0x10)) | (Buffer[3] << 0x18));
  146. #else
  147. return (boost::int32_t)((((Buffer[0] << 0x18) | (Buffer[1] << 0x10)) | (Buffer[2] << 8)) | Buffer[3]);
  148. #endif
  149. };
  150. boost::int64_t BitConverter::ToInt64(char* Value, boost::uint32_t StartIndex)
  151. {
  152. if (Value == nullptr)
  153. return 0;
  154. if (StartIndex >= sizeof(Value))
  155. return 0;
  156. if (StartIndex > (sizeof(Value) - 8))
  157. return 0;
  158. char* Buffer = &(Value[StartIndex]);
  159. if ((StartIndex % 8) == 0)
  160. return *(((boost::int64_t*) Buffer));
  161. #if defined(LITTLE_ENDIAN)
  162. boost::int32_t Num1 = (boost::int32_t)((Buffer[0] | (Buffer[1] << 8)) | (Buffer[2] << 0x10)) | (Buffer[3] << 0x18);
  163. boost::int32_t Num2 = (boost::int32_t)((Buffer[4] | (Buffer[5] << 8)) | (Buffer[6] << 0x10)) | (Buffer[7] << 0x18);
  164. return (((boost::int64_t)((boost::uint64_t)Num1)) | (Num2 << 0x20));
  165. #else
  166. boost::int32_t Num3 = (boost::int32_t)(((Buffer[0] << 0x18) | (Buffer[1] << 0x10)) | (Buffer[2] << 8)) | Buffer[3];
  167. boost::int32_t Num4 = (boost::int32_t)(((Buffer[4] << 0x18) | (Buffer[5] << 0x10)) | (Buffer[6] << 8)) | Buffer[7];
  168. return (((boost::int64_t)((boost::uint64_t)Num4)) | (Num3 << 0x20));
  169. #endif
  170. };
  171. boost::uint16_t BitConverter::ToUInt16(char* Value, boost::uint32_t StartIndex)
  172. {
  173. return (boost::uint16_t)ToInt16(Value, StartIndex);
  174. };
  175. boost::uint32_t BitConverter::ToUInt32(char* Value, boost::uint32_t StartIndex)
  176. {
  177. return (boost::uint32_t)ToInt32(Value, StartIndex);
  178. };
  179. boost::uint64_t BitConverter::ToUInt64(char* Value, boost::uint32_t StartIndex)
  180. {
  181. return (boost::uint64_t)ToInt64(Value, StartIndex);
  182. };
  183. };

comments powered by Disqus