strtol

23 Feb 2010

Hi!

Has someone tried to implement the functions from stdlib.h of c++ in the mbed? I've tried to put in my program the strtol but it doesn't work. I've also tried to import the stdlib.h but the compiler gives some errors.

What can I do?

Thanks

23 Feb 2010

Show your code or tell the exact error text. I just tried the sample from here, compiled fine. Didn't even have to add any includes.

23 Feb 2010

Hi Igor,

You are so fast!

Here there is part of my code:

token = nmeatoken(nmea_buffer, &index);

hores = strtol(token, NULL, 10);

If I print token it shows me the correct thing but when I try to separate some parts from token (hores) and I print hores, theres nothing in hores.

Do I have to put another library like stdio.h or stdlib.h?

 

Thanks!

23 Feb 2010

What is "the correct thing", what did you expect to be in hores and what you got instead? Also please show how you're printing it.

23 Feb 2010

Hi

I have the token variable which is for example:  123519  with my function:

token = nmeatoken(nmea_buffer, &index); //Hora UTC

hores = strtol(token, NULL, 10);

I wanted to print hores which it should print 123519 because my function in theory is what it does.

What I did, was: pc.putc (hores); and I haven't got anything.

I found that I should put:  pc.printf (%ld", hores); but I really don't know why.

Now this problem is solved, but I have another problem. How can I print an int?

Because I've passed my variable hores (which is an unsigned long) to an int:.

hora=hores;

where hora is an int and hores is an unsigned long and I want to know if this conversion is ok or not. How can I do?

23 Feb 2010

I think you should read up on differences between putc and printf...

As for conversion, on ARM int is the same size as long, so it should be fine. You can also use atoi() which returns an int.

23 Feb 2010 . Edited: 23 Feb 2010

^ I was intrigued by your statement above, so did some googling and found the following data type size information (ARM).

TypeSize in bitsNatural alignment in bytes
char 8 1 (byte-aligned)
short 16 2 (halfword-aligned)
int 32 4 (word-aligned)
long 32 4 (word-aligned)
long long 64 4 (word-aligned)
float 32 4 (word-aligned)
double 64 4 (word-aligned)
long double 64 4 (word-aligned)
All pointers 32 4 (word-aligned)
bool (C++ only) 32 4 (word-aligned)

 

Why would a bool need 32 bits?

24 Feb 2010

Why would a bool need 32 bits?

It doesn't - that table is somewhat incorrect. See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348b/Babfcgfc.html