How to mid string on a binary string?

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

    How to mid string on a binary string?

    I'm needing to take a binary string start at a certain position and return a
    pointer from that postion to the end of the binary stirng.

    something like this:

    char bstr[2048];
    char *pos;

    pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
    starting at element 35 */

    Any help showing me how I can do this? I don't know how I can do a char
    *mid(bstr,ele)

    Thanks,
    jt


  • Alexei A. Frounze

    #2
    Re: How to mid string on a binary string?

    "jt" <jtsoft@hotmail .com> wrote in message
    news:lP3Ye.9321 0$4i6.18011@tor nado.tampabay.r r.com...[color=blue]
    > I'm needing to take a binary string[/color]

    I'm not familiar with the concept of a binary string. As far as the C
    programming language goes, there are only character strings, more strictly,
    arrays of thereof and pointers to them. You are free to define something in
    terms of something, but then we both must know that difinition to speak the
    same language.
    [color=blue]
    > start at a certain position and return a
    > pointer from that postion to the end of the binary stirng.[/color]

    There's no such thing as pointer from to. A pointer in C always points to
    (or at if you will) and never from.
    [color=blue]
    > something like this:
    >
    > char bstr[2048];
    > char *pos;
    >
    > pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
    > starting at element 35 */[/color]

    Do you need something like:
    pos = bstr + 35;
    ???

    Mind you, if a char contains more than 1 bit (you never told how much of
    your binaries are in a char, 1 or CHAR_BIT), then you can't have a pointer
    to a bit. The minimum addressable(poi ntable) unit of memory is char (byte).
    You may alter its bits though, but for that you need to know not just the
    char's address (i.e. ptr to this char) but also the bit position inside,
    hence it's not really a single pointing thing, rather a compound pointer...
    Maybe you would like to use an integer instead of the pair
    pointer+integer ...
    But I don't know, you're vague...

    Alex


    Comment

    • Keith Thompson

      #3
      Re: How to mid string on a binary string?

      "jt" <jtsoft@hotmail .com> writes:[color=blue]
      > I'm needing to take a binary string start at a certain position and
      > return a pointer from that postion to the end of the binary stirng.
      >
      > something like this:
      >
      > char bstr[2048];
      > char *pos;
      >
      > pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
      > starting at element 35 */
      >
      > Any help showing me how I can do this? I don't know how I can do a char
      > *mid(bstr,ele)[/color]

      It's not clear (to me, anyway) just what you mean by "binary string".

      A C string is "a contiguous sequence of characters terminated by and
      including the first null character".

      Presumably in a "binary string" you want to allow null characters
      within the string -- which means you have to have some other way to
      specify how long it is. You don't seem to have done so here.

      Also, if you want to store arbitrary bit patterns, you'll be better
      off using unsigned char rather than char.

      If you're just talking about ordinary strings, you can ignore most of
      the above.

      The usual way of manipulating a string is via a pointer (of type
      char*) to its first element. The pointer only specifies where the
      first character is; it works as a pointer to the whole string because
      the end is marked by the null character.

      So, ignoring the question of determining where it ends, here's how
      to get a pointer to element 35 (which is actually the 36th element)
      of bstr:

      char bstr[2048];
      char *pos = bstr + 35;

      If you're uncomfortable with C's pointer arithmetic, you can also do
      this as:

      char bstr[2048];
      char *pos = &bstr[35];

      This is equivalent because the indexing operator is defined in terms
      of pointer arithmetic: x[y] is by definition equivalent to *(x+y), and
      the unary "*" and "&" operators cancel each other out. (Usually x is
      a pointer and y is an integer.)

      Here's an example:

      #include <stdio.h>
      int main(void)
      {
      char *s = "hello, world";
      printf("s = \"%s\"\n", s);
      printf("s+7 = \"%s\"\n", s+7);
      printf("&s[7] = \"%s\"\n", &s[7]);
      return 0;
      }

      Output:

      s = "hello, world"
      s+7 = "world"
      &s[7] = "world"

      Read section 6, "Arrays and Pointers", of the C FAQ (google "C FAQ").
      Then read the rest of it.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • jt

        #4
        Re: How to mid string on a binary string?

        You are correct. The string has null characters in it. Its a binary file
        that I read in. I shouldn't of mentioned binary string.

        jt


        "Keith Thompson" <kst-u@mib.org> wrote in message
        news:lnzmq7gktu .fsf@nuthaus.mi b.org...[color=blue]
        > "jt" <jtsoft@hotmail .com> writes:[color=green]
        >> I'm needing to take a binary string start at a certain position and
        >> return a pointer from that postion to the end of the binary stirng.
        >>
        >> something like this:
        >>
        >> char bstr[2048];
        >> char *pos;
        >>
        >> pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
        >> starting at element 35 */
        >>
        >> Any help showing me how I can do this? I don't know how I can do a char
        >> *mid(bstr,ele)[/color]
        >
        > It's not clear (to me, anyway) just what you mean by "binary string".
        >
        > A C string is "a contiguous sequence of characters terminated by and
        > including the first null character".
        >
        > Presumably in a "binary string" you want to allow null characters
        > within the string -- which means you have to have some other way to
        > specify how long it is. You don't seem to have done so here.
        >
        > Also, if you want to store arbitrary bit patterns, you'll be better
        > off using unsigned char rather than char.
        >
        > If you're just talking about ordinary strings, you can ignore most of
        > the above.
        >
        > The usual way of manipulating a string is via a pointer (of type
        > char*) to its first element. The pointer only specifies where the
        > first character is; it works as a pointer to the whole string because
        > the end is marked by the null character.
        >
        > So, ignoring the question of determining where it ends, here's how
        > to get a pointer to element 35 (which is actually the 36th element)
        > of bstr:
        >
        > char bstr[2048];
        > char *pos = bstr + 35;
        >
        > If you're uncomfortable with C's pointer arithmetic, you can also do
        > this as:
        >
        > char bstr[2048];
        > char *pos = &bstr[35];
        >
        > This is equivalent because the indexing operator is defined in terms
        > of pointer arithmetic: x[y] is by definition equivalent to *(x+y), and
        > the unary "*" and "&" operators cancel each other out. (Usually x is
        > a pointer and y is an integer.)
        >
        > Here's an example:
        >
        > #include <stdio.h>
        > int main(void)
        > {
        > char *s = "hello, world";
        > printf("s = \"%s\"\n", s);
        > printf("s+7 = \"%s\"\n", s+7);
        > printf("&s[7] = \"%s\"\n", &s[7]);
        > return 0;
        > }
        >
        > Output:
        >
        > s = "hello, world"
        > s+7 = "world"
        > &s[7] = "world"
        >
        > Read section 6, "Arrays and Pointers", of the C FAQ (google "C FAQ").
        > Then read the rest of it.
        >
        > --
        > Keith Thompson (The_Other_Keit h) kst-u@mib.org
        > <http://www.ghoti.net/~kst>
        > San Diego Supercomputer Center <*>
        > <http://users.sdsc.edu/~kst>
        > We must do something. This is something. Therefore, we must do this.[/color]


        Comment

        • jt

          #5
          Re: How to mid string on a binary string?

          My string is random in length every time I read the pipe.

          jt

          "jt" <jtsoft@hotmail .com> wrote in message
          news:lP3Ye.9321 0$4i6.18011@tor nado.tampabay.r r.com...[color=blue]
          > I'm needing to take a binary string start at a certain position and return
          > a pointer from that postion to the end of the binary stirng.
          >
          > something like this:
          >
          > char bstr[2048];
          > char *pos;
          >
          > pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
          > starting at element 35 */
          >
          > Any help showing me how I can do this? I don't know how I can do a char
          > *mid(bstr,ele)
          >
          > Thanks,
          > jt
          >
          >[/color]


          Comment

          • jt

            #6
            Re: How to mid string on a binary string?

            Let me clarify this please.

            Your correct, I should of not send binary string. Its a string containing
            nulls.

            With a given length of the string,

            int iString = 4096; /* length of string */
            int iStartPos=45;
            char *pos;

            pos=mid(some_so me_string_with_ nulls, StartPos,iStrin gLen);

            The mid function should return the pointer to starting at the StartPos to
            the end of the string iStringLen

            How can I do this mid function, or actually you can call it SubStr().

            Thanks,
            jt




            "Alexei A. Frounze" <alexfru@chat.r u> wrote in message
            news:3pc1keF9lg buU1@individual .net...[color=blue]
            > "jt" <jtsoft@hotmail .com> wrote in message
            > news:lP3Ye.9321 0$4i6.18011@tor nado.tampabay.r r.com...[color=green]
            >> I'm needing to take a binary string[/color]
            >
            > I'm not familiar with the concept of a binary string. As far as the C
            > programming language goes, there are only character strings, more
            > strictly,
            > arrays of thereof and pointers to them. You are free to define something
            > in
            > terms of something, but then we both must know that difinition to speak
            > the
            > same language.
            >[color=green]
            >> start at a certain position and return a
            >> pointer from that postion to the end of the binary stirng.[/color]
            >
            > There's no such thing as pointer from to. A pointer in C always points to
            > (or at if you will) and never from.
            >[color=green]
            >> something like this:
            >>
            >> char bstr[2048];
            >> char *pos;
            >>
            >> pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
            >> starting at element 35 */[/color]
            >
            > Do you need something like:
            > pos = bstr + 35;
            > ???
            >
            > Mind you, if a char contains more than 1 bit (you never told how much of
            > your binaries are in a char, 1 or CHAR_BIT), then you can't have a pointer
            > to a bit. The minimum addressable(poi ntable) unit of memory is char
            > (byte).
            > You may alter its bits though, but for that you need to know not just the
            > char's address (i.e. ptr to this char) but also the bit position inside,
            > hence it's not really a single pointing thing, rather a compound
            > pointer...
            > Maybe you would like to use an integer instead of the pair
            > pointer+integer ...
            > But I don't know, you're vague...
            >
            > Alex
            >
            >[/color]


            Comment

            • Walter Roberson

              #7
              Re: How to mid string on a binary string?

              In article <0R4Ye.93707$4i 6.78814@tornado .tampabay.rr.co m>,
              jt <jtsoft@hotmail .com> wrote:[color=blue]
              >Let me clarify this please.[/color]
              [color=blue]
              >With a given length of the string,[/color]
              [color=blue]
              >int iString = 4096; /* length of string */
              >int iStartPos=45;
              >char *pos;[/color]
              [color=blue]
              >pos=mid(some_s ome_string_with _nulls, StartPos,iStrin gLen);[/color]
              [color=blue]
              >The mid function should return the pointer to starting at the StartPos to
              >the end of the string iStringLen[/color]
              [color=blue]
              >How can I do this mid function, or actually you can call it SubStr().[/color]

              You can't do that in C.

              The closest you could come would be to malloc an area iStringLen long
              and memcpy the existing data into it... but it appears you
              already know that data is going to be null, so zero'ing the
              malloc()'d data would be sufficient instead of copying.

              What the above would give you is a pointer to a chunk of memory
              of the correct length. What it will *not* give you is a pointer
              to an object of that exact length in some_some_strin g_with_nulls .

              If you want to create a mid() function in C, then the result
              that it returns will have to be a structure (or pointer to
              a structure), and you will need to use routines for accessing
              or setting the data. There isn't any way in C to create an
              lvalue of a particular size that will work with the standard
              assignment operator.
              --
              "It is important to remember that when it comes to law, computers
              never make copies, only human beings make copies. Computers are given
              commands, not permission. Only people can be given permission." -- Brad Templ

              Comment

              • Keith Thompson

                #8
                Re: How to mid string on a binary string?

                "jt" <jtsoft@hotmail .com> writes:[color=blue]
                > "jt" <jtsoft@hotmail .com> wrote in message
                > news:lP3Ye.9321 0$4i6.18011@tor nado.tampabay.r r.com...[color=green]
                >> I'm needing to take a binary string start at a certain position and return
                >> a pointer from that postion to the end of the binary stirng.
                >>
                >> something like this:
                >>
                >> char bstr[2048];
                >> char *pos;
                >>
                >> pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
                >> starting at element 35 */
                >>
                >> Any help showing me how I can do this? I don't know how I can do a char
                >> *mid(bstr,ele)[/color]
                >
                > My string is random in length every time I read the pipe.[/color]

                Please don't top-post. See <http://www.caliburn.nl/topposting.html >.

                Ok. How do you keep track of the length of the string? Do you have a
                separate variable that tells you how long it is?

                You might consider something like this:

                #define MAX_LEN 2048
                unsigned char bstr[MAX_LEN];
                int curr_len;

                /*
                * Assume you've read some data into bstr, and curr_len holds the
                * number of bytes you've read.
                */

                unsigned char *new_bstr = bstr + 35;
                int new_len = curr_len - 35;

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Simon Biber

                  #9
                  Re: How to mid string on a binary string?

                  jt wrote:[color=blue]
                  > I'm needing to take a binary string start at a certain position and return a
                  > pointer from that postion to the end of the binary stirng.
                  >
                  > something like this:
                  >
                  > char bstr[2048];
                  > char *pos;
                  >
                  > pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
                  > starting at element 35 */
                  >
                  > Any help showing me how I can do this? I don't know how I can do a char
                  > *mid(bstr,ele)[/color]

                  First: If it has embedded null characters, it's not a string. It's just
                  an array of char. The length will be stored separately. Consider making
                  a struct:

                  typedef struct {
                  unsigned char *chunk;
                  size_t length;
                  } binary_chunk;

                  Then you can write the mid function like this:

                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <string.h>
                  #include <assert.h>

                  binary_chunk
                  mid(binary_chun k bstr, size_t ele)
                  {
                  binary_chunk result;
                  assert(ele < bstr.length);
                  result.length = bstr.length - ele;
                  result.chunk = malloc(result.l ength);
                  if(!result.chun k)
                  {
                  fprintf(stderr, "Error allocating memory\n");
                  exit(EXIT_FAILU RE);
                  }
                  memcpy(result.c hunk, bstr.chunk + ele, result.length);
                  return result;
                  }

                  The binary_chunk returned by this function contains a new copy of part
                  of bstr, in newly allocated memory. The allocated memory must be
                  released by calling the free function when no longer required.

                  --
                  Simon.

                  Comment

                  • Lawrence Kirby

                    #10
                    Re: How to mid string on a binary string?

                    On Wed, 21 Sep 2005 03:55:40 +0000, jt wrote:
                    [color=blue]
                    > Let me clarify this please.
                    >
                    > Your correct, I should of not send binary string. Its a string containing
                    > nulls.[/color]


                    The problem isn't with "binary" it is with "string". In C strings cannot
                    contain nulls, they are terminated by the first one in the data. You have
                    binary data which isn't a string.
                    [color=blue]
                    > With a given length of the string,
                    >
                    > int iString = 4096; /* length of string */ int iStartPos=45;
                    > char *pos;
                    >
                    > pos=mid(some_so me_string_with_ nulls, StartPos,iStrin gLen);
                    >
                    > The mid function should return the pointer to starting at the StartPos
                    > to the end of the string iStringLen[/color]

                    Pointers don't contain any length information. You probably need to create
                    another variable that holds the length. You could write

                    pos = some_some_strin g_with_nulls + StartPos;

                    pos will then point to the start of the data you want. There's no error
                    checking here so there had better be at least StartPos elements of data
                    in the array. You may also need

                    length = length_of_origi nal_data - StartPos;

                    pos points into the original data array. If you need it to point to a
                    separate copy of the data that can be used independently you will need to
                    create another array and use something like memcpy() to copy the section
                    of data you want into it. This is closer to the behaviour of mid/mid$
                    function you find in languages such as BASIC.

                    Lawrence

                    Comment

                    • Gregory Pietsch

                      #11
                      Re: How to mid string on a binary string?


                      jt wrote:[color=blue]
                      > I'm needing to take a binary string start at a certain position and return a
                      > pointer from that postion to the end of the binary stirng.
                      >
                      > something like this:
                      >
                      > char bstr[2048];
                      > char *pos;
                      >
                      > pos=mid(bstr,35 ); / *return a pointer of the rest of the binary string
                      > starting at element 35 */
                      >
                      > Any help showing me how I can do this? I don't know how I can do a char
                      > *mid(bstr,ele)
                      >
                      > Thanks,
                      > jt[/color]

                      This is another job for the string library from FreeDOS edlin,
                      available from either ibiblio or alt.sources.

                      Gregory Pietsch

                      Comment

                      Working...