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
Dec to Hex
- 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
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.
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;
}Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Dec to Hex
Hello Ben,
Thankyou very much, works a treat
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?
Thankyou very much, works a treat
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?
- 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
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
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel