tags:

views:

275

answers:

5

what i mean is that if i want to store for example 11110011 i want to store it in exactly 1 byte in memory not in array of chars.

example: if i write 10001111 as an input while scanf is used it only get the first 1 and store it in the variable while what i want is to get the whole value into the variable of type char just to consume only one byte of memory.

+3  A: 

You need to calculate the number and then just store it in a char.

If you know how binary works this should be easy for you. I dont know how you have the binary data stored, but if its in a string, you need to go through it and for each 1 add the appropriate power of two to a temp variable (initialized to zero at first). This will yield you the number after you go through the whole array.

Look here: http://www.gidnetwork.com/b-44.html

PeterK
what if i need to read the value from a file??
jhon
if you have this in a file, then you will read it into a string, and then just do what i described in my answer. A simple for loop will do the trick.
PeterK
+5  A: 

like this....

unsigned char mybyte = 0xF3;
Keith Nicholas
+1  A: 

Use an unsigned char and then store the value in it. Simple?

If you have read it from a file and it is in the form of a string then something like this should work:

char str[] = "11110011";
unsigned char number = 0;

for(int i=7; i>=0; i--)
{
    unsigned char temp = 1;
    if (str[i] == '1')
    {
        temp <<= (7-i);
        number |= temp;
    }
}
Aamir
+10  A: 

One way to write that down would be something like this:

unsigned char b = 1 << 7 |
                  1 << 6 |
                  1 << 5 |
                  1 << 4 |
                  0 << 3 |
                  0 << 2 |
                  1 << 1 |
                  1 << 0;

Here's a snippet to read it from a string:

int i;
char num[8] = "11110011";
unsigned char result = 0;

for ( i = 0; i < 8; ++i )
    result |= (num[i] == '1') << (7 - i);
UncleZeiv
I'd suggest using `|`(OR) instead of addition, though.
Hasturkun
@Hasturkun - It may be helpful to some if you elaborate as to why you would use | (OR) rather than addition.
semaj
@Hasturkun, ok; @semaj, in this case it wouldn't make any difference, but in general it's what you want to do when setting a bit, e.g. when you are overwriting an unknown value.
UncleZeiv
+2  A: 

Using a "bit field"?

#include <stdio.h>

union u {
   struct {
   int a:1;
   int b:1;
   int c:1;
   int d:1;
   int e:1;
   int f:1;
   int g:1;
   int h:1;
   };
   char ch;
};

int main()
{
   union u info;
   info.a = 1; // low-bit
   info.b = 1;
   info.c = 0;
   info.d = 0;
   info.e = 1;
   info.f = 1;
   info.g = 1;
   info.h = 1; // high-bit
   printf("%d %x\n", (int)(unsigned char)info.ch, (int)(unsigned char)info.ch);
}
pascal
And I was just wondering what interesting things a union could be used for yesterday... Could you assign to `ch` using the bit-shift method? Because if so, this seems to be the most flexible way to do it.
JAB
Yes, if you assign to `ch`, you can retrieve the bits from `a-h`. Bits seem to be "unsigned-extended", so `1` will be retrieved as `0b111...111`, i.e. `-1`.
pascal