Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: String to Long

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default String to Long

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

    Java 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:
    Java 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?
    Last edited by PhQ; 01-04-2013 at 04:19 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

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

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: String to Long

    Quote Originally Posted by pbrockway2 View Post
    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)
    Java 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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: String to Long

    Th easiest way would be to pass the strings as strings. Reading from and Writing to a URLConnection (The Java™ Tutorials > Custom Networking > Working with URLs)

    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... :)
    PhQ likes this.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: String to Long

    Quote Originally Posted by pbrockway2 View Post
    Th easiest way would be to pass the strings as strings. Reading from and Writing to a URLConnection (The Java™ Tutorials > Custom Networking > Working with URLs)

    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... :)
    yyyeeaaaa..... I'll think I'll ignore encryption for now.
    Thanks for the link too.
    Last edited by PhQ; 01-04-2013 at 04:28 PM.

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2012, 01:10 PM
  2. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 08:19 AM
  3. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 04:44 PM
  4. Replies: 4
    Last Post: 12-31-2009, 11:05 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •