Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26237 > unrolled thread

End of String Problem: Sending C++ string to java code

Started byclusardi2k@aol.com
First post2013-10-08 09:56 -0700
Last post2013-10-09 11:03 -0600
Articles 8 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  End of String Problem: Sending C++ string to java code clusardi2k@aol.com - 2013-10-08 09:56 -0700
    Re: End of String Problem: Sending C++ string to java code Arivald <NOSPAMarivald@interia.pl> - 2013-10-08 20:05 +0200
    Re: End of String Problem: Sending C++ string to java code Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-10-08 11:13 -0700
    Re: End of String Problem: Sending C++ string to java code Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-10-08 20:30 -0400
      Re: End of String Problem: Sending C++ string to java code Mark <i@dontgetlotsofspamanymore.invalid> - 2013-10-09 15:29 +0100
      Re: End of String Problem: Sending C++ string to java code "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-10-09 15:35 +0100
    Re: End of String Problem: Sending C++ string to java code Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 10:50 -0600
      Re: End of String Problem: Sending C++ string to java code Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 11:03 -0600

#26237 — End of String Problem: Sending C++ string to java code

Fromclusardi2k@aol.com
Date2013-10-08 09:56 -0700
SubjectEnd of String Problem: Sending C++ string to java code
Message-ID<177b1b66-ea89-4995-ab6c-9187bb754895@googlegroups.com>
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,

[toc] | [next] | [standalone]


#26238

FromArivald <NOSPAMarivald@interia.pl>
Date2013-10-08 20:05 +0200
Message-ID<l31hn5$r9t$1@news.dialog.net.pl>
In reply to#26237
W dniu 2013-10-08 18:56, clusardi2k@aol.com pisze:
> 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.
>

How You send this string?


> I tried appending the standard end of string ("\0") in C++ code...

No need, C++ string already have \0 terminator.

Maybe try send C string, You can get it from C++ string by 
std::string.c_str() function call.

-- 
Arivald

[toc] | [prev] | [next] | [standalone]


#26239

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-10-08 11:13 -0700
Message-ID<CXX4u.48292$F%.39759@fx18.iad>
In reply to#26237
On 10/8/13 9:56 AM, clusardi2k@aol.com wrote:
> 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,
>
How are you "sending" it?

Some general advice:

Java strings are *not* zero terminated. Java strings are length+char 
array.  You'll want to actually count the size of the C++ string before 
constructing the Java String.

Depending on how you're passing the character data around, you may be 
able to do that on either the C++ or Java side, though I would suggest 
the C++ side.

Also note that a char[] in C++ is not the same as a char[] in Java. 
char[] in C++ is more like (but not exactly) byte[] in Java.

[toc] | [prev] | [next] | [standalone]


#26244

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-10-08 20:30 -0400
Message-ID<l3283f$r6r$1@dont-email.me>
In reply to#26237
On 10/8/2013 8:22 PM, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> template< typename S >void serialize( S const s )
>
>    The deserializer in Java, a draft, not extensively tested:
>
> public final class Main
> {
>    public static java.lang.Object deserialize( final java.lang.String s )
>    throws java.lang.Throwable
>    { return new javax.script.ScriptEngineManager().getEngineByName( "JavaScript" )
>      .eval( s.replace( "{", "[" ).replace( "}", "]" ) +" .join( '' )" ); }
>
>    public static void main( final java.lang.String[] args )
>    throws java.lang.Throwable
>    { java.lang.System.out.println( deserialize( "{ '\\x61', '\\x62', '\\x63' }" )); }}

     Granted, "efficiency" is a dirty word nowadays ...

     ... but ...

     ... really?  Is this what it takes to transport a String
across the JNI/Java interface?  REALLY?

     (Not a JNI expert, just someone about to barf.)

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#26248

FromMark <i@dontgetlotsofspamanymore.invalid>
Date2013-10-09 15:29 +0100
Message-ID<r0qa59hehitjaqm376t30frp5qf33tkkh0@4ax.com>
In reply to#26244
On Tue, 08 Oct 2013 20:30:34 -0400, Eric Sosman
<esosman@comcast-dot-net.invalid> wrote:

>On 10/8/2013 8:22 PM, Stefan Ram wrote:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> template< typename S >void serialize( S const s )
>>
>>    The deserializer in Java, a draft, not extensively tested:
>>
>> public final class Main
>> {
>>    public static java.lang.Object deserialize( final java.lang.String s )
>>    throws java.lang.Throwable
>>    { return new javax.script.ScriptEngineManager().getEngineByName( "JavaScript" )
>>      .eval( s.replace( "{", "[" ).replace( "}", "]" ) +" .join( '' )" ); }
>>
>>    public static void main( final java.lang.String[] args )
>>    throws java.lang.Throwable
>>    { java.lang.System.out.println( deserialize( "{ '\\x61', '\\x62', '\\x63' }" )); }}
>
>     Granted, "efficiency" is a dirty word nowadays ...
>
>     ... but ...
>
>     ... really?  Is this what it takes to transport a String
>across the JNI/Java interface?  REALLY?
>
>     (Not a JNI expert, just someone about to barf.)

If using JNI all he could use the NewStringUTF function.
-- 
(\__/)  M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong?

[toc] | [prev] | [next] | [standalone]


#26275

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2013-10-09 15:35 +0100
Message-ID<vYCdnfwlqIJezsvPnZ2dnUVZ8iednZ2d@bt.com>
In reply to#26244
Eric Sosman wrote:
>     ... really?  Is this what it takes to transport a String
> across the JNI/Java interface?  REALLY?

No :)

    -- chris 

[toc] | [prev] | [next] | [standalone]


#26253

FromJim Janney <jjanney@shell.xmission.com>
Date2013-10-09 10:50 -0600
Message-ID<ydnmwmijsmv.fsf@shell.xmission.com>
In reply to#26237
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.

-- 
Jim Janney

[toc] | [prev] | [next] | [standalone]


#26254

FromJim Janney <jjanney@shell.xmission.com>
Date2013-10-09 11:03 -0600
Message-ID<ydniox6js0p.fsf@shell.xmission.com>
In reply to#26253
Jim Janney <jjanney@shell.xmission.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web