I am trying to convert Strings to Longs, but it seems not to be working for me.
Here is how I do it:

Code:

String username = "this is a long username";
String password = "this is a long password!";
long user = Long.parseLong(username, Character.MAX_RADIX);
long pass = Long.parseLong(password, Character.MAX_RADIX);

and I get this:

Code:

Exception in thread "org.deividasg.rpg.Game_GameLoop" java.lang.NumberFormatException: For input string: "this is a long username"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at org.deividasg.rpg.net.packets.Packet00Login.sendPakcet(Packet00Login.java:21)
at org.deividasg.rpg.net.GameClient.login(GameClient.java:46)
at org.deividasg.rpg.Game.init(Game.java:88)
at org.deividasg.rpg.Game.run(Game.java:102)
at java.lang.Thread.run(Unknown Source)

When I use short Strings, it works fine. For example if I use "test" as the username it prints out 1372205.
What am I doing wrong? Is there a better way to convert Strings to longs?

01-04-2013, 05:24 AM

pbrockway2

Re: String to Long

I would imagine that the number could be too large for a long. You could break the string up into smaller chunks, however you will have to use BigDecimal rather than long as the numbers are going to get very big. Another problem is that you should check that space and ! are valid characters for that radix.

Perhaps there could be a better way of doing whatever it is you are doing.

01-04-2013, 05:33 AM

PhQ

Re: String to Long

Quote:

Originally Posted by pbrockway2

I would imagine that the number could be too large for a long. You could break the string up into smaller chunks, however you will have to use BigDecimal rather than long as the numbers are going to get very big. Another problem is that you should check that space and ! are valid characters for that radix.

Perhaps there could be a better way of doing whatever it is you are doing.

Well, I am trying to send a packet to the server containing the username and password details and I thought that converting the Strings to longs would be the easiest solution.
I am sending these bytes to the server: (00 is the packet ID which is login)

Code:

("00" + user + " " + pass).getBytes()

but now that I think about it, I should include the length of the username and password, and then I should use substring to parse the packet.
This is the best idea that came to my head right now... Is there any better ways to send strings of data? Or will this work just fine?

Also while we are on topic of Strings as numbers, whats the best way to encrypt packets which contain sensitive information such as passwords? I have found some examples but they look long and complicated.

Encryption *is* difficult. As everyone recommends: don't implement it yourself, use a standard library. (Java includes a crypto library). But it's still difficult to understand. What are you trying to protect against? Someone reading the data en route? Someone altering the data? Either happening in the reverse direction? There has to be some shared secret between client and server for authenticated encryption to take place, and that secret has to be established across a channel that is, intrinsically, insecure. About this time I'd be thinking "sod it, I'll send the data in the clear", especially as I see "Game" in the stack trace. But it's your code... :)

Encryption *is* difficult. As everyone recommends: don't implement it yourself, use a standard library. (Java includes a crypto library). But it's still difficult to understand. What are you trying to protect against? Someone reading the data en route? Someone altering the data? Either happening in the reverse direction? There has to be some shared secret between client and server for authenticated encryption to take place, and that secret has to be established across a channel that is, intrinsically, insecure. About this time I'd be thinking "sod it, I'll send the data in the clear", especially as I see "Game" in the stack trace. But it's your code... :)