changing hex to decimal
2009-08-04 by aligole_2005
Yahoo Groups archive
Index last updated: 2026-04-28 22:41 UTC
Thread
2009-08-04 by aligole_2005
Hi friends I want to make a function in Code vision to change a hexadecimal byte to decimal or int, Can anybody help me? Maybe it 'll be good that you know the hex data is stored in a string with the length of 5 byte. in fact I want to have the c code of the function that gives a char or string hex data and give me the decimal data.
2009-08-04 by iijfet
--- In AVR-Chat@yahoogroups.com, "aligole_2005" <aligole_2005@...> wrote:
>
> Hi friends
> I want to make a function in Code vision to change a hexadecimal byte to decimal or int, Can anybody help me?
> Maybe it 'll be good that you know the hex data is stored in a string with the length of 5 byte.
> in fact I want to have the c code of the function that gives a char or string hex data and give me the decimal data.
>
this is:
#include <stdio.h>
int main()
{
int hexa;
scanf("%x", &hexa);
printf("%d", hexa);
return 0;
}2009-08-04 by Dave Hylands
Perhaps strol or strtoul is the function that you're looking for? On Mon, Aug 3, 2009 at 11:59 PM, aligole_2005<aligole_2005@yahoo.com> wrote: > Hi friends > I want to make a function in Code vision to change a hexadecimal byte to decimal or int, Can anybody help me? > Maybe it 'll be good that you know the hex data is stored in a string with the length of 5 byte. > in fact I want to have the c code of the function that gives a char or string hex data and give me the decimal data. -- Dave Hylands Shuswap, BC, Canada http://www.DaveHylands.com/
2009-08-04 by Don Kinzer
--- In AVR-Chat@yahoogroups.com, "aligole_2005" <aligole_2005@...> wrote: > in fact I want to have the c code of the function that gives > a char or string hex data and give me the decimal data. The suggestion to use strtoul() may work but you should note that it requires that the string have a 0x prefix if it is hexadecimal. From your description, I take it that the prefix does not exist and would have to be added. The scanf() suggestion is also useful but you may find that using it add a *lot* of code to your application due to its generality. The algorithm for converting from hexadecimal characters to the corresponding value is very simple. 1) Initialize the value accumulator to zero 2) For each character of the sequence: if the character is '0' (0x30) through '9' (0x39), subtract 0x30 from the character and add the result to the accumulator after having multiplied the accumulator by 16; if the character is 'A' (0x41) through 'F' (0x46), subtract (0x41 - 10) from the character and add the result to the accumulator after having multiplied the accumulator by 16. 3) The numeric value of the hexadecimal sequence is in the accumulator. If you need to also accept hexadecimal characters 'a' through 'f' it is simple to extend step 2 for those characters. Also, you need to decide what to do when you encounter a character that is not a valid hexadecimal character or if too many hexadecimal characters are present given the size of the accumulator. Don Kinzer ZBasic Microcontrollers http://www.zbasic.net
2009-08-04 by David Kelly
On Tue, Aug 04, 2009 at 08:36:59AM -0000, iijfet wrote:
> --- In AVR-Chat@yahoogroups.com, "aligole_2005" <aligole_2005@...> wrote:
> >
> > Hi friends
> > I want to make a function in Code vision to change a hexadecimal
> > byte to decimal or int, Can anybody help me? Maybe it 'll be good
> > that you know the hex data is stored in a string with the length of
> > 5 byte.
> > in fact I want to have the c code of the function that gives a char
> > or string hex data and give me the decimal data.
> >
>
> this is:
> #include <stdio.h>
> int main()
>
> {
> int hexa;
>
> scanf("%x", &hexa);
> printf("%d", hexa);
>
> return 0;
>
> }
scanf() and printf() are very expensive resource hogs.
--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.2009-08-04 by David Kelly
On Tue, Aug 04, 2009 at 06:59:57AM -0000, aligole_2005 wrote: > Hi friends > I want to make a function in Code vision to change a hexadecimal byte > to decimal or int, Can anybody help me? Maybe it 'll be good that you > know the hex data is stored in a string with the length of 5 byte. You want to make a function rather than used an existing function? So what is your problem? This is a trivial exercise. Sounds like a homework assignment for the first week of a class. -- David Kelly N4HHE, dkelly@HiWAAY.net ======================================================================== Whom computers would destroy, they must first drive mad.