how to put 8 "int" => 10100010 into one character of type "char"

A

Anna

I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.
 
S

shantanu

I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.

Just convert the binary value to hex and assign it to char variable.

Shantanu
 
T

Tomás Ó hÉilidhe

I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.


In C, you use "0x" to specify a hexadecimal number, e.g.:

0x784b

Standard C doesn't have a facility for specifying binary, but all
embedded systems compilers have "0b":

0b10100010

For a desktop computer, e.g. a PC, You have the option though of using
syntax such as the following:

#include <stdio.h>

int main(void)
{
unsigned i = B("10100010");

printf("%u\n",i);
}

This will print the decimal number 162.

Here's the code I've written for "B". I've only written it in the last
two minutes and I've only tested it with the binary number 10100010 so
you might wanna make sure it's perfect:

#include <assert.h>

unsigned B(char const *binary)
{
unsigned retval = 0;

assert(binary);
assert(*binary);

for ( ; ; retval <<= 1)
{
assert('0' == *binary || '1' == *binary);

if ('1' == *binary++) retval |= 1;

if (!*binary) return retval;
}
}
 
T

Tomás Ó hÉilidhe

unsigned B(char const *binary)
{
    unsigned retval = 0;

    assert(binary);


That should be:

assert(binary != 0)

because C needs the argument to "assert" to be of integer type
 
F

Flash Gordon

Tomás Ó hÉilidhe wrote, On 16/06/08 13:23:
In C, you use "0x" to specify a hexadecimal number, e.g.:

0x784b

Standard C doesn't have a facility for specifying binary, but all
embedded systems compilers have "0b":

0b10100010

I believe that this is incorrect. For a start I believe that the
compiler for the TMS320C2xx/5x DSPs by Texas Instruments does *not* have
this extension. In fact, the only extensions it has are implementing
most of the standard C library (it is not hosted, so it is not required
to implement it) and items specifically to do with using the HW
(extension to use IO ports, asm and a few others).

Please don't say "all" as you don't have the experience to know that it
is true.
For a desktop computer, e.g. a PC, You have the option though of using
syntax such as the following:

#include <stdio.h>

int main(void)
{
unsigned i = B("10100010");

printf("%u\n",i);
}

This will print the decimal number 162.

Hmm. No prototype for B in scope, not very good.
Here's the code I've written for "B". I've only written it in the last
two minutes and I've only tested it with the binary number 10100010 so
you might wanna make sure it's perfect:

#include <assert.h>

unsigned B(char const *binary)
{
unsigned retval = 0;

assert(binary);
assert(*binary);

for ( ; ; retval <<= 1)

Not very sensible use of a for loop IMHO. More sense would be a "do
while" loop or, so that it can cope with an empty string, a "while" loop
with the change to retval being done in the loop.
{
assert('0' == *binary || '1' == *binary);

if ('1' == *binary++) retval |= 1;

if (!*binary) return retval;
}
}

Finally, people have posted macros for achieving this over the years
which would enable you to have a compile-time constant which is a
massive advantage IMHO. For example:
http://groups.google.co.uk/group/co...e+constant+group:comp.lang.c#e948360835df0e0d
 
W

Willem

Tomás Ó hÉilidhe wrote:
) Here's the code I've written for "B". I've only written it in the last
) two minutes and I've only tested it with the binary number 10100010 so
) you might wanna make sure it's perfect:
)
) #include <assert.h>
)
) unsigned B(char const *binary)
) {
) unsigned retval = 0;
)
) assert(binary);
) assert(*binary);
)
) for ( ; ; retval <<= 1)
) {
) assert('0' == *binary || '1' == *binary);
)
) if ('1' == *binary++) retval |= 1;
)
) if (!*binary) return retval;
) }
) }

What's wrong with the standard C library ?
As you may know, strtol takes an optional base parameter, which can be 2:

strtol(binary, NULL, 2)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
V

vippstar

Tomás Ó hÉilidhe wrote:

) Here's the code I've written for "B". I've only written it in the last
) two minutes and I've only tested it with the binary number 10100010 so
) you might wanna make sure it's perfect:
)
) #include <assert.h>
)
) unsigned B(char const *binary)
) {
) unsigned retval = 0;
)
) assert(binary);
) assert(*binary);
)
) for ( ; ; retval <<= 1)
) {
) assert('0' == *binary || '1' == *binary);
)
) if ('1' == *binary++) retval |= 1;
)
) if (!*binary) return retval;
) }
) }

