String to Long and then to String

U

usgog

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

Can I use:
long test = Long.valueof("This is a test");
String string = Long.valueof(test).toString();

Now string is: "This is a test"? If not, any better way to do it?
 
L

Luc The Perverse

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

Can I use:
long test = Long.valueof("This is a test");
String string = Long.valueof(test).toString();

Now string is: "This is a test"? If not, any better way to do it?

Explain "convert to a long" and we could probably help you.

valueof function does not convert strings to numbers. IT does convert
decimal representation to a computer number.

"This is a test" assuming a 27 letter alphabet has a high digit of 27^13
giving you an effective range of 0 - 109418989131512359208 (27^14 - 1).
You will notice that this exceeds the holding capacity of a long.

First . . WHY are you converting a string to a long. And if you know
why, then HOW do you want it converted (it's ok if you don't know the how
part, as long as you can get the why part we can probably help you)?
 
U

usgog

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

lordy

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
String.
Why?

Can I use:
long test = Long.valueof("This is a test");
String string = Long.valueof(test).toString();

Now string is: "This is a test"? If not, any better way to do it?

Use a counter and some kind of lookup structure you can store strings in.
Increment the counter with each new string and store counter,string pair.
This may or may not meet other requirements.

Lordy
 
F

Frank van Schie

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

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

Jeffrey Schwab

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?
 
R

Rhino

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.

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

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

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

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
 

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
474,265
Messages
2,571,071
Members
48,771
Latest member
ElysaD

Latest Threads

Top