Integer and character manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ruroma@gmail.com

    Integer and character manipulation

    Hello,

    I need some help with the following:

    1) I need to split a 16bit INT into two 8bit characters, and then be
    able to join these two characters to form again the original 16bit
    integer.

    For example with values expressed in binary:

    int n= 000000001111111 ;
    --> char a=00000000;
    --> char b=11111111;

    and then be able to join a and b to form back n.


    2) And last, I need to convert an integer into a string.
    For example,

    int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']


    Thank you very much!! I've tried working with casting but as far as I
    know they don't seem to be the answer. I've also searched numerous
    tutorials and web pages, but nothing productive. Any help or
    suggestion will be highly appreciated. Thank you very much!!

  • aegis

    #2
    Re: Integer and character manipulation


    ruroma@gmail.co m wrote:[color=blue]
    > Hello,
    >
    > I need some help with the following:
    >
    > 1) I need to split a 16bit INT into two 8bit characters, and then be
    > able to join these two characters to form again the original 16bit
    > integer.
    >
    > For example with values expressed in binary:
    >
    > int n= 000000001111111 ;
    > --> char a=00000000;
    > --> char b=11111111;
    >
    > and then be able to join a and b to form back n.
    >[/color]

    You can use a mask for that.

    a = n & 0xff00;
    b = n & 0x00ff;

    And use 'unsigned char' instead of 'char'. 'char' can be either
    equivalent to 'unsigned char' or 'signed char'. If you rely on signed
    char and use some value that cannot be represented with signed char,
    then you may invoke implementation defined behavior or raise an
    implementation defined signal.
    [color=blue]
    >
    > 2) And last, I need to convert an integer into a string.
    > For example,
    >
    > int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
    >[/color]

    'int' is a part of the standard integer types and can't be used to
    represent rationals. So you should either use an object type of float
    or double. To produce a string from a given value of type float or
    double, use sprintf/snprintf
    [color=blue]
    >
    > Thank you very much!! I've tried working with casting but as far as I
    > know they don't seem to be the answer. I've also searched numerous
    > tutorials and web pages, but nothing productive. Any help or
    > suggestion will be highly appreciated. Thank you very much!![/color]

    casting is not a magical thing. The reason casting exists in the
    language is for a few corner cases, namely: %p expecting a (void *)
    with *printf family of functions. To compare the return value of mktime
    to (time_t)-1 in case of error. For the is*, to* functions/macros.

    There may be one or two other places that escape me at the moment but
    not much else beyond that. The reason it doesn't produce a string is
    because a cast yields a value of a given type, given a value. As there
    does not exist a string type in C, then you can't expect that there
    will be a string produced by a cast.


    --
    aegis

    Comment

    • Barry Schwarz

      #3
      Re: Integer and character manipulation

      On 5 Sep 2005 12:03:12 -0700, ruroma@gmail.co m wrote:
      [color=blue]
      >Hello,
      >
      > I need some help with the following:
      >
      >1) I need to split a 16bit INT into two 8bit characters, and then be
      >able to join these two characters to form again the original 16bit
      >integer.
      >
      >For example with values expressed in binary:
      >
      >int n= 000000001111111 ;
      >--> char a=00000000;
      >--> char b=11111111;
      >
      >and then be able to join a and b to form back n.[/color]

      Look at the bitwise operators, especially "and" and shift.
      [color=blue]
      >
      >
      >2) And last, I need to convert an integer into a string.
      >For example,
      >
      >int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2'][/color]

      3.142 is not an integer value of any kind. Your array is not a
      string. Perhaps you'd like to tell us what you really need rather
      than provide an incorrect example.
      [color=blue]
      >
      >
      >Thank you very much!! I've tried working with casting but as far as I
      >know they don't seem to be the answer. I've also searched numerous
      >tutorials and web pages, but nothing productive. Any help or
      >suggestion will be highly appreciated. Thank you very much!![/color]

      You will get a lot more help if you post your code. We won't do you
      homework for you.


      <<Remove the del for email>>

      Comment

      • Barry Schwarz

        #4
        Re: Integer and character manipulation

        On 5 Sep 2005 13:49:26 -0700, "aegis" <aegis@mad.scie ntist.com> wrote:
        [color=blue]
        >
        >ruroma@gmail.c om wrote:[color=green]
        >> Hello,
        >>
        >> I need some help with the following:
        >>
        >> 1) I need to split a 16bit INT into two 8bit characters, and then be
        >> able to join these two characters to form again the original 16bit
        >> integer.
        >>
        >> For example with values expressed in binary:
        >>
        >> int n= 000000001111111 ;
        >> --> char a=00000000;
        >> --> char b=11111111;
        >>
        >> and then be able to join a and b to form back n.
        >>[/color]
        >
        >You can use a mask for that.
        >
        >a = n & 0xff00;[/color]

        You are missing a shift. On most systems, the result of your
        expression will not fit in a char.
        [color=blue]
        >b = n & 0x00ff;
        >
        >And use 'unsigned char' instead of 'char'. 'char' can be either
        >equivalent to 'unsigned char' or 'signed char'. If you rely on signed
        >char and use some value that cannot be represented with signed char,
        >then you may invoke implementation defined behavior or raise an
        >implementati on defined signal.
        >[color=green]
        >>
        >> 2) And last, I need to convert an integer into a string.
        >> For example,
        >>
        >> int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
        >>[/color]
        >
        >'int' is a part of the standard integer types and can't be used to
        >represent rationals. So you should either use an object type of float
        >or double. To produce a string from a given value of type float or
        >double, use sprintf/snprintf
        >[color=green]
        >>
        >> Thank you very much!! I've tried working with casting but as far as I
        >> know they don't seem to be the answer. I've also searched numerous
        >> tutorials and web pages, but nothing productive. Any help or
        >> suggestion will be highly appreciated. Thank you very much!![/color]
        >
        >casting is not a magical thing. The reason casting exists in the
        >language is for a few corner cases, namely: %p expecting a (void *)
        >with *printf family of functions. To compare the return value of mktime
        >to (time_t)-1 in case of error. For the is*, to* functions/macros.
        >
        >There may be one or two other places that escape me at the moment but
        >not much else beyond that. The reason it doesn't produce a string is
        >because a cast yields a value of a given type, given a value. As there
        >does not exist a string type in C, then you can't expect that there
        >will be a string produced by a cast.[/color]


        <<Remove the del for email>>

        Comment

        • Ian

          #5
          Re: Integer and character manipulation

          ruroma@gmail.co m wrote:[color=blue]
          > Hello,
          >
          > I need some help with the following:
          >
          > 1) I need to split a 16bit INT into two 8bit characters, and then be
          > able to join these two characters to form again the original 16bit
          > integer.
          >
          > For example with values expressed in binary:
          >
          > int n= 000000001111111 ;
          > --> char a=00000000;
          > --> char b=11111111;
          >
          > and then be able to join a and b to form back n.
          >
          >[/color]
          Sounds like homework! Mug up on shifting and masking.
          [color=blue]
          > 2) And last, I need to convert an integer into a string.
          > For example,
          >
          > int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
          >
          >[/color]
          3.142.isn't an int, its a floating point value. Look up formatted output.

          Ian

          Comment

          • aegis

            #6
            Re: Integer and character manipulation


            Barry Schwarz wrote:[color=blue]
            > On 5 Sep 2005 13:49:26 -0700, "aegis" <aegis@mad.scie ntist.com> wrote:
            >[color=green]
            > >
            > >ruroma@gmail.c om wrote:[color=darkred]
            > >> Hello,
            > >>
            > >> I need some help with the following:
            > >>
            > >> 1) I need to split a 16bit INT into two 8bit characters, and then be
            > >> able to join these two characters to form again the original 16bit
            > >> integer.
            > >>
            > >> For example with values expressed in binary:
            > >>
            > >> int n= 000000001111111 ;
            > >> --> char a=00000000;
            > >> --> char b=11111111;
            > >>
            > >> and then be able to join a and b to form back n.
            > >>[/color]
            > >
            > >You can use a mask for that.
            > >
            > >a = n & 0xff00;[/color]
            >
            > You are missing a shift. On most systems, the result of your
            > expression will not fit in a char.[/color]

            Oops. Indeed. That should have been a = (n & 0xff00) >> 8;

            [color=blue]
            >[color=green]
            > >b = n & 0x00ff;
            > >
            > >And use 'unsigned char' instead of 'char'. 'char' can be either
            > >equivalent to 'unsigned char' or 'signed char'. If you rely on signed
            > >char and use some value that cannot be represented with signed char,
            > >then you may invoke implementation defined behavior or raise an
            > >implementati on defined signal.
            > >[color=darkred]
            > >>
            > >> 2) And last, I need to convert an integer into a string.
            > >> For example,
            > >>
            > >> int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
            > >>[/color]
            > >
            > >'int' is a part of the standard integer types and can't be used to
            > >represent rationals. So you should either use an object type of float
            > >or double. To produce a string from a given value of type float or
            > >double, use sprintf/snprintf
            > >[color=darkred]
            > >>
            > >> Thank you very much!! I've tried working with casting but as far as I
            > >> know they don't seem to be the answer. I've also searched numerous
            > >> tutorials and web pages, but nothing productive. Any help or
            > >> suggestion will be highly appreciated. Thank you very much!![/color]
            > >
            > >casting is not a magical thing. The reason casting exists in the
            > >language is for a few corner cases, namely: %p expecting a (void *)
            > >with *printf family of functions. To compare the return value of mktime
            > >to (time_t)-1 in case of error. For the is*, to* functions/macros.
            > >
            > >There may be one or two other places that escape me at the moment but
            > >not much else beyond that. The reason it doesn't produce a string is
            > >because a cast yields a value of a given type, given a value. As there
            > >does not exist a string type in C, then you can't expect that there
            > >will be a string produced by a cast.[/color]
            >
            >
            > <<Remove the del for email>>[/color]

            Comment

            • ruroma@gmail.com

              #7
              Re: Integer and character manipulation

              Hi to all,

              Firstly thank you very much all of you for such a quick answer.

              Using the bitwise operators was the important hint, and yes, after
              using the masks and shifting 8 positions I've finally got the high byte
              and low byte separated as two 8 bit characters as I needed.

              As for the second part, sorry, of course 3.142 isn't an int, just quick
              writing and bad example. I'havent tried that part yet, but after more
              searching I think that fcvtf will probably do the job.

              Posting the whole code wont probably help, as these are just two little
              parts I need for doing the big task. It is not actually homework, but
              something I need to do after some long time without coding (and wasn't
              too good either when I used to code more often).

              Thank you once again :) I'll let you know If I encounter problems with
              the second part, though I hope I don't ;)

              Ian wrote:[color=blue]
              > ruroma@gmail.co m wrote:[color=green]
              > > Hello,
              > >
              > > I need some help with the following:
              > >
              > > 1) I need to split a 16bit INT into two 8bit characters, and then be
              > > able to join these two characters to form again the original 16bit
              > > integer.
              > >
              > > For example with values expressed in binary:
              > >
              > > int n= 000000001111111 ;
              > > --> char a=00000000;
              > > --> char b=11111111;
              > >
              > > and then be able to join a and b to form back n.
              > >
              > >[/color]
              > Sounds like homework! Mug up on shifting and masking.
              >[color=green]
              > > 2) And last, I need to convert an integer into a string.
              > > For example,
              > >
              > > int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
              > >
              > >[/color]
              > 3.142.isn't an int, its a floating point value. Look up formatted output.
              >
              > Ian[/color]

              Comment

              • Barry Schwarz

                #8
                Re: Integer and character manipulation

                On 5 Sep 2005 14:38:26 -0700, ruroma@gmail.co m wrote:
                [color=blue]
                >Hi to all,
                >
                > Firstly thank you very much all of you for such a quick answer.
                >
                >Using the bitwise operators was the important hint, and yes, after
                >using the masks and shifting 8 positions I've finally got the high byte
                >and low byte separated as two 8 bit characters as I needed.
                >
                >As for the second part, sorry, of course 3.142 isn't an int, just quick
                >writing and bad example. I'havent tried that part yet, but after more
                >searching I think that fcvtf will probably do the job.[/color]

                Is fcvtf a standard function?


                <<Remove the del for email>>

                Comment

                • Lawrence Kirby

                  #9
                  Re: Integer and character manipulation

                  On Mon, 05 Sep 2005 14:30:25 -0700, aegis wrote:
                  [color=blue]
                  >
                  > Barry Schwarz wrote:[color=green]
                  >> On 5 Sep 2005 13:49:26 -0700, "aegis" <aegis@mad.scie ntist.com> wrote:[/color][/color]

                  ....
                  [color=blue][color=green][color=darkred]
                  >> >You can use a mask for that.
                  >> >
                  >> >a = n & 0xff00;[/color]
                  >>
                  >> You are missing a shift. On most systems, the result of your
                  >> expression will not fit in a char.[/color]
                  >
                  > Oops. Indeed. That should have been a = (n & 0xff00) >> 8;[/color]

                  That can have problems on systems with 16 bit ints because you could be
                  shifting a negative value.

                  Lawrence

                  Comment

                  • Tim Rentsch

                    #10
                    Re: Integer and character manipulation

                    Lawrence Kirby <lknews@netacti ve.co.uk> writes:
                    [color=blue]
                    > On Mon, 05 Sep 2005 14:30:25 -0700, aegis wrote:
                    >[color=green]
                    > >
                    > > Barry Schwarz wrote:[color=darkred]
                    > >> On 5 Sep 2005 13:49:26 -0700, "aegis" <aegis@mad.scie ntist.com> wrote:[/color][/color]
                    >
                    > ...
                    >[color=green][color=darkred]
                    > >> >You can use a mask for that.
                    > >> >
                    > >> >a = n & 0xff00;
                    > >>
                    > >> You are missing a shift. On most systems, the result of your
                    > >> expression will not fit in a char.[/color]
                    > >
                    > > Oops. Indeed. That should have been a = (n & 0xff00) >> 8;[/color]
                    >
                    > That can have problems on systems with 16 bit ints because you could be
                    > shifting a negative value.[/color]

                    If ints were 16 bits, I'd be inclined to think 0xff00 would
                    be an unsigned int, which means n would be converted to unsigned
                    int before the '&' were done; the shift would then be working
                    on an unsigned int.

                    Comment

                    • Lawrence Kirby

                      #11
                      Re: Integer and character manipulation

                      On Tue, 06 Sep 2005 03:14:13 -0700, Tim Rentsch wrote:

                      ....
                      [color=blue]
                      > If ints were 16 bits, I'd be inclined to think 0xff00 would
                      > be an unsigned int, which means n would be converted to unsigned
                      > int before the '&' were done; the shift would then be working
                      > on an unsigned int.[/color]

                      You're right, the problem in this case isn't in shifting a negative value,
                      is is converting a negative value to an unsigned type which won't preserve
                      the bit pattern on 1's complement and sign-magnitude based implementations .

                      Lawrence



                      Comment

                      Working...