Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: End of String Problem: Sending C++ string to java code Date: Wed, 09 Oct 2013 11:03:34 -0600 Organization: bilaterally symmetric Lines: 26 Message-ID: References: <177b1b66-ea89-4995-ab6c-9187bb754895@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx05.eternal-september.org; posting-host="c75a2e3068ccf6c98395286f3e40b97d"; logging-data="25283"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FPcEJYBea1owFrRv4LU0l" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:9731VncYpVy9+vo98RSvY3iBikU= sha1:5Cwe2bReackoeB2y9ocaFfq1dus= Xref: csiph.com comp.lang.java.programmer:26254 Jim Janney writes: > clusardi2k@aol.com writes: > >> Question: How should I send a string from C++ to java. >> >> When I send a string (std::string) from the C++ code to the java code (String), I have to count (on the java side) the number of characters that I expect. >> >> If I don't take a specific number of characters I get more characters than I want on the java side. The extra characters are garbage. >> >> I tried appending the standard end of string ("\0") in C++ code and sending the string to java code, but the problem was not resolved. >> >> Any ideas, >> Thanks, > > My C++ is very rusty at the moment, but I'm pretty sure that instances > of std::string can contain nul characters internally. Use the size or > length methods to get the number of characters. And if you're calling std::string::data, there's no guarantee that the result will be terminated. See http://www.cplusplus.com/reference/string/string/data/ -- Jim Janney