Thread: chars to int

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    chars to int

    Why does print() put the string in reverse order ??

    Code:
    void print (unsigned int num)
    {
     int i;
    
     typedef union
     {
      unsigned int number;
      char string[4];
     } DATA;
    
     DATA data;
    
     data.number = num;
    
     for (i = 0; i < 4; i++)
      printf ("%c", *(data.string + i));
     
     putc ('\n', stdout);
     
     return;
    }
    Last edited by PutoAmo; 04-16-2002 at 05:49 AM.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    This is because the low byte of the integer is stored before the high byte of the integer.

    Example: 0x31323334 (1234) is stored in memory as: 0x34 0x33 0x32 0x31.

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Oh, the Little Endian thing ! I should have thought about it

    Thank you, Monster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM