Word to byte (or INT to CHAR)

This is a discussion on Word to byte (or INT to CHAR) within the C Programming forums, part of the General Programming Boards category; Who can I convert a word (int or 16 bits) to 2 bytes (char or 8 bits) I tried follolowing ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Word to byte (or INT to CHAR)

    Who can I convert a word (int or 16 bits) to 2 bytes (char or 8 bits)

    I tried follolowing but it didn't work:

    TX_Buffer[length] = (char)(crc<<8) ;
    TX_Buffer[length+1] = (char)(crc && 0x00ff);

    crc is word and TX_Buffer is byte

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Don't you mean crc>>8? crc<<8 will move everything left (more significant) 8 bits, and thus take it out of the char range.

    Also, you mean &, not &&. & is a bitwise and, && is a logical and.
    Last edited by cwr; 09-30-2005 at 03:53 AM. Reason: typo

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Yes of course, my fault, just a beginner in C, thank you very much

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    unsigned char bytes[sizeof(int)];
    int n = 123;
    *(int *)bytes = n;
    after the above, the character array bytes will contain the binary representation of the integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21