What's wrong with the standard C library ?
As you may know, strtol takes an optional base parameter, which can be 2:
The base parameter is not optional... what do you mean optional?
I agree that it's best to use strtol instead of that algorithm.
 
V

vippstar

That should be:

assert(binary != 0)

because C needs the argument to "assert" to be of integer type
int actually.
It should be noted that ISO C99 does not require that.
 
W

Willem

(e-mail address removed) wrote:
)> What's wrong with the standard C library ?
)> As you may know, strtol takes an optional base parameter, which can be 2:
) The base parameter is not optional... what do you mean optional?
) I agree that it's best to use strtol instead of that algorithm.

You can put in 0. I agree that you have to use three parameters,
but because 0 doesn't mean 'use base 0', but rather 'use something
else' I call it that. Same with the second 'endptr' parameter.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
T

Tomás Ó hÉilidhe

Not very sensible use of a for loop IMHO. More sense would be a "do
while" loop or, so that it can cope with an empty string, a "while" loop
with the change to retval being done in the loop.


I originally started out with a canonical for loop but then decided it
was better to roll my own.

If you'd like to propose an alternative, I'm all ears.

Finally, people have posted macros for achieving this over the years
which would enable you to have a compile-time constant which is a
massive advantage IMHO. For example:http://groups.google.co.uk/group/comp.lang.c/browse_frm/thread/7a31d6...


Brilliant macro. That Matthew Hendry chap is a bright ol' penny.
 
A

Anna

I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.

wow! I was surprise to see these many answers, you guys are so
generous. Thank you all of you for the solution :). I'm new to C and
I think it's so difficult, I hate it so much haha.
 
F

Flash Gordon

Tomás Ó hÉilidhe wrote, On 16/06/08 22:38:
I originally started out with a canonical for loop but then decided it
was better to roll my own.

If you'd like to propose an alternative, I'm all ears.

<snip>

I did above. You should be able to recast your loop easily enough as
either a while-do or a while loop.
 
T

Tomás Ó hÉilidhe

I did above. You should be able to recast your loop easily enough as
either a while-do or a while loop.


If you could give me a few lines of code that'd be great. (I'm not
trying to put you on the spot or anything, just wondering what kind of
loop you'd make).
 
F

Flash Gordon

Tomás Ó hÉilidhe wrote, On 18/06/08 00:31:
If you could give me a few lines of code that'd be great. (I'm not
trying to put you on the spot or anything, just wondering what kind of
loop you'd make).

Your code was as follows:
#include <assert.h>

unsigned B(char const *binary)
{
unsigned retval = 0;

assert(binary==NULL);
assert(*binary);

for ( ; ; retval <<= 1)
{
assert('0' == *binary || '1' == *binary);

if ('1' == *binary++) retval |= 1;

if (!*binary) return retval;
}

}

I would be more likely do do something like...

#include <assert.h>

unsigned B(const char *binary)
{
unsigned retval = 0;
assert(binary==NULL);

while (*binary) {
assert('0' == *binary || '1' == *binary);
retval <<= 1;
if ('1' == *binary++) retval |= 1;
}
}

Or maybe
#include <assert.h>

unsigned B(const char *binary)
{
unsigned retval = 0;
assert(binary==NULL);
assert(*binary);

do {
assert('0' == *binary || '1' == *binary);
retval <<= 1;
if ('1' == *binary) retval |= 1;
} while (*++binary)
}

Or maybe
#include <assert.h>

unsigned B(const char *binary)
{
unsigned retval = 0;
assert(binary==NULL);

for (retval=0; *binary; binary++) {
assert('0' == *binary || '1' == *binary);
retval <<= 1;
if ('1' == *binary) retval |= 1;
}
}

None of the above abuse the for loop the way you did. Some of them will
treat an empty string as meaning 0 (deliberately). There may be bugs
since I'm knackered, but they should give you the general idea.

Of course, I would actually use one of the macro versions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top