Wolframe, 0.0.3

numberBaseConversion.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
34 
35 #ifndef _Wolframe_TYPES_NUMBER_BASE_CONVERSION_HPP_INCLUDED
36 #define _Wolframe_TYPES_NUMBER_BASE_CONVERSION_HPP_INCLUDED
37 #include "types/integer.hpp"
38 #include <string>
39 
40 namespace _Wolframe {
41 namespace types {
42 
49 unsigned int convertBinaryToBCD( const unsigned char* uintptr, unsigned int uintsize, unsigned char* digitsbuf, unsigned int digitsbufsize);
50 
57 void convertBCDtoBinary( const unsigned char* digits, unsigned int nofdigits, unsigned char* buf, unsigned int bufsize);
58 
65 template <typename UINTTYPE>
66 unsigned int convertBigEndianUintToBCD( const UINTTYPE& val, unsigned char* digitsbuf, unsigned int digitsbufsize)
67 {
68  return convertBinaryToBCD( (const unsigned char*)&val, sizeof(val), digitsbuf, digitsbufsize);
69 }
70 
77 template <typename UINTTYPE>
78 void convertBCDtoBigEndianUint( const unsigned char* digits, unsigned int nofdigits, UINTTYPE& val)
79 {
80  convertBCDtoBinary( digits, nofdigits, (unsigned char*)&val, sizeof(val));
81 }
82 
84 struct Endian
85 {
86  template <typename UINTTYPE>
87  static void reorder( UINTTYPE& num)
88  {
89  unsigned char* ref = (unsigned char*)&num;
90  unsigned int ii=0, nn=sizeof(UINTTYPE)/2;
91  for (; ii<nn; ++ii)
92  {
93  unsigned char tmp = ref[ii];
94  ref[ii] = ref[ sizeof(UINTTYPE)-ii-1];
95  ref[ sizeof(UINTTYPE)-ii-1] = tmp;
96  }
97  }
98 };
99 
100 }}//namespace
101 #endif
102 
Application wide definitions of integer number value types.
static void reorder(UINTTYPE &num)
Definition: numberBaseConversion.hpp:87
unsigned int convertBinaryToBCD(const unsigned char *uintptr, unsigned int uintsize, unsigned char *digitsbuf, unsigned int digitsbufsize)
Convert a large but limited size big endian integer value to a BCD number.
Class for little/big endian operations.
Definition: numberBaseConversion.hpp:84
void convertBCDtoBinary(const unsigned char *digits, unsigned int nofdigits, unsigned char *buf, unsigned int bufsize)
Convert a large but limited size BCD number to a big endian integer value.
unsigned int convertBigEndianUintToBCD(const UINTTYPE &val, unsigned char *digitsbuf, unsigned int digitsbufsize)
Convert a large but limited size big endian integer value to a BCD number.
Definition: numberBaseConversion.hpp:66
void convertBCDtoBigEndianUint(const unsigned char *digits, unsigned int nofdigits, UINTTYPE &val)
Convert a large but limited size BCD number to a big endian integer value.
Definition: numberBaseConversion.hpp:78