Converting text to hex

From my tests I found the problem to be somewhere in the Mantissa part that converts it to binary, I'm not sure what the issue there is though. I included the whole function so you can have a better idea of what is going on.
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
bool xsDLL GetHexFromSF_IEEE754( void* to, Text  from, ui08 tSize )
{
	from.UpperCase();
	int db = 0, dB = 0, dBEnd = tSize, dBLast = ( tSize - 1u ), dbEnd = dBEnd * 8;
	ui08 *data = reinterpret_cast< ui08* >( to );
	for ( ; dB >= 0; --dB )
		data[ dB ] = 0u;
	ui16 v16 = 0u;
	ui32 eVU = 0u, v32 = 0u;
	int eb = 0, eB = 0, eBEnd = 0, ebEnd = 0, eBLast = 0, eMax = 0, eMin = 0;
	switch ( tSize )
	{
		case 2u:
			eMax = 15;
			eBEnd = 1;
			ebEnd = 5;
			break;
		case 4u:
		case 6u:
			eMax = 128;
			eBEnd = 2;
			ebEnd = 8;
			break;
		case 8u:
			eMax = 1024;
			eBEnd = 3;
			ebEnd = 11;
		case 10u:
		case 16u:
			eMax = 16384;
			eBEnd = 3;
			ebEnd = 15;
			break;
		default:
			data[ 0 ] = 0x80;
			return false;
	}
	int mb = 0, mB = 0, mBEnd = ( tSize - eBEnd ), mbEnd = (( dbEnd - ebEnd ) - 1), mBLast = mBEnd - 1, mPos = 0;
	eMin = -( eMax - 1 );
	wxChar c = from[ 0 ];
	ui08 mVI[ mBEnd ], mVF[ mBEnd ];
	for ( ; mB < mBEnd; ++mB )
	{
		mVI[ mB ] = 0u;
		mVF[ mB ] = 0u;
	}
	int i = 0, iEnd = from.Length();
	bool r = true,  isNeg = ( c == cNeg ), isPos = ( c == cPos ), isDot = false, isExp = false, is0 = true;
	eBLast = eBEnd - 1;
	if ( !isNeg && !isPos )
		isPos = true;
	else
		++i;
	mB = mBLast;
	dB = 0;
	// Capture
	while ( db < dbEnd )
	{
		if ( i < iEnd )
		{
			c = from[ i ];
			++i;
			if ( c >= c0 && c <= c9 )
			{
				if ( db >= mbEnd && !isExp )
					break;
				if ( is0 )
					mB = mBLast;
				is0 = false;
				v16 *= 10u;
				v16 += ( c - c0 );
			}
			else if ( !isExp && !isDot && c == cDot )
			{
				isDot = true;
				v16 = 0u;
				mPos = mB;
			}
			else if ( !isExp && c == cE )
			{
				isExp = true;
				v16 = 0u;
				++i;
			}
			else
			{
				r = false;
				continue;
			}
		}
		else
			break;
		if ( isExp )
		{
			eVU = v16;
			if ( ( eVU * 10u ) >= v32 )
				break;
		}
		else
		{
			if ( c > c0 && c<= c9 )
				is0 = false;
			if ( isDot )
				mVF[ mB ] = ( v16 & 0xFF );
			else
				mVI[ mB ] = ( v16 & 0xFF );
			if ( v16 > 0xFFF )
			{
				--mB; db += 8u;
				if ( db >= mbEnd )
					v16 = 0u;
				else
					v16 >>= 8u;
			}
		}
	}
	ui08 dbv[ dbEnd ];
	dbv[ 0 ] = isNeg ? 1u : 0u;
	for ( db = 1; db < dbEnd; ++db )
		dbv[ db ] = false;
	// Mantissa
	// FIXME(awsdert) IEEE754 Mantissa needs fixing
	mb = 0;
	mB = mPos;
	while ( ( mVI[ mB ] & 0x80 ) == 0u )
	{
		mVI[ mB ] <<= 1u;
		++mb;
		if ( mb == 8 )
			break;
	}
	v32 = 0u;
	for ( i = 0, mb = 0, db = ebEnd; mb < mbEnd; ++i, ++mb, ++db, ++v32 )
	{
		dbv[ db ] = (( mVI[ mB ] & 0x80 ) >> 7u );
		mVI[ mB ] <<= 1u;
		if ( i == 7 )
		{
			++mB; i = 0;
		}
	}
	dB = dBLast; dBLast -= 8;
	for ( i = 0, mb = 0; mb < mbEnd; ++mb, ++db )
	{
		eB = mPos;
		v16 = mVF[ eB ];
		for ( --eB; eB >= 0; --eB )
		{
			v16 *= 2u;
			mVF[ eB + 1 ] = ( v16 & 0xFF );
			v16 >>= 8u;
			mVF[ eB ] = v16;
		}
		is0 = ( mVF[ mPos ] == 0u );
		if ( isNeg )
			dbv[ db ] = is0 ? 1uLL : 0uLL;
		else
			dbv[ db ] = is0 ? 0uLL : 1uLL;
		mVF[ mPos ] = 0u;
	}
	// Exponent
	if ( !isExp )
		eVU = v32;
	// - Check Range
	v32 = eMax;
	while ( eVU > v32 )
		eVU /= 10u;
	v32 = -eMin;
	if ( isNeg && eVU > v32 )
		eVU -= 1u;
	// - Push bits forward
	// NOTE(awsdert) IEEE754 Exponent might not be recorded correctly
	db = ebEnd - 8;
	while ( db > 0 )
		db -= 8;
	for ( ; db < 0; ++db )
		eVU <<= 1u;
	// - Place bits in main variable
	if ( isNeg )
		eVU += eMax;
	//*/
	for ( db = 1, eb = 0; eb < ebEnd; ++eb, ++db )
	{
		dbv[ db ] = (( eVU & 0x80000000 ) >> 31u );
		eVU <<= 1u;
	}
	// Put everything into pointed variable
	for ( db = 0, dB = dBEnd - 1, i = 7; db < dbEnd; ++db, --i )
	{
		data[ dB ] |= ( dbv[ db ] << i );
		if ( i == 0 )
		{
			--dB; i = 7;
		}
	}
	return r;
}

Edit: Adjustments made, still having problems though.
Edit: After finding a more helpful resource( http://en.wikipedia.org/wiki/Single-precision_floating-point_format ) I got the function looking more like it should but am still having problems
Last edited on
I saw that my thread had dropped to the next page, using this reply to put it back on first page for now
Last edited on
Topic archived. No new replies allowed.