Thread: Daft Question Time

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Daft Question Time

    if i declare the following:

    static char sendData[8];

    is sendData[0] possible of contain a 8bit hexidecimal number, such as

    sendData[0] = 0x6085
    sendData[1] = 0x6060
    sendData[2] = 0
    sendData[3] = 0x5085

    etc

    I would have assumed this to be simple but i cant get it to work.

    Thanks

    James

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by r_james14 View Post
    is sendData[0] possible of contain a 8bit hexidecimal number, such as
    Yes, but these:

    Code:
    sendData[0] = 0x6085
    sendData[1] = 0x6060
    Are not 8-bit hex numbers, they are 16 bits*. You need two bits for each digit in a hexadecimal number, but nb. that is presuming an unsigned type.

    * well, 0x6085 is less than 32768 and so would fit in 15-bits (ie, a signed 16-bit type), but hopefully you see the point.
    Last edited by MK27; 11-18-2011 at 09:12 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    if i declare the following:

    static char sendData[8];

    is sendData[0] possible of contain a 8bit hexidecimal number, such as

    sendData[0] = 0x6085
    sendData[1] = 0x6060
    sendData[2] = 0
    sendData[3] = 0x5085

    etc

    I would have assumed this to be simple but i cant get it to work.

    Thanks

    James
    Redclare your array as... static unsigned short sendData[8]; ... or use smaller numbers...

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    ahhh yeah sorry, explains a awful lot now, next related question, is there a easy way to print this sendData in the following form

    SendData[0]=0x00c3;
    SendData[1]=0x00c4;
    SendData[2]=0x00c5;
    SendData[3]=0x00c6;
    SendData[4]=0x00c7;

    So it prints c3c4c5c6 and not combines them without having to put SendData[0]SendData[1] etc

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    ahhh yeah sorry, explains a awful lot now, next related question, is there a easy way to print this sendData in the following form

    SendData[0]=0x00c3;
    SendData[1]=0x00c4;
    SendData[2]=0x00c5;
    SendData[3]=0x00c6;
    SendData[4]=0x00c7;

    So it prints c3c4c5c6 and not combines them without having to put SendData[0]SendData[1] etc
    Lose the 00 from each value... Yes they will fit in a char type, but the notation is deceiving... 0xc3 0xc4 etc.

    You want to print them like above?..
    Code:
    for (int i = 0; i < 5; i++)
      printf("SendData[%d] = 0x%x\n", i, SendData[i]);

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    Thanks that worked brilliantly.

    Currently im trying to write code to send over Cnopen protocol which its dataset is split into 7 bytes, hence having to be specific to where the data is kept and in correct order.
    Thanks Again

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Make sure you declare your data as unsigned, otherwise

    printf ("%x", SendData[i])

    may sign-extend when it casts from char to int and give you something like FFFFFFC7. If you're only dealing with 7-bit data, though, the high bit will never be set you'll be OK, but best to be safe here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Everyone who reads this will laugh, such a daft question.
    By r_james14 in forum C Programming
    Replies: 8
    Last Post: 11-07-2011, 07:22 AM
  2. It's a question of time ...
    By Mandy_C in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 02:18 PM
  3. time question.
    By InvariantLoop in forum C Programming
    Replies: 5
    Last Post: 02-01-2005, 02:00 PM
  4. Question Time!!
    By loki_cmr in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2002, 08:08 PM
  5. just a question this time
    By korbitz in forum C Programming
    Replies: 13
    Last Post: 11-18-2001, 06:21 AM