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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anna

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

    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.
  • shantanu

    #2
    Re: how to put 8 "int&qu ot; => 10100010 into one character of type "char&quot ;

    On Jun 15, 2:46 pm, Anna <petitmou...@gm ail.comwrote:
    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

    Comment

    • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

      #3
      Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

      On Jun 15, 10:46 am, Anna <petitmou...@gm ail.comwrote:
      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;
      }
      }

      Comment

      • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

        #4
        Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

        On Jun 16, 1:23 pm, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
        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

        Comment

        • Flash Gordon

          #5
          Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

          Tomás Ó hÉilidhe wrote, On 16/06/08 13:23:
          On Jun 15, 10:46 am, Anna <petitmou...@gm ail.comwrote:
          >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
          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:

          --
          Flash Gordon

          Comment

          • Willem

            #6
            Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

            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

            Comment

            • vippstar@gmail.com

              #7
              Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

              On Jun 16, 11:09 pm, Willem <wil...@stack.n lwrote:
              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.

              Comment

              • vippstar@gmail.com

                #8
                Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                On Jun 16, 3:39 pm, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
                On Jun 16, 1:23 pm, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
                >
                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
                int actually.
                It should be noted that ISO C99 does not require that.

                Comment

                • Willem

                  #9
                  Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                  vippstar@gmail. com wrote:
                  ) On Jun 16, 11:09 pm, Willem <wil...@stack.n lwrote:
                  )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

                  Comment

                  • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

                    #10
                    Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                    On Jun 16, 8:42 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                    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/com.../thread/7a31d6...

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

                    Comment

                    • Anna

                      #11
                      Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                      On 15 juin, 11:46, Anna <petitmou...@gm ail.comwrote:
                      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.

                      Comment

                      • Flash Gordon

                        #12
                        Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                        Tomás Ó hÉilidhe wrote, On 16/06/08 22:38:
                        On Jun 16, 8:42 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                        >
                        >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.
                        <snip>

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

                        Comment

                        • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

                          #13
                          Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                          On Jun 17, 11:43 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                          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).

                          Comment

                          • CBFalconer

                            #14
                            Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                            vippstar@gmail. com wrote:
                            Willem <wil...@stack.n lwrote:
                            >
                            .... snip ...
                            >
                            >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.
                            >From N1256. Note the third parameter.
                            7.20.1.4 The strtol, strtoll, strtoul,and strtoull functions
                            Synopsis
                            #include <stdlib.h>
                            long int strtol(const char * restrict nptr,
                            char ** restrict endptr,
                            int base);
                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            • Flash Gordon

                              #15
                              Re: how to put 8 &quot;int&qu ot; =&gt; 10100010 into one character of type &quot;char&quot ;

                              Tomás Ó hÉilidhe wrote, On 18/06/08 00:31:
                              On Jun 17, 11:43 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                              >
                              >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).
                              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.
                              --
                              Flash Gordon

                              Comment

                              Working...