473,770 Members | 5,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to mid string on a binary string?

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
Nov 15 '05 #1
10 2385
"jt" <jt****@hotmail .com> wrote in message
news:lP******** ***********@tor nado.tampabay.r r.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(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
Nov 15 '05 #2
"jt" <jt****@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_Keit h) ks***@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.
Nov 15 '05 #3
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" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"jt" <jt****@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_Keit h) ks***@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.

Nov 15 '05 #4
jt
My string is random in length every time I read the pipe.

jt

"jt" <jt****@hotmail .com> wrote in message
news:lP******** ***********@tor nado.tampabay.r r.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

Nov 15 '05 #5
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_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" <al*****@chat.r u> wrote in message
news:3p******** ****@individual .net...
"jt" <jt****@hotmail .com> wrote in message
news:lP******** ***********@tor nado.tampabay.r r.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(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

Nov 15 '05 #6
In article <0R************ *******@tornado .tampabay.rr.co m>,
jt <jt****@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_s ome_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().


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
Nov 15 '05 #7
"jt" <jt****@hotmail .com> writes:
"jt" <jt****@hotmail .com> wrote in message
news:lP******** ***********@tor nado.tampabay.r r.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_Keit h) ks***@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.
Nov 15 '05 #8
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_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.
Nov 15 '05 #9
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_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


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

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8770
by: Niko Korhonen | last post by:
I'm currently in the process of programming a multimedia tagging library in standard C++. However, I've stumbled across one or two unclear issues while working with the library. First of all, is it safe to store binary data in std::string? This question rose from my implementation with APEv2 tags. An APEv2 tag's field value can contain either UTF encoded text or binary data. I've decided to use std::string to represent the field value....
4
1675
by: Lampa Dario | last post by:
Hi, I need to estract single characters in a string, that may be '0' or '1', and evaluate them. I defined these variables char *binary; char cypher; int value;
20
8811
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for something simple. A single class, preferrably. Does such a thing exist? Thanks, Chris
7
17071
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the MD5 is computing the right Byte for the hash when compared to other MD5 hash generators online. However, when I attempt to convert it back to tring using the line String outputData = textConverter.GetString( result ) ; I essentially get...
2
13128
by: Edvin | last post by:
Why is binary array written to a file different than when converting the binary array to string and then writing it to file! For example: // --- Allocate byte array byte arrByte = new byte; for( int i=1; i< arrByte.Length; i++ ) arrByte = (byte)i;
14
1802
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I can do this in notepad and it works fine, but when I start getting in to reading the files I think it has some encoding problem. I tried saving the file with every encoding option. When I open a PDF in the
3
3135
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British television comedy sketch show that first aired on the BBC on October 5, 1969. A total of 45 episodes were made over four series. However, the Python phenomenon was much greater, spawning stage tours, a musical, four films, numerous albums, and...
7
19230
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
6
10569
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf). Seperately the posting words fine but when I want to create one multipart message with both then things go wrong. The binary file is converted and of datatype byte() The xml file is just a string.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.