Dec to Hex

For E-blocks user to discuss using E-blocks and programming for them.

Moderators: Benj, Mods

Post Reply
Pete
Posts: 4
Joined: Fri Feb 01, 2008 9:40 pm
Location: Hull, UK

Dec to Hex

Post by Pete »

Hi, I have just decided to start programming in C (2 weeks ago) I normally program in PIC ASM. I decided to expand on the processors I use, so bought the E-Blocks ARM Board.

I am wanting to send a 32bit int to a function and return it as an hexadecimal string.

Can someone tell me how I go about doing this, or if you have a code example?.

Thanks

Pete

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Dec to Hex

Post by Benj »

Hello Pete,

The following code should help. It converts a 16bit number into a 4 digit hex string and stores the result into a globally defined array.

Code: Select all

void ToHexString(unsigned short Number)
{
	unsigned short tmp_int;
	char tmp_byte;

	Num_Str[0] = ' ';
	Num_Str[1] = ' ';
	Num_Str[2] = ' ';
	Num_Str[3] = ' ';

	tmp_int = Number;
	if (Number >= 4096)
	{
		tmp_byte = tmp_int / 4096;

		if (tmp_byte > 9)
		{
			Num_Str[0] = 'A' + (tmp_byte - 10);
		}
		else
			Num_Str[0] = '0' + tmp_byte;

		while (tmp_byte > 0)
		{
			tmp_int = tmp_int - 4096;
			tmp_byte--;
		}
	}
	if (Number >= 256)
	{
		tmp_byte = tmp_int / 256;

		if (tmp_byte > 9)
		{
			Num_Str[1] = 'A' + (tmp_byte - 10);
		}
		else
			Num_Str[1] = '0' + tmp_byte;

		while (tmp_byte > 0)
		{
			tmp_int = tmp_int - 256;
			tmp_byte--;
		}
	}
	if (Number >= 16)
	{
		tmp_byte = tmp_int / 16;

		if (tmp_byte > 9)
		{
			Num_Str[2] = 'A' + (tmp_byte - 10);
		}
		else
			Num_Str[2] = '0' + tmp_byte;

		while (tmp_byte > 0)
		{
			tmp_int = tmp_int - 16;
			tmp_byte--;
		}
	}

	if (tmp_int > 9)
	{
		Num_Str[3] = 'A' + (tmp_int - 10);
	}
	else
		Num_Str[3] = '0' + tmp_int;
}

Pete
Posts: 4
Joined: Fri Feb 01, 2008 9:40 pm
Location: Hull, UK

Re: Dec to Hex

Post by Pete »

Hello Ben,
Thankyou very much, works a treat 8) :D

Can you suggest any decent Embedded C books or websites to learn from? I am using 'Teach your self C++ in 21 Days' and 'C++ Builder' from Sams Publishing. I have learnt some basic principles of C from these books, but seems I have to make a function for everything I want to do when it comes to embedded, which I don't mind, it's just knowing how to do it.. which these books dont seem to cover. Are there any websites that have code snippets for the AT91SAM7S128?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Dec to Hex

Post by Benj »

C++ books are generally aimed at computers, the meaning of the ++ is that it has an increased functionality to control IBM based processors.

What you want is either a book on Embedded or a book on C aimed for a certain microcontroller Eg PIC or AVR. There are pleanty of these available on Amazon.

Also a lot of C compilers come free with routines to control peripherals like SPI, ADC, I2C etc. You normally just have to find the correct header file in the inc directory to see the structure of the functions..

But for a really good learning curve for C I would recommend Flowcode. You can use our hardware macros to do the programming and then look at the commented C code to understand the background code. We have a course for Flowcode available here if you are interested.

http://212.241.213.173/software/Flowcou ... censed.zip

Post Reply