How to mid string on a binary string?

Discussion in 'C Programming' started by jt, Sep 21, 2005.

  1. jt

    jt Guest

    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
     
    jt, Sep 21, 2005
    #1
    1. Advertising

  2. "jt" <> wrote in message
    news:lP3Ye.93210$...
    > I'm needing to take a binary string


    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.

    > start at a certain position and return a
    > pointer from that postion to the end of the binary stirng.


    There's no such thing as pointer from to. A pointer in C always points to
    (or at if you will) and never from.

    > 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 */


    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(pointable) 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
     
    Alexei A. Frounze, Sep 21, 2005
    #2
    1. Advertising

  3. "jt" <> writes:
    > 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)


    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_Keith) <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.
     
    Keith Thompson, Sep 21, 2005
    #3
  4. jt

    jt Guest

    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" <> wrote in message
    news:...
    > "jt" <> writes:
    >> 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)

    >
    > 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_Keith)
    > <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.
     
    jt, Sep 21, 2005
    #4
  5. jt

    jt Guest

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

    jt

    "jt" <> wrote in message
    news:lP3Ye.93210$...
    > 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
    >
    >
     
    jt, Sep 21, 2005
    #5
  6. jt

    jt Guest

    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_some_string_with_nulls, StartPos,iStringLen);

    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" <> wrote in message
    news:...
    > "jt" <> wrote in message
    > news:lP3Ye.93210$...
    >> I'm needing to take a binary string

    >
    > 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.
    >
    >> start at a certain position and return a
    >> pointer from that postion to the end of the binary stirng.

    >
    > There's no such thing as pointer from to. A pointer in C always points to
    > (or at if you will) and never from.
    >
    >> 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 */

    >
    > 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(pointable) 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
    >
    >
     
    jt, Sep 21, 2005
    #6
  7. In article <0R4Ye.93707$>,
    jt <> wrote:
    >Let me clarify this please.


    >With a given length of the string,


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


    >pos=mid(some_some_string_with_nulls, StartPos,iStringLen);


    >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().


    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_string_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
     
    Walter Roberson, Sep 21, 2005
    #7
  8. "jt" <> writes:
    > "jt" <> wrote in message
    > news:lP3Ye.93210$...
    >> 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)

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


    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_Keith) <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.
     
    Keith Thompson, Sep 21, 2005
    #8
  9. jt

    Simon Biber Guest

    jt wrote:
    > 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)


    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_chunk bstr, size_t ele)
    {
    binary_chunk result;
    assert(ele < bstr.length);
    result.length = bstr.length - ele;
    result.chunk = malloc(result.length);
    if(!result.chunk)
    {
    fprintf(stderr, "Error allocating memory\n");
    exit(EXIT_FAILURE);
    }
    memcpy(result.chunk, 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.
     
    Simon Biber, Sep 21, 2005
    #9
  10. On Wed, 21 Sep 2005 03:55:40 +0000, jt wrote:

    > Let me clarify this please.
    >
    > Your correct, I should of not send binary string. Its a string containing
    > nulls.



    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.

    > With a given length of the string,
    >
    > int iString = 4096; /* length of string */ int iStartPos=45;
    > char *pos;
    >
    > pos=mid(some_some_string_with_nulls, StartPos,iStringLen);
    >
    > The mid function should return the pointer to starting at the StartPos
    > to the end of the string iStringLen


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

    pos = some_some_string_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_original_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
     
    Lawrence Kirby, Sep 22, 2005
    #10
  11. jt wrote:
    > 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


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

    Gregory Pietsch
     
    Gregory Pietsch, Sep 22, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MarkD
    Replies:
    0
    Views:
    758
    MarkD
    Dec 4, 2003
  2. MJB
    Replies:
    0
    Views:
    328
  3. bruce barker
    Replies:
    2
    Views:
    379
    bruce barker
    Aug 23, 2004
  4. Kieran Benton

    Causing a page refresh mid-method?

    Kieran Benton, Jul 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    458
    intrader
    Jul 1, 2005
  5. Replies:
    1
    Views:
    370
    souri challa
    Jul 8, 2005
Loading...

Share This Page