-
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
-
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.
-
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.
-
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?
-
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
-
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
-
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.
-
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; }