#looking for a reversible string-to-number conversion and vice versa in java

32 messages · Page 1 of 1 (latest)

spice charm
#

1st method, input string (example "test"), output number
2nd method, input number from 1st method and output string, in this example will output "test"

it should include character ";" and shouldnt return long variables

idk if anyone has ever done this or knows how, i'd be very grateful if someone could give me an idea for how to at least

its fine if it can do it for strings up to 12 characters or similar

lean baneBOT
#

This post has been reserved for your question.

Hey @spice charm! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mortal plume
#

wht will test give as output?

#

binary number?

spice charm
#

doesnt matter

#

i just want a way to store a string as a number

#

i tried making each character be 2 digits of the number (using a-z + ; as chars) but then the number is too long very fast

mortal plume
#

hm

#

any other way u thinked of?

spice charm
#

no lol

#

like maybe multiplication stuff

#

but im too tired to think

#

thats why im seeking help here

alpine night
#

Are you seeking a bijection?

spice charm
alpine night
#

A unique mapping between all strings < 12 characters and some set of numbers

#

A really poor performing solution to this would be to use the fundamental thereom of arithmetic

#

convert each character in the string to an integer value, using the index of each character in the string you find the prime base 0->2 , 1->3 , 2 ->5 ... then you take that base to the power of the converted character and take the product of all these values. That would give you a unique number that represents that string.

The inverse would be finding the prime factorization (constrained to the first 12 primes).
The exponents in the factorization would map back to the character values in the string.

spice charm
alpine night
#

yes

#

hmm, can't you just use base64 encoding?

#

or do you need access to all characters?

spice charm
#

or actually

#

just a-z is fine

alpine night
#

then yes you can use base64 encoding/decoding with a caveat. ; is not valid base64. Convert all occurences of ; to / (a valid base64 character)

#

then encode string to base64

#

then base64 to decimal

mellow forge
#

Or, you know, base 26

#

But the numbers will still become huge. What the hell do you expect?