473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to concatenate strings that contains has nulls?

jt
I can't seem to find a way to concatenate strings that have nulls within the
string.

I have a string that I need another string that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?

Thanks,
jt
Nov 15 '05 #1
13 2334
"jt" <jt****@hotmail.com> writes:
I can't seem to find a way to concatenate strings that have nulls
within the string.

I have a string that I need another string that has nulls in it and
what to append the 2nd string, 3 string and so forth to the 1st
string.

Any ideas how to go about this?


It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.

Since strcat() works with strings, it can't work with the sequences
you're using (as I'm sure you've discovered.

Since you don't have the trailing '\0' to mark the end of your
sequence, you have to have some other way of specifying how long it
is. Once you've done that, you can probably use memcpy() (or
memmove() if there's a possibility of overlap) to copy things around.
The required arguments to memcpy() are determined by the lengths of
the sequences you want to copy. Since you haven't told us how that's
defined, we can't provide more details.

Of course, you still have the same memory allocation issues (making
sure the target is big enough to hold the concatentated result) that
you have with real strings.

--
Keith Thompson (The_Other_Keith) 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 #2

"jt" <jt****@hotmail.com> wrote in message
news:h1*******************@tornado.tampabay.rr.com ...
I can't seem to find a way to concatenate strings that have nulls within
the string.
Of course not. By definition, a string cannot contain
embedded null characters. The null character ('\0')
is used to denote the end of a string.

I have a string that I need another string that has nulls in it and what
to append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?


Stop thinking of them as strings. They're simply arrays
of characters. Then look up 'memcpy()' and 'memmove()'.
(Observe the notations in the documentation about overlapping
areas of memory).

-Mike
Nov 15 '05 #3
>I can't seem to find a way to concatenate strings that have nulls within the
string.
If it has nulls internal to it, it's *NOT* a string.
How do you find the end or length of such a non-string?

I have a string
No, you don't. It's NOT a string!
that I need another string
Another *NON*-string!
that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.
They AREN'T STRINGS!!
Any ideas how to go about this?


Assuming:
You have as many ByteSequences ByteSequence1, ByteSequence2, ...
as you need, of length ByteSequence1Length, ByteSequence2Length, ...
and that there is sufficient storage at the end of ByteSequence1 to
hold the rest of them:

memcpy(ByteSequence1+ByteSequence1Length, ByteSequence2, ByteSequence2Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length,
ByteSequence3, ByteSequence3Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length+ByteSequence3Length,
ByteSequence4, ByteSequence4Length);
...

NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ... ;
Gordon L. Burditt
Nov 15 '05 #4
Keith Thompson <ks***@mib.org> wrote:
It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5
>> It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?


But you still have no way to figure out when you've reached the
LAST one. And you don't know whether the last \0 is part of
the non-string.

Gordon L. Burditt
Nov 15 '05 #6

jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.
This is impossible. By definition, a null marks the end of a string. You
can't have nulls within a string.

Perhaps you are thinking not of a string, but an array of characters of
known length.

I have a string that I need another string that has nulls in it and what
to append
the 2nd string, 3 string and so forth to the 1st string.
To concatenate two arrays, use memcpy()


Any ideas how to go about this?

Thanks,
jt


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 15 '05 #7
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within the
string.


Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.
Nov 15 '05 #8
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Keith Thompson <ks***@mib.org> wrote:
It's logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it's not a string.


But it would be acceptable to refer to the array OP mentioned as a
series of contiguous strings, would it not? The sequence as a whole
is not a string, but the subsequences are, yes?


Only if the last character of the array happens to be a '\0';
otherwise it's a series of contiguous strings followed by an
unterminated character array.

In any case, that's probably not a useful way to look at it. They
seem to be just character arrays with '\0' being an ordinary value
with no special significance.

--
Keith Thompson (The_Other_Keith) 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 #9
jt

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthli nk.net...
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.


Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string. But, what if that string happen to be a
binary file?
Nov 15 '05 #10
In article <o4********************@tornado.tampabay.rr.com> ,
jt <jt****@hotmail.com> wrote:
True, a null terminates a string. But, what if that string happen to be a
binary file?


Then it is still terminated by the first NUL character.

If you want something that is able to hold NUL characters within it,
then you are NOT using a "string". A string is DEFINED to end
at the first NUL.

If you want to work with groupings of arbitrary binary characters
that might include NUL, then you probably want to work with an array
of char (better yet, unsigned char).
C90 does not offer any mechanism to dynamically determine "the size"
[allocated? initialized? Highest entry written to? Highest entry
read from?] of an array of char. The closest it comes to that is
that if you have declared a character array (not pointer to char!)
then within the same scope you can use sizeof to get to the size:
char foo[123]; /* sizeof(foo) would be 123 * sizeof(char) */

You can't do this across scope boundaries. For example,

int baz(char myarg[]) {
return sizeof myarg;
}

void koz(void) {
char foo[123];
printf( "size of foo was: %d\n", baz(foo) );
}

This will NOT print out 123: C89 does NOT "somehow" transmit the
"real" size when it passes an object in as an argument.
(Instead, you will find that myarg is treated as a pointer to
a character, so the size printed will be the size of a char pointer.)
C99 offers a new mechanism, the variable sized array; if that is
used (and everything is passed appropriately) then you can determine
an object size at a lower level routine.
--
Watch for our new, improved .signatures -- Wittier! Profounder! and
with less than 2 grams of Trite!
Nov 15 '05 #11
On Sat, 24 Sep 2005 02:44:36 GMT, in comp.lang.c , "jt"
<jt****@hotmail.com> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthl ink.net...
jt wrote:
I can't seem to find a way to concatenate strings that have nulls within
the string.
Your problem is meaningless. A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string. But, what if that string happen to be a
binary file?


All computer data is binary.

And anyway it doesn't matter - the definition of a string is a
sequence of characters terminated by a null. The first null is thus
the end of the string.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12
Mark McIntyre wrote:

On Sat, 24 Sep 2005 02:44:36 GMT, in comp.lang.c , "jt"
<jt****@hotmail.com> wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:9f****************@newsread3.news.atl.earthl ink.net...
jt wrote:
I can't seem to find a way to concatenate
strings that have nulls within
the string.

Your problem is meaningless.
A null terminates a string. There is no
such thing as nulls within a string.


True, a null terminates a string.
But, what if that string happen to be a
binary file?


All computer data is binary.


"Binary files" is standard terminology.

The standard describes two kinds of streams:
binary streams and text streams.
Those kinds of streams would go accordingly
to either binary files or text files.

N869
7.19.3 Files
[#2] Binary files are not truncated, except as defined in
7.19.5.3. Whether a write on a text stream causes the
associated file to be truncated beyond that point is
implementation-defined.

--
pete
Nov 15 '05 #13

jt wrote:
I can't seem to find a way to concatenate strings that have nulls within the
string.

I have a string that I need another string that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?

Thanks,
jt


Okay, so you want to concatenate strings, but you want to preserve null
characters? You might want to look at the string-handling code of
FreeDOS edlin (available on ibiblio or alt.sources). Also, these
wouldn't be C strings, but BLOBs (Binary Large OBjects) as they contain
the null character, so you'd need to store lengths as well.

Gregory Pietsch

Nov 15 '05 #14

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

Similar topics

4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
30
by: priya | last post by:
Hi How to concatenate two integer Values. Example Program : #include "Port.h" #include "BinaryConversion.h" # include "iostream.h"
2
by: HumanJHawkins | last post by:
The following query works perfectly (returning all words on a list called "Dolch" that do not contain a form of "doing"): SELECT 'Dolch' AS , dbo.Dolch.vchWord FROM dbo.Dolch LEFT OUTER JOIN...
0
by: LesM | last post by:
This is a change of behaviour between Access 2000 SP3 and Access 2002 SP3. I have Progress table that is linked via ODBC into Access using OpenLink Lite for Progress 9.0b. For over a year, using...
37
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I...
3
by: Lars Tackmann | last post by:
Hi - I need a function to concatenate a variable number of strings. I have two ideas but cannot deside which way to go. 1) use the "stdarg" macros - if i use these macros it will be easy to step...
4
by: Barry | last post by:
Hi all! I support a rather large production EDI application with a number of C programs, and I ran across a very interesting problem. I have some code that used to work just fine for years, and...
23
by: tejasm | last post by:
Hi, I have - char* c="file.txt" char* con="AB.txt"; I want to replace and concatenate the above to get a new string- "fileAB.txt"
2
by: CharlesL | last post by:
I am trying to handle binary strings in php. I get a binary output initialization vector from mcrypt as such: from mcrypt: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); This output may have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.