Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

String to Long and then to String

Thread view: 
usgog@yahoo.com - 08 Jul 2006 05:30 GMT
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?
Luc The Perverse - 08 Jul 2006 06:15 GMT
>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 - 5 lines]
>
> 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)?

--
LTP

:)
usgog@yahoo.com - 08 Jul 2006 06:27 GMT
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
lordy - 08 Jul 2006 06:28 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.