Thanks for the help.
Basically I need to call a function and that function only takes a
"long" arg. But I only have a String. That's why I want to convert the
String into "long" and then inside the function, I convert the "long"
back to the original String...It doesn't even sound right but I don't
have a better solution since I can't change the function signature...
> >I have a String: "This is a test" and I need to convert it into a long
> > value. Then I need to convert the long value back to the original
[quoted text clipped - 23 lines]
>
> :)
Frank van Schie - 08 Jul 2006 10:49 GMT
> Thanks for the help.
>
[quoted text clipped - 3 lines]
> back to the original String...It doesn't even sound right but I don't
> have a better solution since I can't change the function signature...
The simple answer is: You probably do not want to call that function
with a representation of a String, since obviously the function is not
designed for that. It doesn't do what you want, and you either have to
use a different one, or make your own.
It's impossible to say which, given that you haven't answered Luc's
question yet: WHY.
Don't say what solution you picked, say what you want to do.

Signature
Frank
Jeffrey Schwab - 08 Jul 2006 13:34 GMT
> Basically I need to call a function and that function only takes a
> "long" arg. But I only have a String. That's why I want to convert the
> String into "long" and then inside the function, I convert the "long"
> back to the original String...It doesn't even sound right but I don't
> have a better solution since I can't change the function signature...
This comes up when trying to extend certain scripting languages in C.
The usual solution is pass the function a pointer to the object, in this
case the string. This is theoretically non-portable, because of the
pointer/long conversions, but it works in practice.
Are you using JNI?
Rhino - 08 Jul 2006 14:04 GMT
Do you have the source code for the function you are calling? Or even API
documentation for what it's supposed to do? If so, it would help if you
posted it.
If the input to the function is a long, it is probably doing something
mathematical, like calculating a cube root. In that case, it makes no sense
to take the cube root of "dog" or "this is my string", even if you convert
it to a long.
Perhaps you are using the wrong function entirely??
--
Rhino
> Thanks for the help.
>
[quoted text clipped - 31 lines]
>>
>> :)
Dražen Gemić - 08 Jul 2006 16:26 GMT
> Thanks for the help.
>
[quoted text clipped - 3 lines]
> back to the original String...It doesn't even sound right but I don't
> have a better solution since I can't change the function signature...
It is not possible to convert general string value to long in a way
that string keeps it's content.
Bu, maybe, you could try with some kind of two way dictionary.
ArrayList longToString;
HashMap stringToLong;
....
longToString=new ArrayList();
stringToLong=new HashMap();
....
long toLong(String s)
{
Object o=stringToLong(s);
if(o == null)
{
longToString.add(s);
long lng=(long) (longToString.size() -1);
stringToLong.put(s,new Long(l));
return lng;
}
return ((Long)o).longValue();
}
.....
Be carefull of multithreaded access, you might want to make method
synchronized.
DG