 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alec S. Guest
|
Posted: Mon Sep 20, 2004 2:50 am Post subject: ? Strings containing NULL characters |
|
|
Hi,
I need a way of manipulating strings with NULL characters. I read
binary data from the registry and I need a way of extracting sections of
buffer but the string manipulation routines use null-terminated strings so
they don't work. I need copy and delete functions.
I've got this:
CRegKey reg;
ULONG len=0;
char* buf=NULL;
....
reg.QueryBinaryValue("value", NULL, &len);
if (!buf) {
return 0;
}
reg.QueryBinaryValue("value", buf, &len);
Now I need a way of cutting out sections of buf. Of course I'm not
married to this so if there's a better way then I'm open to it.
Any ideas?
Thanks.
--
Alec S.
alec <@> synetech <.> cjb <.> net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ken Alverson Guest
|
Posted: Mon Sep 20, 2004 3:18 am Post subject: Re: ? Strings containing NULL characters |
|
|
"Alec S." <a@a.com> wrote
| Quote: |
I need a way of manipulating strings with NULL characters. I read
binary data from the registry and I need a way of extracting sections of
buffer but the string manipulation routines use null-terminated strings so
they don't work. I need copy and delete functions.
|
std::string is capable of storing and manipulating strings with internal NUL
characters. The caveat is you need to be careful when you are passing in c
style string as parameters - until it becomes a std::string object, it will
still behave like c strings (terminating at the first NUL).
To initialize a std::string given a character array that contains NULs, you
simply pass the char pointer and the number of characters. Then you can
manipulate as you wish.
As an aside, NULL is a pointer, NUL is a character.
Ken
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alec S. Guest
|
Posted: Mon Sep 20, 2004 3:19 am Post subject: Re: ? Strings containing NULL characters |
|
|
"Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote
| Quote: | As an aside, NULL is a pointer, NUL is a character.
Ken
|
Oh, and NULL and NUL are both 0. They are the same thing but used in
different contexts. If you do a Google search for "terminated string"
you'll see about an equal number of "null terminated string" as "nul
terminated string". Null, nul, nil, and nill are all the same, they mean
nothing (nullus).
See http://en.wikipedia.org/wiki/Null for details. (You can also try
http://en.wikipedia.org/wiki/Nul and notice the redirection) ;)
--
Alec S.
alec <@> synetech <.> cjb <.> net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Mon Sep 20, 2004 6:03 am Post subject: Re: ? Strings containing NULL characters |
|
|
Alec S. wrote:
| Quote: | I need a way of manipulating strings with NULL characters.
|
Either std::string or std::vector<char> will work fine for this,
in combination with the standard algorithms.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Risto Lankinen Guest
|
Posted: Mon Sep 20, 2004 6:23 pm Post subject: NULL -vs- NUL (was: Re: ? Strings containing NULL characters |
|
|
"Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote
| Quote: |
As an aside, NULL is a pointer, NUL is a character.
|
To continue on the tangent (moderators permitting), NULL is
actually both a pointer and a character. The NUL is not really
a word but a three letter abbreviation of the word NULL. It is
typically used to refer only to the 'null' character, which is what
Ken's remark correctly points out, but it does not invalidate the
original poster's use of NULL for the same purpose in the title
of this thread.
Other similar abbreviations are BEL ('bell' character of the old
teletype terminals), TAB ('tabulator'), and SPC ('space'). See
any old ASCII or EBCID chart for more examples... :-)
Cheers!
- Risto -
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephan Br?nnimann Guest
|
Posted: Mon Sep 20, 2004 6:26 pm Post subject: Re: ? Strings containing NULL characters |
|
|
"Alec S." <a@a.com> wrote
| Quote: | Hi,
I need a way of manipulating strings with NULL characters. I read
binary data from the registry and I need a way of extracting sections of
buffer but the string manipulation routines use null-terminated strings so
they don't work. I need copy and delete functions.
I've got this:
CRegKey reg;
ULONG len=0;
char* buf=NULL;
...
reg.QueryBinaryValue("value", NULL, &len);
if (!buf) {
return 0;
}
reg.QueryBinaryValue("value", buf, &len);
Now I need a way of cutting out sections of buf. Of course I'm not
married to this so if there's a better way then I'm open to it.
Any ideas?
Thanks.
|
Are you really working with char or rather with unsigned char?
In the former case you can use std::string,
in the latter case std::vector<unsigned char>.
#include <cctype>
#include <iostream>
#include <iomanip>
#include <vector>
typedef std::vector<unsigned char> BinaryBuffer;
void printBuffer(const BinaryBuffer& buf)
{
typedef BinaryBuffer::size_type Size;
for (Size i = 0, len = buf.size(); i < len; ++i) {
unsigned char c = buf[i];
if (std::isprint(c)) {
std::cout << std::dec << c;
}
else {
std::cout << "(0x" << std::setw(2) << std::setfill('0')
<< std::hex << static_cast
}
}
std::cout << std::endl;
}
int main()
{
const unsigned char p[] = "abcdefgh ijklmnop";
BinaryBuffer buf(p, p+sizeof(p)-1); // skip terminating ' '
printBuffer(buf);
return 0;
}
BTW: Is it correct that the standard does not require char_traits
for unsigned char?
Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Milivoj Davidov Guest
|
Posted: Mon Sep 20, 2004 6:30 pm Post subject: Re: ? Strings containing NULL characters |
|
|
"Alec S." <a@a.com> wrote
| Quote: | ... I read
binary data from the registry and I need a way of extracting sections of
buffer but the string manipulation routines use null-terminated strings so
they don't work. I need copy and delete functions.
I've got this:
CRegKey reg;
ULONG len=0;
char* buf=NULL;
...
reg.QueryBinaryValue("value", NULL, &len);
if (!buf) {
return 0;
}
reg.QueryBinaryValue("value", buf, &len);
...
|
Try std::vector, it provides contiguous memory and the pointer
to the start of that memory is &buf[0]:
CRegKey reg;
ULONG len=0;
// reg.Open()
LONG res = reg.QueryBinaryValue("value", NULL, &len);
if (ERROR_SUCCESS != res || 0 == len) {
return 0;
}
std::vector<char> buf (len);
res = reg.QueryBinaryValue("value", &buf[0], &len);
Milivoj Davidov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Sep 21, 2004 11:47 am Post subject: Re: NULL -vs- NUL (was: Re: ? Strings containing NULL charac |
|
|
"Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote
| Quote: | "Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote in message
news:cili5m$lhs (AT) netlab (DOT) cs.rpi.edu...
As an aside, NULL is a pointer, NUL is a character.
To continue on the tangent (moderators permitting), NULL is actually
both a pointer and a character.
|
It's not really a tangent. There are two (or more) frequently used
meanings of NULL. According to the standard, it is a macro which
expands to an integral constant expression with the value 0. Never a
pointer, and I've never seen a case where it was a character, either.
In general use, of course, it is only used when it is meant to be a null
pointer constant; i.e. it will be immediately converted into a pointer
type. (G++ has an extension so that it is "sort of" a pointer -- it is
still an integer, but if you use it without converting it to a pointer,
you get a warning.)
Finally, I've also seen it used in descriptive text to designate a null
pointer in general (and not just the standard defined macro). This
probably stems from the fact that the usual way to implement "if p is
null" in C++ is "if ( p == NULL )".
| Quote: | The NUL is not really a word but a three letter abbreviation of the
word NULL.
|
NUL is the official name of a control character in the ASCII code set.
It has nothing to do with NULL (except that both are, in C++, integral
values evaluating to 0). The normal spelling of NUL in C and in C++ is
' '. Note, however, that this character has a different semantic in
C/C++ than NUL does in ASCII. The normal use of NUL in ASCII is for
padding, when you need to kill time, and the character should be ignored
at the receiving end. For this reason, I would strongly avoid using NUL
unless I were talking about characters transmitted on the line.
| Quote: | It is typically used to refer only to the 'null' character, which is
what Ken's remark correctly points out, but it does not invalidate the
original poster's use of NULL for the same purpose in the title of
this thread.
Other similar abbreviations are BEL ('bell' character of the old
teletype terminals), TAB ('tabulator'), and SPC ('space'). See any
old ASCII or EBCID chart for more examples...
|
I've never seen SPC, but BEL and TAB are the official names, according
to ASCII.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue Sep 21, 2004 7:02 pm Post subject: Re: NULL -vs- NUL (was: Re: ? Strings containing NULL charac |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | "Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote in message
news:<FFv3d.25716$g4.482700 (AT) news2 (DOT) nokia.com>...
"Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote in message
news:cili5m$lhs (AT) netlab (DOT) cs.rpi.edu...
As an aside, NULL is a pointer, NUL is a character.
snip
Other similar abbreviations are BEL ('bell' character of the old
teletype terminals), TAB ('tabulator'), and SPC ('space'). See any
old ASCII or EBCID chart for more examples... :-)
I've never seen SPC, but BEL and TAB are the official names, according
to ASCII.
|
The mnemonics used in Unicode, which I believe are the same as in
ASCII and ISO 646, are SP, BEL and HT.
--
Ben Hutchings
The generation of random numbers is too important to be left to chance.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 22, 2004 9:51 am Post subject: Re: NULL -vs- NUL (was: Re: ? Strings containing NULL charac |
|
|
Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote:
"Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote in message
news:<FFv3d.25716$g4.482700 (AT) news2 (DOT) nokia.com>...
"Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote in message
news:cili5m$lhs (AT) netlab (DOT) cs.rpi.edu...
As an aside, NULL is a pointer, NUL is a character.
snip
Other similar abbreviations are BEL ('bell' character of the old
teletype terminals), TAB ('tabulator'), and SPC ('space'). See
any old ASCII or EBCID chart for more examples... :-)
I've never seen SPC, but BEL and TAB are the official names,
according to ASCII.
The mnemonics used in Unicode, which I believe are the same as in
ASCII and ISO 646, are SP, BEL and HT.
|
Your right, at least with regards to Unicode. Except that I'm not sure
that the control characters are officially part of Unicode; the code
charts show them as a mnemonic in a dotted box for the representation,
but simply use <control> as the official character name.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Wed Sep 22, 2004 6:28 pm Post subject: Re: ? Strings containing NULL characters |
|
|
Good morning Stephan Brönnimann,
Stephan Br?nnimann schrieb:
| Quote: | BTW: Is it correct that the standard does not require char_traits
for unsigned char?
Yes, 21.1 of Our Holy Standard, garantees only the provision of the |
specialization of
the char_traits class template for char and wchar_t.
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
J. J. Farrell Guest
|
Posted: Fri Sep 24, 2004 11:27 am Post subject: Re: NULL -vs- NUL (was: Re: ? Strings containing NULL charac |
|
|
<kanze (AT) gabi-soft (DOT) fr> wrote
| Quote: | "Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote in message
news:<FFv3d.25716$g4.482700 (AT) news2 (DOT) nokia.com>...
"Ken Alverson" <USENET.Ken (AT) Alverson (DOT) net> wrote in message
news:cili5m$lhs (AT) netlab (DOT) cs.rpi.edu...
As an aside, NULL is a pointer, NUL is a character.
To continue on the tangent (moderators permitting), NULL is actually
both a pointer and a character.
It's not really a tangent. There are two (or more) frequently used
meanings of NULL. According to the standard, it is a macro which
expands to an integral constant expression with the value 0. Never a
pointer, and I've never seen a case where it was a character, either.
|
NULL can be defined as a pointer in C (note the cross-posting).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Sep 24, 2004 11:28 am Post subject: Re: ? Strings containing NULL characters |
|
|
[email]broeni (AT) hotmail (DOT) com[/email] (Stephan Br?nnimann) wrote in message
news:<d1d5e4b8.0409192342.4fd9cf25 (AT) posting (DOT) google.com>...
[...]
| Quote: | BTW: Is it correct that the standard does not require char_traits
for unsigned char?
|
Yes. On the other hand, it allows the implementation to provide one.
Both VC++ and g++, for example, provide one. They're incompatible, of
course:-). And of course, you're not allowed to provide one.
In practice, I'm sceptical of the utility of a basic_string< unsigned
char > -- what does it buy you that std::vector< unsigned char >
doesn't. But if you want to, and all you need is string (no IO), then
you can do something like std::basic_string< unsigned char, MyTraits >.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|