How to mid string on a binary string?

J

jt

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
 
A

Alexei A. Frounze

jt said:
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
 
K

Keith Thompson

jt said:
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.
 
J

jt

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 said:
jt said:
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) (e-mail address removed)
<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.
 
J

jt

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 said:
jt said:
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
 
W

Walter Roberson

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.
 
K

Keith Thompson

jt said:
jt said:
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;
 
S

Simon Biber

jt said:
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.
 
L

Lawrence Kirby

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
 
G

Gregory Pietsch

jt said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top