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

C

clusardi2k

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,
 
A

Arivald

W dniu 2013-10-08 18:56, (e-mail address removed) 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.
 
D

Daniel Pitts

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.
 
S

Stefan Ram

Subject: Re: End of String Problem:

Maybe, you meant: »End-Of-String Problem«?

http://en.wikipedia.org/wiki/Compound_modifier
Sending C++ string to java code

To send a string from a process controlled by a program
written in C++ to a process controlled by a program written
in Java, one might serialize (in a way that adds a
terminating mark) the string to a TCP socket in the former
process and then read in from a socket in the latter.

For example, the C string »"a\nb"« can be serialized to the
external representation »a\nb\0« or »"a\nb"«. You can choose
any of them, or use a protocol already established like
TELNET (RFC 137) or HTTP (REST, RESTful) (libraries should
already be available for these).
 
S

Stefan Ram

To send a string from a process controlled by a program
written in C++ to a process controlled by a program written
in Java, one might serialize (in a way that adds a

I just wrote the following simple C++-string serializer
(not fully tested, just a draft):

#include <iostream>
#include <ostream>
#include <string>
#include <ios>

void serialize( ::std::string const s )
{ if( auto i = s.length() )
{ ::std::cout << "{";
while( i-- )
::std::cout << " '\\x" << ::std::hex << static_cast< int >( s.at( i ))<<
'\'' << ( i ? "," : "" );
::std::cout << " }\n"; }
else ::std::cout << "{}\n"; }

int main()
{ serialize( ::std::string{} );
serialize( ::std::string{ '\x0', '\x0', '\x0' });
serialize( ::std::string{ "alpha" }); }
 
S

Stefan Ram

::std::cout << " '\\x" << ::std::hex << static_cast< int >( s.at( i ))<<
'\'' << ( i ? "," : "" );

next attempt:

template< typename S >void serialize( S const s )
{ if( auto i = s.length() )
{ ::std::cout << "{";
for( ; i; --i )
::std::cout << " '\\x" << ::std::hex
<< S::traits_type::to_int_type( s.at( s.length() - i ))<<
'\'' << "," + !( i - 1 );
::std::cout << " }\n"; }
else ::std::cout << "{}\n"; }

This corrects the wrong sequence of the characters and the
former error when the string's char type is larger than the
int type, should work with more string types, and possibly
has on branch less due to removal of the conditional expression.
 
S

Stefan Ram

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' }" )); }}
 
E

Eric Sosman

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.)
 
M

Mark

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.
 
J

Jim Janney

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top