How to mid string on a binary string?

  • Follow


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


0
Reply jtsoft (74) 9/21/2005 2:45:37 AM

"jt" <jtsoft@hotmail.com> wrote in message
news:lP3Ye.93210$4i6.18011@tornado.tampabay.rr.com...
> 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


0
Reply alexfru (352) 9/21/2005 3:16:24 AM


"jt" <jtsoft@hotmail.com> 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) 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.
0
Reply kst-u (21474) 9/21/2005 3:29:17 AM

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.mib.org...
> "jt" <jtsoft@hotmail.com> 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) 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. 


0
Reply jtsoft (74) 9/21/2005 3:31:34 AM

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

jt

"jt" <jtsoft@hotmail.com> wrote in message 
news:lP3Ye.93210$4i6.18011@tornado.tampabay.rr.com...
> 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
>
> 


0
Reply jtsoft (74) 9/21/2005 3:32:52 AM

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" <alexfru@chat.ru> wrote in message 
news:3pc1keF9lgbuU1@individual.net...
> "jt" <jtsoft@hotmail.com> wrote in message
> news:lP3Ye.93210$4i6.18011@tornado.tampabay.rr.com...
>> 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
>
> 


0
Reply jtsoft (74) 9/21/2005 3:55:40 AM

In article <0R4Ye.93707$4i6.78814@tornado.tampabay.rr.com>,
jt <jtsoft@hotmail.com> 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
0
Reply roberson2 (8067) 9/21/2005 4:20:22 AM

"jt" <jtsoft@hotmail.com> writes:
> "jt" <jtsoft@hotmail.com> wrote in message 
> news:lP3Ye.93210$4i6.18011@tornado.tampabay.rr.com...
>> 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) 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.
0
Reply kst-u (21474) 9/21/2005 4:33:35 AM

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.
0
Reply news195 (400) 9/21/2005 9:30:41 AM

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

0
Reply lknews (877) 9/22/2005 2:07:43 PM

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

0
Reply GKP1 (83) 9/22/2005 9:07:59 PM

10 Replies
26 Views

(page loaded in 0.156 seconds)


Reply: