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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
bool GetHexFromSF( Text text, void* buffer, int size, int mode )
{
// Cancel function if size is to small
if ( size < 2 || size > 18 ) return false;
// Check if can access last byte
int b = size - 1;
ui8* buff = ( ui8* )buffer;
try { buff[ b ] = 0u; }
catch ( void* any_error ) { return false; }
// Check size is of standard
int eBits = 0, mBits = 0;
switch ( mode )
{
case HEX_SF_IEEE754:
{
if ( !GetHexFromSF_IEEE754_SIZE( eBits, mBits, size ) ) return false;
break;
}
default: return false;
}
Char c;
text.UpperCase();
ui8 mVal[ size ];
// Capture FPN ( Floating point number )
/*
i: Iteration only
e: Exponent
w: Whole Number part of it
f: Floating part of it
mVal: Mantissa Root Value
t: Text position
tCnt: Letter count
tVal: Temporary Value ( Mainly to move overflow to next byte position )
*/
bool isNaN = false, eNeg = false, mNeg = false;
int i = 0, e = 0, w = 0, f = 0;
int mSig = -1, mDot = -1; // mSig is for first significant number
int t, tCnt = text.length();
ui16 tVal = 0u;
ui32 eVal = 0u;
// Capture Mantissa Sign
c = text[ 0 ];
if ( c == cNeg )
{
mNeg = true;
text = text.Right( tCnt - 1 );
tCnt -= 1;
}
else if ( c == cPos )
{
text = text.Right( tCnt - 1 );
tCnt -= 1;
}
// Capture Mantissa FPN
if ( text == wxT( "INFINITY" ) ) b = -1;
else
{
for ( t = 0; t < tCnt; ++t )
{
c = text[ t ];
if ( c >= c0 && c <= c9 )
{
c -= c0;
if ( mSig < 0 && c > 0 ) mSig = i;
if ( mDot < 0 ) ++w;
else ++f;
tVal = mVal[ b ];
tVal *= 10;
tVal += c;
if ( tVal > 0xFF )
{
mVal[ b ] = ( tVal & 0xFF );
tVal >>= 2u;
--b;
if ( b < 0 ) break;
}
mVal[ b ] = tVal;
++i;
}
else if ( mDot < 0 && c == cDot ) mDot = i;
else if ( c == cE ) break;
else isNaN = true;
}
}
// Capture Exponent
if ( !isNaN )
{
if ( b < 0 ) {} // Cancel actions
else if ( t < tCnt )
{
++t;
eVal = 0u;
c = text[ t ];
if ( c == cNeg ) eNeg = true;
else if ( c != cPos ) isNaN = true;
else
{
for ( ++t; t < tCnt; ++t )
{
c = text[ t ];
if ( c >= c0 && c <= c9 )
{
c -= c0;
eVal *= 10u;
eVal += c;
}
else
{
isNaN = true;
break;
}
}
}
}
else if ( mSig > mDot )
{
eNeg = true;
eVal = f;
}
else eVal = w;
}
if ( isNaN )
{
switch ( mode )
{
case HEX_SF_IEEE754:
GetHexFromSF_IEEE754_ISNAN( buff, size );
break;
}
return false;
}
else if ( b < 1 )
{
switch ( mode )
{
case HEX_SF_IEEE754:
GetHexFromSF_IEEE754_INFINITY( buff, size, mNeg );
break;
}
}
else
{
switch ( mode )
{
case HEX_SF_IEEE754:
GetHexFromSF_IEEE754_BUFF( buff, size, mNeg, mBits, mVal, eNeg, eBits, eVal );
break;
}
}
return true;
}
bool GetHexFromSF_IEEE754_SIZE( int& eBits, int& mBits, &int size )
{
switch ( size )
{
// Half
case 2:
// 16bit
eBits = 6;
mBits = 10;
break;
// Float
case 4:
// 32bit
eBits = 8;
mBits = 24;
break;
case 5:
// 40bit
eBits = 8;
mBits = 32;
break;
case 6:
// 48bit
eBits = 8;
mBits = 40;
break;
// Double
case 8:
// 64bit
eBits = 12;
mBits = 52;
break;
case 10:
// 80bit
eBits = 12;
mBits = 68;
break;
// Quadruple
case 16:
// 128bit
eBits = 20;
mBits = 108;
break;
case 18:
// 144bit
eBits = 20;
mBits = 124;
break;
default: return false;
}
return true;
}
void GetHexFromSF_IEEE754_ISNAN( ui8* buff, int& size )
{
for ( int b = 0; b < size; ++b )
buff[ b ] = 0xFF;
}
void GetHexFromSF_IEEE754_INFINITY( ui8* buff, int& size, bool& mNeg )
{
buff[ 0 ] = mNeg ? 0xFF : 0x7F;
buff[ 1 ] = 0x80;
for ( int b = 2; b < size; ++b )
buff[ b ] = 0u;
}
void GetHexFromSF_IEEE754_BUFF( ui8* buff, int& size, bool& mNeg, int& mBits, ui8* mVal, bool& eNeg, u32& eVal )
{
int b, m, i, iEnd = size * 8;
bool bVal[ iEnd ];
bVal[ 0 ] = mNeg;
bVal[ 1 ] = eNeg;
ui16 v, t, num;
for ( i = eBits + 2; i > 1; --i )
{
bVal = ( eVal & 0x1 );
eVal >>= 1;
}
int bEnd = 0;
for ( b = size - 1; b > 0; --b )
{
if ( mVal[ b ] == 0u )
{
bEnd = b + 1;
break;
}
}
v = mVal[ bEnd ];
if ( v >= 0x10 )
{
v -= 0x10;
num = v & 0xF0;
}
else
{
v -= 1u;
num = v & 0xF;
}
for ( i = eBits + 3, m = 0; m < mBits; ++i, ++m )
{
// FPN -= 1.0
t = 0u;
// FPN *= 2
for ( b = size - 1; b >= bEnd; --b )
{
v = ( mVal[ b ] + t ) * 2u;
t = ( v & 0xFF00 );
v = ( v & 0xFF );
mVal[ b ] = v;
}
// If FPN >= 1.0
bVal = ( t > num );
}
}
|