Thread: Convert int to two char

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    Convert int to two char

    hi,
    how can I covert a int to two char...
    I have some idea using shift operators..
    thanks...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your idea is a good one - see what you can come up with
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    my idea:
    using big-endian address..
    Code:
    char is 1 byte, int is 2 byte
    char a, b;
    
    int someValue, result;
    a = someValue;
    b = someValue << 8;
    
    result = getValue(a,b)
    
    int getValue(char a, char b)
    {
        int value1, value2, total ;
        value2 = value2 >> 8;
        total = value2 + value1;
        return total;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, that's the basic approach.
    Now just put it all into a working program and see how you get on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Quote Originally Posted by redsar
    my idea:
    using big-endian address..
    Endianness has nothing to do with addressing. It's about internal representation.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have some idea using a union. That's the incredibly lazy way to do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Emmanuel Delaha
    Endianness has nothing to do with addressing. It's about internal representation.
    Endianness has everything to do with how an int is stored in memory, and thus how you would cast a char to it.

    Code:
    int someint = 0x01234567;
    char somechar = (char)someint;
    On a little endian machine, somechar would equal 0x67. On a big endian machine, somechar would equal 0x01.

  8. #8
    Quote Originally Posted by bithub
    Code:
    int someint = 0x01234567;
    char somechar = (char)someint;
    On a little endian machine, somechar would equal 0x67. On a big endian machine, somechar would equal 0x01.
    Of course, you meant

    char somechar = (char)(someint & 0xFF);

    because a char can have more than 8 bits. The actual value is defined in CHAR_BIT from <limits.h>. On the DSP I work with (TMS320C54) the value is 16.
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    What machine are you running this on that an int is only 2 chars?

    Also, endianess does not affect shifting operations(*), so your code will yield the same result on both little and big endian machines. Casting is something different though.

    (*) meaning sall, sarl, shrl or c's representations produce the same decimal results on both big and little endian
    Last edited by Nyda; 09-05-2004 at 04:06 PM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Nyda
    What machine are you running this on that an int is only 2 chars?
    Many embedded systems are 16 bit, so he might be working on a DSP or a microcontroller. He might also be working on an old DOS or Windows 3.11 machine

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    You can first AND the number with 0xFFh into one variable and then again AND the original number with 0xFF0h into another var.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 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