Go Down

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


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


And I want to upload seconds.

I tried something like this:

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

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:
So writes a "." I understand...

Any idea?


Hello, try:
Code: [Select]

fecha[19] = 9 + '0';


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

Anyway, thank you so much.


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.



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.


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.


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