Go Down

Topic: Int to char*... (Read 640 times) previous topic - next topic

Suriken

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: [Select]
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?

guix

Hello, try:
Code: [Select]

fecha[19] = 9 + '0';

Suriken

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

Anyway, thank you so much.

guix

#3
Nov 28, 2012, 10:37 am Last Edit: Nov 28, 2012, 10:38 am by guix Reason: 1
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 :). Of course, it's exactly the same for other digits, just add '0' to them.

http://www.asciitable.com

PaulS

Code: [Select]
  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.

PeterH


Hello, try:


What's the difference between this:
Code: [Select]

fecha[19] = 9 + '0';

... and this?
Code: [Select]

fecha[19] = '9';

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

KeithRB

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';

Go Up