Thread: stupid question???

  1. #1
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    Unhappy stupid question???

    how would i convert an int to a char ??

    say weve got year, month, day as integers 2 digits long
    ie 011114, held as a long int, or 3 separate ints

    i want to save the date as a character string

    this ought to be easy, but i'm struggling
    am i as thick as a elephant sandwich ????
    Steve

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In the case of 3 separate ints.
    One way is to use sprintf() in <stdio.h>.
    For example:
    Code:
       int mon = 11;
       int day = 14;
       int year = 2001;
       char date[11];
    
       sprintf(date,"%d/%d/%d",year,mon,day); //no leading zeros
       printf("%s\n",date);
       sprintf(date,"%#02d/%#02d/%#02d",year,mon,day); //leading zeros
       printf("%s\n",date);
    >am i as thick as a elephant sandwich ????
    hehe
    Last edited by swoopy; 11-14-2001 at 05:24 PM.

  3. #3
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    oh yeah !!!! thanx

    thanx
    thats a good way to do it !!!
    Steve

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use long's

    ie lDate=15112001;

    I have macros
    Code:
    #define		GETDAY(lDate)		(lDate / 1000000)
    #define		GETMONTH(lDate)		((lDate / 10000) % 100)
    #define		GETYEAR(lDate)		(lDate % 10000)
    Combine this and Swoopies use of sprintf and you may have something.

    (sprintf(sDate,"%d/%d/%d",GETDAY(lDate), ect)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    thats a neat solution, i like that thanx !!!
    Steve

  6. #6
    Sayeh
    Guest
    Actually, you cannot convert an int to a char. Even using itoa() or another function doesn't do this.

    Please allow me to explain.

    A char has 8-bits. It may contain an absolute value of 0-255.
    An int (usually a short int) has 16-bits. It may contain an absolute value of 0-65535.

    When you "convert" a 16-bit int to an 8-bit char, by default you are saying that your int will not be used for any number larger than 0-255 (or -127 to +127). If you try to use a number that is larger than these, when you do the conversion, the top 8-bits are stripped off.

    In c, this is represented this way, and may converted to a macro as an exercise to the reader--

    int a;
    char b;

    a = 5;
    b = 0;

    b = (char)(0x00FF & a);

    ------
    where:

    (char) coerces the resultant math to a form compatible with 'b'.
    0x00FF is a mask to strip off the top 8-bits.

    b == 5

    -------

    Let's say you have made 'a' larger than 255. What happens? Watch...

    a = 27084 ; /* in hex that is: 0x680A */
    b = 0;

    b = (char)(0x00FF & a);

    The stripping:

    0x00FF &
    0x680A
    -------------
    0x000A

    b == 10 /* or 10 = 0x0A */

    -----------------------------------------
    enjoy.

  7. #7
    Sayeh
    Guest

    Correction

    Typo (for crying out loud I fat fingered the above)--

    27084 = 0110 1001 1100 1100 in binary.
    hex = 0x69CC

    So the logic is:

    0x00FF &
    0x69CC
    ----------
    0x00CC

    b = 0xCC.
    b = 204 decimal.

    ---

    Sorry about that, I try to be accurate.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Theoretically, C does not recognize characters. Only ints that can be displayed as chars. For instance (don't quote me on the number -- I'm not sure if '40' is 'a'):
    Code:
    int letter = 40;   /* don't quote me on the char 'a' */
    
    printf("The letter is %c\n", letter);
    If 40 is == to 'a', then it will print out 'a', not an integer. To do a string, you can just loop through the whole string using this format.

    --Garfield
    1978 Silver Anniversary Corvette

  9. #9
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    Thumbs up

    right, thanx guys, i'm much the wiser now !!!
    learning all the time...............
    Steve

  10. #10
    Unregistered
    Guest

    to conver #1 to ASCII 1

    /*this function works for #0 - 9 it will take a one digit # and convert it to ascii value*/
    char int_to_ascii(int x)
    {
    char char_val ;
    char_val = x | 0x0048 ; /* 48 is the ascii value of zero */
    return char_val;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM