Int to char

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok ive been at it for a day & im stuck again.
Im trying to take 3 int values convert them to char & then combine the 3 chars into one value.

I would have used string but i don't think you can use it in AVR chips.

int Hours = 1;
int Minutes = 2;
int Seconds = 3;

//now convert 3 int to char
//now store in one variable
char TotalTime //Stored as 1:2:3

Can anyone supply the code to this?

Thanks
Warren

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int Hours = 1; 
int Minutes = 2; 
int Seconds = 3; 

char TotalTime[9];

sprintf(TotalTime, "%d:%d:%d", Hours, Minutes, Seconds);

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might want to get yourself a good book on C to acquaint yourself with the standard C library functions. Saves re- inventing the wheel.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

You might want to get yourself a good book on C to acquaint yourself with the standard C library functions. Saves re- inventing the wheel.

haha Yeah i probably should, how much do they help?

int Hours = 1;
int Minutes = 2;
int Seconds = 3;

char TotalTime[9];

sprintf(TotalTime, (browser had a fit), Hours, Minutes, Seconds);

That looks really good but it doesn't save it to a variable for use later.

I have found something that works ok see below


char* TempTime;

void StoreTime (void)
{
	//This section gets the current time & converts the int time to string like Hours:Minutes:Seconds
	char cHours[3];
	itoa(Hours,cHours,10);
	char cMinutes[3];
	itoa(Minutes,cMinutes,10);
	char cSeconds[3];
	itoa(Seconds,cSeconds,10);

	int i = 0;
	int x = 0;
	int s = 0;

	char Time[sizeof(cHours) + sizeof(cMinutes) + sizeof(cSeconds)];

	while(cHours[i])
	{
		Time[i] = cHours[i];
		++i;
	}

	Time[i] = ':';        //Puts the : time seprator in.
	++i;

	while(cMinutes[x])
	{
		Time[i] = cMinutes[x];
		++i;
		++x;
	}

	Time[i] = ':';
	++i;

	while(cSeconds[s])
	{
		Time[i] = cSeconds[s];
		++i;
		++s;
	}

	Time[i] = '\0'; // don't forget the null
	TempTime = Time;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

warren1 wrote:
Quote:

You might want to get yourself a good book on C to acquaint yourself with the standard C library functions. Saves re- inventing the wheel.

haha Yeah i probably should, how much do they help?

They really do help a lot. At least they help to avoid asking such dumb questions like yours.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The sprintf code stores the result into TotalTime. Your code works but for the grace of God as it returns a pointer to a temp variable that no longer exists when the function exits. Would a good C book help? How do you think most of us learned?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

They really do help a lot. At least they help to avoid asking such dumb questions like yours.

lol fantastic.

I never came up with the code i found it on the net because i was beat in trying to work it out.

Ok so whats a friggen good one?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

int Hours = 1;
int Minutes = 2;
int Seconds = 3;

char TotalTime[9];

sprintf(TotalTime, (browser had a fit), Hours, Minutes, Seconds);

That looks really good but it doesn't save it to a variable for use later.

Rubbish. Of-course it does.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read the "c in 21 pages" in the imagecraft help file (comes with the eval compiler)

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char Time[sizeof(cHours) + sizeof(cMinutes) + sizeof(cSeconds)]; 

This gives Time the room for the numbers and. the three ':'s that you are also putting into it, but not for the terminating '\0'.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I love you Mbedder

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:

char Time[sizeof(cHours) + sizeof(cMinutes) + sizeof(cSeconds)]; 

This gives Time the room for the numbers and. the three ':'s that you are also putting into it, but not for the terminating '\0'.


He's adding two colons, not three.

Sid

Life... is a state of mind