Converting a byte into an char-field filled with its bits

Converting a byte into an char-field filled with its bits

Post by Dennis Schul » Fri, 20 Jun 2003 06:42:29



Hello!

How can i convert an integer-value into a char-field like the following?

Position:  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 -------------------------------
----
Bit:       1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | \0

I try to convert the int-value in an string because i'd like to find out
which bits are set in this value.
In get informations per parallelport from an external device that send's
its different statusinfos in different bits in a byte. The first bit is on
or off. But how to recognize whether this bit is on or off?

I'am not so good in C but i try to write an program to control an device, i
developed some time ago. At now i use my windows-program which i've written
in VB. Now i'd like to control it with a linux-version of this program.

My Code till now:
// BEGIN
void bytetobin(unsigned int byte) {
    long i;
    int mask;

    char bin[144];
    mask=1;
    for(i=0;i<=3;i++) {
        if(byte!=0 && mask!=0)
            bin[16-i*2]=49;
        else
            bin[16-i*2]=48;
        mask=mask*2;
    }

    bin[8]=32;

    for(i=4;i<=7;i++) {
        if(byte!=0 && mask!=0)
            bin[14-i*2]=49;
        else
            bin[14-i*2]=48;
        mask=mask*2;
    }
    printf("value: %s\n",bin);

Quote:}

//END

I've tried to convert my VB-function into a C-function. But it doesn't work
as expected.

Thanks, to all who spend a piece of their time on my problem.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

 
 
 

Converting a byte into an char-field filled with its bits

Post by J » Fri, 20 Jun 2003 09:55:22


It is my code... i don't speak english very good but the code is
international ;-)

char *dec2bin(int decimal) {
        int temp=decimal,i=0;
        char *binario;
        double num;

        if(temp==0)
                return "0";

        num=log(temp)/log(2)+1.0;

        binario=(char*)malloc(sizeof(char)*(num+1));
        binario[(int)num]='\0';
        binario[0]='1';

        for(i=0;temp!=1;i++) {
                binario[((int)num-1)-i]=temp%2+'0';
                temp=temp/2;
        }

        return binario;

}

> Hello!

> How can i convert an integer-value into a char-field like the following?

> Position:  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9
> ------------------------------- ----
> Bit:       1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | \0

> I try to convert the int-value in an string because i'd like to find out
> which bits are set in this value.
> In get informations per parallelport from an external device that send's
> its different statusinfos in different bits in a byte. The first bit is
> on or off. But how to recognize whether this bit is on or off?

> I'am not so good in C but i try to write an program to control an
> device, i developed some time ago. At now i use my windows-program which
> i've written in VB. Now i'd like to control it with a linux-version of
> this program.

> My Code till now:
> // BEGIN
> void bytetobin(unsigned int byte) {
>    long i;
>    int mask;

>    char bin[144];
>    mask=1;
>    for(i=0;i<=3;i++) {
>        if(byte!=0 && mask!=0)
>            bin[16-i*2]=49;
>        else
>            bin[16-i*2]=48;
>        mask=mask*2;
>    }

>    bin[8]=32;

>    for(i=4;i<=7;i++) {
>        if(byte!=0 && mask!=0)
>            bin[14-i*2]=49;
>        else
>            bin[14-i*2]=48;
>        mask=mask*2;
>    }
>    printf("value: %s\n",bin);
> }
> //END

> I've tried to convert my VB-function into a C-function. But it doesn't
> work as expected.

> Thanks, to all who spend a piece of their time on my problem.



 
 
 

Converting a byte into an char-field filled with its bits

Post by Sybren Stuve » Sat, 21 Jun 2003 08:35:51


Dennis Schulz enlightened us with:

Quote:> I try to convert the int-value in an string because i'd like to find
> out which bits are set in this value.

One method is to use hexadecimal. It's easy to convert hex to bin in
your head, and it's easy to print:

printf("Hex value: 0x%X\n", value);

Another way:

int value = 392;

while(value > 0) {
        printf("%d", value % 2);
        value >>= 1;

Quote:}

This destructs the value of "value", and only prints (doesn't store in a
charater array), but if that's all you need this will do just fine.

Quote:> I'am not so good in C but i try to write an program to control an
> device, i developed some time ago. At now i use my windows-program
> which i've written in VB. Now i'd like to control it with a
> linux-version of this program.

In that case you might be a lot better off if you create a text-based
protocol. You might even want to use XML. That way it'll be so much
easier to understand what is going on, especially when you have to debug
network traffic.

Quote:> Dennis Schulz (redplayer AT web DOT de)

You'll get loads of spam if you post your real email address. I won't
email you either; ask in public, get answered in public.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

 
 
 

Converting a byte into an char-field filled with its bits

Post by Erik Max Franci » Sat, 21 Jun 2003 08:41:56



> while(value > 0) {
>         printf("%d", value % 2);
>         value >>= 1;
> }

It's implementation defined whether this halts if value is negative, due
to possible signed right bitshifts.  Furthermore, value % 2 would be
better written as value & 1 since you're already dealing with bitwise
operations anyway.

--

 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ It's better to be quotable than to be honest.
\__/  Tom Stoppard

 
 
 

Converting a byte into an char-field filled with its bits

Post by Sybren Stuve » Sat, 21 Jun 2003 08:48:03


Erik Max Francis enlightened us with:

Quote:> It's implementation defined whether this halts if value is negative, due
> to possible signed right bitshifts.

Got a point.

while(value != 0)

or simply

while(value)

would be better.

Quote:> Furthermore, value % 2 would be better written as value & 1 since
> you're already dealing with bitwise operations anyway.

Good point too ;-)

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

 
 
 

Converting a byte into an char-field filled with its bits

Post by David Schwart » Sat, 21 Jun 2003 09:34:32





> > while(value > 0) {
> >         printf("%d", value % 2);
> >         value >>= 1;
> > }
> It's implementation defined whether this halts if value is negative, due
> to possible signed right bitshifts.  Furthermore, value % 2 would be
> better written as value & 1 since you're already dealing with bitwise
> operations anyway.

    His loop starts with 'while(value>0)' so it will do nothing if value is
negative.

    DS

 
 
 

Converting a byte into an char-field filled with its bits

Post by Erik Max Franci » Sat, 21 Jun 2003 09:39:41



> Got a point.

> while(value != 0)

> or simply

> while(value)

> would be better.

That wouldn't work either; if value is negative and the implementation
shifts in the sign bit, then it will never become zero with right
shifts.

--

 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Your theory is not right; it is not even wrong.
\__/  Wolfgang Pauli

 
 
 

Converting a byte into an char-field filled with its bits

Post by J » Sat, 21 Jun 2003 11:11:57


I forgot that it's only for non-negative values (Do you need negative
values for parallel-port data?, with minor changes it would work with
2-complement numbers...)

> It is my code... i don't speak english very good but the code is
> international ;-)

> char *dec2bin(int decimal) {
>     int temp=decimal,i=0;
>     char *binario;
>     double num;

>     if(temp==0)
>         return "0";

>     num=log(temp)/log(2)+1.0;

>     binario=(char*)malloc(sizeof(char)*(num+1));
>     binario[(int)num]='\0';
>     binario[0]='1';

>     for(i=0;temp!=1;i++) {
>         binario[((int)num-1)-i]=temp%2+'0';
>         temp=temp/2;
>     }

>     return binario;
> }


>> Hello!

>> How can i convert an integer-value into a char-field like the following?

>> Position:  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9
>> ------------------------------- ----
>> Bit:       1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | \0

>> I try to convert the int-value in an string because i'd like to find
>> out which bits are set in this value.
>> In get informations per parallelport from an external device that
>> send's its different statusinfos in different bits in a byte. The
>> first bit is on or off. But how to recognize whether this bit is on or
>> off?

