Pages: [1]   Go Down
Author Topic: Int to char*...  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having some problems at conversing formats.

I have a function(getFecha()) (non created by me but I have to use it) which returns timestamp in char*. This is the format:

year, month, date, day, hour, minute, second

09:10:20:03:17:35:00


And I want to upload seconds.

I tried something like this:

Code:
USB.print("Time: ");
  USB.println(RTC.getFecha());
  char* fecha = RTC.getFecha();
  fecha[19] = '9'; //Seconds (units)
  USB.println(fecha);
  delay(1000);

This works fine. But I want to operate with seconds, in order to upload exactly what I need. If I try: fecha[19] = 9; It fails and fecha[19] = (char) 9; also fails.

Is there anything more before admit that number? If I print fecha (complete sentence) after update number, is fails. It shows this:
09:10:20:03:17:35:0.
So writes a "." I understand...

Any idea?
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, try:
Code:
fecha[19] = 9 + '0';
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works!! Why this happends? Do you know?

Anyway, thank you so much.
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You want to store an ASCII character, the character '0' = 48 in the ASCII table (and the character '9' = 57), so to make the digit 9 into the character '9', you simply add '0' (or 48) to it smiley. Of course, it's exactly the same for other digits, just add '0' to them.

http://www.asciitable.com
« Last Edit: November 28, 2012, 04:38:56 am by guix » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  char* fecha = RTC.getFecha();
Does getFecha() actually dynamically allocate memory so that it can properly return a pointer? If so, you should be freeing it when you are done.

If not, the function is WRONG! No matter what it is doing.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, try:

What's the difference between this:
Code:
fecha[19] = 9 + '0';
... and this?
Code:
fecha[19] = '9';
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 33
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The former allows you to use a variable instead of the 9, the latter won't.

i.e., this won't work:

int this_is_a_variable = 9;
fecha[19] = 'this_is_a_variable';
Logged

Pages: [1]   Go Up
Jump to: