teile von char array als c strings behandeln
-
Hallo an alle.
Folgendes sieht in meinen augen einfach scheiße aus.
Hat jemand vielleicht 'ne idee, wie mans schöner machen könnte?long encode_time_stamp(unsigned char* tmstmp, int tmpsmp_size) { printf("time: %s\n", tmstmp); char cyear[3]; char cmon[3]; char cday[3]; char chour[3]; char cmin[3]; char csec[3]; cyear[0] = tmstmp[0]; cyear[1] = tmstmp[1]; cyear[2] = '\0'; int year = atoi(cyear); cmon[0] = tmstmp[2]; cmon[1] = tmstmp[3]; cmon[2] = '\0'; int mon = atoi(cmon); cday[0] = tmstmp[4]; cday[1] = tmstmp[5]; cday[2] = '\0'; int day = atoi(cday); chour[0] = tmstmp[6]; chour[1] = tmstmp[7]; chour[2] = '\0'; int hour = atoi(chour); cmin[0] = tmstmp[8]; cmin[1] = tmstmp[9]; cmin[2] = '\0'; int min = atoi(cmin); csec[0] = tmstmp[10]; csec[1] = tmstmp[11]; csec[2] = '\0'; int sec = atoi(csec); struct tm time; time.tm_year = year + 100; time.tm_mon = mon - 1; time.tm_mday = day; time.tm_hour = hour; time.tm_min = min; time.tm_sec = sec; return mktime(&time); }
Grüße
Dimitirj
-
-
Hi
Leider sehe ich bei scanf niergendwo die möglichkeit anzugeben, welche chars aus dem array genommen werden sollen (von welcher bis zu welcher position)
-
probier mal %02d als format fuer eine komponente
-
Wie waere es mit strncpy? Z.B.
strncpy(cday. tmstmp+4, 2);
fuer cday.
Allerdings muesstest Du dann Deine Variablen auf 0 vorinitialisieren, aber das sollte ja mittelschar cday[3] = {0};
gehen.
-
tim_g Danke. War genau das, was ich gesucht habe