>> I'am not so good in C but i try to write an program to control an
>> device, i developed some time ago. At now i use my windows-program
>> which i've written in VB. Now i'd like to control it with a
>> linux-version of this program.

>> My Code till now:
>> // BEGIN
>> void bytetobin(unsigned int byte) {
>>    long i;
>>    int mask;

>>    char bin[144];
>>    mask=1;
>>    for(i=0;i<=3;i++) {
>>        if(byte!=0 && mask!=0)
>>            bin[16-i*2]=49;
>>        else
>>            bin[16-i*2]=48;
>>        mask=mask*2;
>>    }

>>    bin[8]=32;

>>    for(i=4;i<=7;i++) {
>>        if(byte!=0 && mask!=0)
>>            bin[14-i*2]=49;
>>        else
>>            bin[14-i*2]=48;
>>        mask=mask*2;
>>    }
>>    printf("value: %s\n",bin);
>> }
>> //END

>> I've tried to convert my VB-function into a C-function. But it doesn't
>> work as expected.

>> Thanks, to all who spend a piece of their time on my problem.


 
 
 

Converting a byte into an char-field filled with its bits

Post by Sybren Stuve » Sat, 21 Jun 2003 17:23:32


Erik Max Francis enlightened us with:

Quote:> That wouldn't work either; if value is negative and the implementation
> shifts in the sign bit, then it will never become zero with right
> shifts.

How about using

unsigned int value = 384;

then? OP already stated that the data contains status bits, so he's not
interested in the sign anyway.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

 
 
 

Converting a byte into an char-field filled with its bits

Post by Kevin Easto » Sat, 21 Jun 2003 18:36:14



> Dennis Schulz enlightened us with:
>> I try to convert the int-value in an string because i'd like to find
>> out which bits are set in this value.
[...]
> int value = 392;

> while(value > 0) {
>        printf("%d", value % 2);
>        value >>= 1;
> }

> This destructs the value of "value", and only prints (doesn't store in a
> charater array), but if that's all you need this will do just fine.

That will end up outputting the least significant bits to the left, and
the most significant bits to the right.

That's not exactly the usual representation :).

        - Kevin.

 
 
 

Converting a byte into an char-field filled with its bits

Post by Sybren Stuve » Sat, 21 Jun 2003 22:38:25


Kevin Easton enlightened us with:

Quote:> That will end up outputting the least significant bits to the left,
> and the most significant bits to the right.

Ah, details schmetails.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

 
 
 

1. Char bit fields

My application uses char bit fields.
When complied I receive the following warning: ...(E) Bit-field type
specified for xxx is not valid. Type unsigned assumed.

The xlc doesn't allow char bit field, and require each bit fields struct
to be of int (32 bits) type. This cause many problems in my application.

Is there any why to use char (8 bit) bit fields in Aix (4.3) C compiler?

Any help will be appreciated.
--
--------------------------------------------------------------
Boaz Raufman
Xrs Product Manager
2001 Computers & Systems, Dov Groner 7, Herzeliya 46723 ISRAEL
Phone: 972-9-9511225, Fax: 972-9-9511226

--------------------------------------------------------------

2. building new kernel

3. 16-Bit C compiler that can deal properly with bit fields

4. MB with Dual PPro and Ultra-IDE?

5. mac2lat1 Convert 8-bit quoted printable characters to 8-bit latin1

6. Solaris and Linux LDAP

7. Need help on filling current field while application is running

8. Screen clearing ...

9. How Does DT MAIL fill in the login field on the MAILER-LOGIN input box

10. How to avoid filling in the CC field of an email?

11. how2 send Email from C with subject field filled?

12. char *strcasestr(char *haystack, char *needle) a simple case independent strstr()

13. char * in network byte order to unsigned long int