Retrieving two chars from unsigned char *

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

    Retrieving two chars from unsigned char *

    I have a unsigned char *data that points to a 32 character string. I
    need to retrieve 2 characters in the middle of the string.

    What is the best way to do this?
    Platform is embedded 8-bit atmel processor.

    Thanks
  • Eric Sosman

    #2
    Re: Retrieving two chars from unsigned char *

    Seth wrote:
    I have a unsigned char *data that points to a 32 character string. I
    need to retrieve 2 characters in the middle of the string.
    >
    What is the best way to do this?
    Platform is embedded 8-bit atmel processor.
    unsigned char *data = ...whatever...;
    unsigned char c1 = data[15];
    unsigned char c2 = data[16];

    This should work on all processors: Atmel, Hormel,
    and Wilhelmtell.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Seth

      #3
      Re: Retrieving two chars from unsigned char *

      Thanks a lot.
      On a related note when I am comparing two unsigned char with strcmp I
      get:
      warning: pointer targets in passing argument 2 of 'strcmp' differ in
      signedness

      Should I be worried about this?

      Thanks

      On Jul 25, 11:25 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
      Seth wrote:
      I have a unsigned char *data that points to a 32 character string. I
      need to retrieve 2 characters in the middle of the string.
      >
      What is the best way to do this?
      Platform is embedded 8-bit atmel processor.
      >
              unsigned char *data = ...whatever...;
              unsigned char c1 = data[15];
              unsigned char c2 = data[16];
      >
           This should work on all processors: Atmel, Hormel,
      and Wilhelmtell.
      >
      --
      Eric Sosman
      esos...@ieee-dot-org.invalid

      Comment

      • santosh

        #4
        Re: Retrieving two chars from unsigned char *

        Seth wrote:
        Thanks a lot.
        On a related note when I am comparing two unsigned char with strcmp I
        get:
        warning: pointer targets in passing argument 2 of 'strcmp' differ in
        signedness
        >
        Should I be worried about this?
        >
        Thanks
        If you array is composed solely of the characters specified in the basic
        character set, then you needn't be worried. If your array may consist
        of any byte value then interpreting an unsigned value as a signed value
        may cause overflow and undefined behaviour.

        Comment

        • rahul

          #5
          Re: Retrieving two chars from unsigned char *

          On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:
          Thanks a lot.
          On a related note when I am comparing two unsigned char
          I take it that you meant to say "comparing two unsigned char *"
          Should I be worried about this?
          Depends on you charset. If you are using ASCII, no reasons to worry
          about that.

          Comment

          • Harald van =?UTF-8?b?RMSzaw==?=

            #6
            Re: Retrieving two chars from unsigned char *

            On Fri, 25 Jul 2008 21:14:12 -0700, Seth wrote:
            On a related note when I am comparing two unsigned char with strcmp I
            get:
            warning: pointer targets in passing argument 2 of 'strcmp' differ in
            signedness
            >
            Should I be worried about this?
            You need to cast them to char * and not rely on an implicit conversion
            your compiler happens to support. Other than that, no, you don't need to
            worry about it. strcmp works with unsigned char * anyway: it converts its
            parameters from char * back to unsigned char * before reading the strings.

            Comment

            • vippstar@gmail.com

              #7
              Re: Retrieving two chars from unsigned char *

              On Jul 26, 10:26 am, rahul <rahulsin...@gm ail.comwrote:
              On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:Than ks a lot.
              Should I be worried about this?
              >
              Depends on you charset. If you are using ASCII, no reasons to worry
              about that.
              Please explain why ASCII matters (hint: it doesn't)

              Comment

              • vippstar@gmail.com

                #8
                Re: Retrieving two chars from unsigned char *

                On Jul 26, 10:39 am, vipps...@gmail. com wrote:
                On Jul 26, 10:26 am, rahul <rahulsin...@gm ail.comwrote:
                >
                On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:>
                Thanks a lot.
                Should I be worried about this?
                >
                Depends on you charset. If you are using ASCII, no reasons to worry
                about that.
                >
                Please explain why ASCII matters (hint: it doesn't)
                I think I snipped context here.
                Anyway the discussion was about pointer signedness passed to strcmp().
                OP asked if it did matter and rahul suggested that if ASCII is
                available it doesn't matter.
                This code is perfectly valid,

                ....
                unsigned char p[] = "hello", s[] = "world";

                if(strcmp(p, s)) printf("They match!\n");
                ....

                As is

                if(strcmp((void *)p, (void *)s)) printf("They match!\n");

                Though the latter will make (most) compilers not warn.

                Comment

                • Ike Naar

                  #9
                  Re: Retrieving two chars from unsigned char *

                  In article <7174264f-12f7-4b7e-9b5e-7e3da17fd134@w7 g2000hsa.google groups.com>,
                  <vippstar@gmail .comwrote:
                  >Anyway the discussion was about pointer signedness passed to strcmp().
                  >OP asked if it did matter and rahul suggested that if ASCII is
                  >available it doesn't matter.
                  >This code is perfectly valid,
                  >
                  >...
                  >unsigned char p[] = "hello", s[] = "world";
                  >
                  >if(strcmp(p, s)) printf("They match!\n");
                  >...
                  strcmp returns 0 if the strings match:

                  if (0 == strcmp(p, s)) printf("They match!\n");

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: Retrieving two chars from unsigned char *

                    On Jul 26, 11:05 am, i...@localhost. claranet.nl (Ike Naar) wrote:
                    <vipps...@gmail .comwrote:
                    if(strcmp(p, s)) printf("They match!\n");
                    ...
                    >
                    strcmp returns 0 if the strings match:
                    >
                    if (0 == strcmp(p, s)) printf("They match!\n");
                    Heh. I wanted printf to print "They don't match!\n" though halfway I
                    forgot about it :P

                    Comment

                    • Barry Schwarz

                      #11
                      Re: Retrieving two chars from unsigned char *

                      On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vippstar@gmail. com wrote:
                      >On Jul 26, 10:26 am, rahul <rahulsin...@gm ail.comwrote:
                      >On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:Than ks a lot.
                      Should I be worried about this?
                      >>
                      >Depends on you charset. If you are using ASCII, no reasons to worry
                      >about that.
                      >
                      >Please explain why ASCII matters (hint: it doesn't)
                      strcmp is expecting to process "plain" char. On a system where char
                      is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
                      in the unique range of unsigned char, attempting to treat the value as
                      signed could produce incorrect results:

                      Attempting to store too large a value in an object yields
                      undefined behavior (C90) or "strange" behavior (C99)

                      Comparing two such object which are unequal may produce the
                      "wrong" less than/greater than result.

                      ASCII "matters" only because its seven bit values always fit in a
                      eight bit (minimum) char.


                      Remove del for email

                      Comment

                      • pete

                        #12
                        Re: Retrieving two chars from unsigned char *

                        Barry Schwarz wrote:
                        On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vippstar@gmail. com wrote:
                        >
                        >On Jul 26, 10:26 am, rahul <rahulsin...@gm ail.comwrote:
                        >>On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:Than ks a lot.
                        >>>Should I be worried about this?
                        >>Depends on you charset. If you are using ASCII, no reasons to worry
                        >>about that.
                        >Please explain why ASCII matters (hint: it doesn't)
                        >
                        strcmp is expecting to process "plain" char. On a system where char
                        is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
                        in the unique range of unsigned char, attempting to treat the value as
                        signed could produce incorrect results:
                        However, strcmp won't treat the values as signed char.

                        N869
                        7.21.4 Comparison functions
                        [#1] The sign of a nonzero value returned by the comparison
                        functions memcmp, strcmp, and strncmp is determined by the
                        sign of the difference between the values of the first pair
                        of characters (both interpreted as unsigned char) that
                        differ in the objects being compared.

                        --
                        pete

                        Comment

                        • Barry Schwarz

                          #13
                          Re: Retrieving two chars from unsigned char *

                          On Sat, 26 Jul 2008 09:01:22 -0700, Barry Schwarz <schwarzb@dqel. com>
                          wrote:
                          >strcmp is expecting to process "plain" char. On a system where char
                          >is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
                          >in the unique range of unsigned char, attempting to treat the value as
                          >signed could produce incorrect results:
                          >
                          Attempting to store too large a value in an object yields
                          >undefined behavior (C90) or "strange" behavior (C99)
                          >
                          Comparing two such object which are unequal may produce the
                          >"wrong" less than/greater than result.
                          >
                          >ASCII "matters" only because its seven bit values always fit in a
                          >eight bit (minimum) char.
                          Please disregard this obviously incorrect answer which proceeds from a
                          false assumption.

                          I have to remember to read the higher level paragraphs when reading
                          the function descriptions.


                          Remove del for email

                          Comment

                          Working...