reading decimal data....

Y

Yodai

Hi all..

I'm trying to program an application for an emmbedded sistem where an
external processor introduces data into my RAM. The thing is some if the
data I have to pick up is written in hexadecimal format, but some other is
written on the memory positions in decimal format. How can I retrieve this
information reading it as decimal?

So far I use this:


#define MYHEXAVAR (uint_16t*(0x200))


//Which would allow me to read data from MYHEXAVAR just declaring it in my
function. Let's say the position 0x200 of my memory contains 0x0012 as a
value


void myfunc ()

{

sprintf(NewKey, "%3u", *MYHEXAVAR);
memcpy(Key, NewKey, 4);

}





or something similar, I've just outline the function because the real one is
much more complex and long and it wouldn't be good to make my point.
so this func would read memory position 0x200 and write the decimal
representation of 0x0012 which is "18". Now my problem is that I need it to
write "12"
Any of you guys have an idea to how to solve this problem.? It should be
fairly easy, but I don't know whats the easiest. I was thinking about doing
the whole conversion manually reading bit to bit, but I am sure there's some
other easier way....


Thanks in advance!

Marc
 
J

Joona I Palaste

Yodai said:
I'm trying to program an application for an emmbedded sistem where an
external processor introduces data into my RAM. The thing is some if the
data I have to pick up is written in hexadecimal format, but some other is
written on the memory positions in decimal format. How can I retrieve this
information reading it as decimal?
So far I use this:
#define MYHEXAVAR (uint_16t*(0x200))
//Which would allow me to read data from MYHEXAVAR just declaring it in my
function. Let's say the position 0x200 of my memory contains 0x0012 as a
value
void myfunc ()
{
sprintf(NewKey, "%3u", *MYHEXAVAR);
memcpy(Key, NewKey, 4);
}
or something similar, I've just outline the function because the real one is
much more complex and long and it wouldn't be good to make my point.
so this func would read memory position 0x200 and write the decimal
representation of 0x0012 which is "18". Now my problem is that I need it to
write "12"
Any of you guys have an idea to how to solve this problem.? It should be
fairly easy, but I don't know whats the easiest. I was thinking about doing
the whole conversion manually reading bit to bit, but I am sure there's some
other easier way....

Do you want the sprintf() call to write "12" (eighteen in
hexadecimal) into NewKey rather than "18" (eighteen in decimal)? If so,
then change "%3u" to "%2x" and you should be settled.

If this is not the real problem, then please explain in detail what is.
Note that embedded systems, external processors, and even physical
memory locations are a feature of the computer system itself, not of C.
 
N

Nick Landsberg

Yodai said:
Hi all..

I'm trying to program an application for an emmbedded sistem where an
external processor introduces data into my RAM. The thing is some if the
data I have to pick up is written in hexadecimal format, but some other is
written on the memory positions in decimal format. How can I retrieve this
information reading it as decimal?

So far I use this:


#define MYHEXAVAR (uint_16t*(0x200))


//Which would allow me to read data from MYHEXAVAR just declaring it in my
function. Let's say the position 0x200 of my memory contains 0x0012 as a
value


void myfunc ()

{

sprintf(NewKey, "%3u", *MYHEXAVAR);
memcpy(Key, NewKey, 4);

}





or something similar, I've just outline the function because the real one is
much more complex and long and it wouldn't be good to make my point.
so this func would read memory position 0x200 and write the decimal
representation of 0x0012 which is "18". Now my problem is that I need it to
write "12"
Any of you guys have an idea to how to solve this problem.? It should be
fairly easy, but I don't know whats the easiest. I was thinking about doing
the whole conversion manually reading bit to bit, but I am sure there's some
other easier way....


Thanks in advance!

Marc

comp.algorithms question, I believe, but, nevertheless ---

char hextable[] = { '0', '1', '2',
/* you fill in the rest */ , 'D', 'E', 'F'};

step through the bytes(octets?) and output hextable[byte_value];

Worry about endian-ess.
 
Y

Yodai

problem solved! you were right...... I knew it had to be something I
overlooked, as it is clearly specified in the sprinf conversion charts...

Cheers....

Yodai
 
Y

Yodai

problem solved! you were right...... I knew it had to be something I
overlooked, as it is clearly specified in the sprinf conversion charts...

Cheers....

Yodai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top