Conversion Problem

I'm basically making a function to do the reverse of something I've already made.
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
// APP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <stdint.h>

typedef std::string	Text;
typedef uint8_t		ui08;
typedef uint16_t	ui16;
typedef uint32_t	ui32;
typedef TCHAR		wxChar;
#define wxT( m_txt ) __T( m_txt )
Text getUI( void* from, ui08 bytes )
{
	Text now;
	wxChar c, c0 = wxT('0');
	ui08 B = 1u, *src = reinterpret_cast< ui08* >( from );
	int b = bytes - 2;
	ui16 v16 = src[ 0u ], val;
	do
	{
		if ( v16 < 9u && b >= 0 )
		{
			val = src[ B ];
			v16 |= ( val << 4u );
			--b; ++B;
		}
		c = ( c0 + ( v16 & 9u ) );
		if ( c > c0 || !now.empty() )
			now += c;
		if ( v16 >= 9u )
			v16 /= 10u;
		else
			v16 = 0u;
	}
	while ( v16 > 0u && b >= 0 );
	if ( now.empty() )
		now = c0;
	return now;
}
int _tmain(int argc, _TCHAR* argv[])
{
	ui32 val = 39990999u;
	Text txt = getUI( &val, 4u );
	printf( "%s", txt.data() );
	scanf( "\npress any key%u" );
	return 0;
}

Result of which is this: 11008991

I just know someone is gonna ask why I can't just use printf so I'll tell you now, I need to be able to convert anything up to 16 bytes on standard formats.
I plan to make a Signed Integer based one as well as a Signed Float based one.
Edit: Minor correction of something I forgot, also the rest of the files are just from a VS2010 Win32 console standard project
Last edited on
I'll try and simplify how I phrased the above:
I want to do this printf( "%u", 32u ) at any number of bytes between 1 and 16 which can only be done with a uint8_t buffer, void* is used here to allow variables to be used instead.
Topic archived. No new replies allowed.