Thread: short value to char

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    short value to char

    How i can convert a short value (example 120) to char?
    I must do it using bit shifting but how..?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suppose you are not looking for something like this:
    Code:
    short x = 120;
    char y;
    
    ...
    y = x;
    You may need to write:
    Code:
    y = (char)x;
    if the compiler issues a warning for "loss of precision".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    Thats not solve my problem. Decimal 120 is hex value 78.
    I need to get first 8 bits in one char and last 8 bits to another char.
    I just dont know how i have to shift those bits..

    I hope you understand what i try to say

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming that sizeof(short) is 2, you can do this;
    Code:
    #include <stdio.h>
    
    typedef union
    {
         short short_value;
         char char_value[2];
    } Converter;
    
    int main()
    {
         Converter c;
         c.short_value = 120;
         
      
         fprintf(stdout, "%d %d\n", x.char_value[0], x.char_value[1]);    /* print the two chars as int values */
    }
    Note that the values output are machine dependent (they depend on endianness).

    You can also do this via bit fiddling, or by manipulation of a pointer, but I'll leave that as a further exercise. The technique above will, at least, give the same values that you should obtain via bit fiddling.
    Last edited by grumpy; 06-03-2009 at 03:20 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by downfall80 View Post
    I need to get first 8 bits in one char and last 8 bits to another char.
    Why didn't you just say that in the first place? Read the bit-shifting FAQ. Shift by the number of bits in a byte. (Hint: CHAR_BIT)


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-22-2008, 07:20 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM