Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. time in ascii

time in ascii

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    styve
    wrote on last edited by
    #1

    I'm using time.h, to get time of the day. In my code it is converted into number so it is possible to read the time. But I will have it in ascii format. I dont know how to do to get hour, min and sec into ascii. I will not have them converted into numbers. My code: time_t tid; tm * ptm; time(&tid); ptm = gmtime(&tid); struct timeval *Tps; struct timezone *Tzp; Tps = (struct timeval* ) malloc(sizeof(struct timeval)); Tzp = 0; gettimeofday(Tps, Tzp); hour = ptm->tm_hour; //become integers, will have ascii min = ptm->tm_min; sec = ptm ->tm_sec; //Stefan

    M 1 Reply Last reply
    0
    • S styve

      I'm using time.h, to get time of the day. In my code it is converted into number so it is possible to read the time. But I will have it in ascii format. I dont know how to do to get hour, min and sec into ascii. I will not have them converted into numbers. My code: time_t tid; tm * ptm; time(&tid); ptm = gmtime(&tid); struct timeval *Tps; struct timezone *Tzp; Tps = (struct timeval* ) malloc(sizeof(struct timeval)); Tzp = 0; gettimeofday(Tps, Tzp); hour = ptm->tm_hour; //become integers, will have ascii min = ptm->tm_min; sec = ptm ->tm_sec; //Stefan

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      Try sprintf(buffer, "%02d:%02d:%02d", hour, min, sec); Maxwell Chen

      S 1 Reply Last reply
      0
      • M Maxwell Chen

        Try sprintf(buffer, "%02d:%02d:%02d", hour, min, sec); Maxwell Chen

        S Offline
        S Offline
        styve
        wrote on last edited by
        #3

        yes, I can use it, but then there will be digits. I will have it in ascii format, because it take less space in a file.

        M P 2 Replies Last reply
        0
        • S styve

          yes, I can use it, but then there will be digits. I will have it in ascii format, because it take less space in a file.

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          For saving space issue, I would use this way... Seven bytes for the whole date and time. unsigned char sDateTime[7] = { 20, 03, 2, 19, 16, 17, 55 }; int iYr, iMon, iDay, iHr, iMin, iSec; // To extract from buffer... iYr = sDateTime[0] * 100 + sDateTime[1]; iMon = sDateTime[2]; iDay = sDateTime[3]; iHr = sDateTime[4]; iMin = sDateTime[5]; iSec = sDateTime[6]; Maxwell Chen

          S 1 Reply Last reply
          0
          • S styve

            yes, I can use it, but then there will be digits. I will have it in ascii format, because it take less space in a file.

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #5

            styve wrote: I will have it in ascii format, because it take less space in a file :confused::confused: Any ways if you want to store 'A' in the file what do you want to store 'A' or '65' (65 is ascii for 'A') which one takes more space ???


            "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

            S 1 Reply Last reply
            0
            • M Maxwell Chen

              For saving space issue, I would use this way... Seven bytes for the whole date and time. unsigned char sDateTime[7] = { 20, 03, 2, 19, 16, 17, 55 }; int iYr, iMon, iDay, iHr, iMin, iSec; // To extract from buffer... iYr = sDateTime[0] * 100 + sDateTime[1]; iMon = sDateTime[2]; iDay = sDateTime[3]; iHr = sDateTime[4]; iMin = sDateTime[5]; iSec = sDateTime[6]; Maxwell Chen

              S Offline
              S Offline
              styve
              wrote on last edited by
              #6

              Hi! Your solution were good. It made me think in another way. But there is one problem. How will I solve microseconds (6 digits)?? Two digits is one byte, so six digits willbe 3 byte??

              1 Reply Last reply
              0
              • P Prakash Nadar

                styve wrote: I will have it in ascii format, because it take less space in a file :confused::confused: Any ways if you want to store 'A' in the file what do you want to store 'A' or '65' (65 is ascii for 'A') which one takes more space ???


                "When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)

                S Offline
                S Offline
                styve
                wrote on last edited by
                #7

                One letter or one digit that is stored to a file is one byte. If I only store A, the file is one byte. If I store 65, the file is two byte. It was that I meant

                D 1 Reply Last reply
                0
                • S styve

                  One letter or one digit that is stored to a file is one byte. If I only store A, the file is one byte. If I store 65, the file is two byte. It was that I meant

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  styve wrote: If I store 65, the file is two byte. Not necessarily. Consider this:

                  FILE *f;
                  char letr = 'A';
                  fwrite(&letr, sizeof(char), 1, f);

                  letr = 65;
                  fwrite(&letr, sizeof(char), 1, f);

                  The size of the file in both cases will be one byte.

                  char *ltrs = "65";
                  fwrite(ltrs, sizeof(char), 2, f);

                  Now the size of the file will be two bytes.

                  int num = 65;
                  fwrite(&num, sizeof(int), 1, f);

                  Now the size of the file will be four bytes.


                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups