Comms

This is a discussion on Comms within the C Programming forums, part of the General Programming Boards category; Okay I have some code which stores 8 bytes of data. char Send[8]; each code Send entry has a number ...

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

    Comms

    Okay I have some code which stores 8 bytes of data.

    char Send[8];

    each code Send entry has a number stored in it (derived from some other part of code). Problem is not all is 8 bit long. So when it prints it send A3A2A1 instead 000000A3000000A2000000A1. I know that A3 would be sent as binary and take more than 2 digits however i kept it simple to explain my problem. Is there anyway i can make sure it prints 8 bits leaving free ones empty, if tht makes sense,

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two ways come to mind:

    1) Count the bits you're sending, and have "padding" bits added to those sends that are short.

    2) Make all your data you send, into fixed length fields, before you send them.

    You don't want to leave free bits empty, you want to add bits of "padding" to make them all the right length, imo.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    2) sounds the best fit for what im doing (well sounds the easist) ill be honest, how do i go about doing that? im unsure how to do either.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not at all sure you have a need for this. You have an array of 8 char's - what makes you believe that you don't have 8 x 8 = 64 bits. Have you checked the size with sizeof(arrayName), or how did you come to that conclusion?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    well if i put hex 0x03 into the first char of Send[8], wont it be saved as 11? How to i define it is saved as 00000011. 90% of my code bytes are full it is just odd examples such as 3 that im worried about.

    James

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    ive tried the following code printf("%d",sizeof(Send[4]));

    each Send I got 1, and when i simply had Send i received a value of 8

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sizeof() is reporting send[N] is 1 char = 1 byte.

    I'm unsure why you think that 00000011 has empty bits? True, they're zeroes, but they are a placeholder in making up the byte. 11 by itself, is no byte, of course. It could be read as a whole byte value, but that's special programming.

    Wait for a response from one of the "bit masters" however. I seldom work at the bit level. They may have a better answer for you.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This might help... it produces padded binary output and you may be able to adapt it to your program...

    Code:
    #include <stdio.h>
    
    int main (void)
      { char binary[33] = {0};
        unsigned int number;
        int x = 0;
        // get user input
        printf("Enter a number from 0 to 4294967295 : ");
        scanf("%u", &number);
     
       // fill array with 0
        while(x < 32)
          binary[x++] = '0';
    
        // crunch out binary number
        do
          binary[--x] = (number & 1) + '0';
        while ( (number /= 2) > 0 );
     
       // show it off
        printf("32bit binary :  %s\n\n", binary);
    
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PIC16F88 wireless serial comms with USART
    By c.fry in forum C Programming
    Replies: 1
    Last Post: 05-16-2011, 10:50 AM
  2. Parent/Child comms homework
    By csgirl in forum C Programming
    Replies: 7
    Last Post: 05-15-2010, 03:40 PM
  3. HELP... LabVIEW Fluke 45 Serial Comms VI
    By studentsaj in forum Windows Programming
    Replies: 0
    Last Post: 03-08-2002, 11:17 AM

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