Converting a string to an uint

01/04/2012 21:14 Kiyono#1
The authresponse packet sends 2 values that can be practically anything and the 1052 packet uses those values for decryption reasons (5017). Now I want to send the account ID with the authresponse packet, the issue here is that the authresponse packet requires an uint and the account ID is obviously a string so is there a way to convert the string to an uint and back again when needed?
When googling I came across a slightly roundabout way that didn't really work as intended, it was along the lines of string -> byte array -> uint but when converting back, it came out a bit... weird, the string ended up being halved.

//edit Posted this in the wrong section, should be moved to the pserver section or programming section.
01/04/2012 22:55 Lateralus#2
The account ID is also an unsigned 4 byte value, not a string. With that being said, you can convert a string to any type by using the Parse or TryParse methods for that type.
01/04/2012 23:26 CptSky#3
Quote:
Originally Posted by Kiyono View Post
The authresponse packet sends 2 values that can be practically anything and the 1052 packet uses those values for decryption reasons (5017). Now I want to send the account ID with the authresponse packet, the issue here is that the authresponse packet requires an uint and the account ID is obviously a string so is there a way to convert the string to an uint and back again when needed?
When googling I came across a slightly roundabout way that didn't really work as intended, it was along the lines of string -> byte array -> uint but when converting back, it came out a bit... weird, the string ended up being halved.

//edit Posted this in the wrong section, should be moved to the pserver section or programming section.
As Lateralus said, the Account ID is really an integer. It must be an unique ID generated at the account creation and linked to the account name. Anyway, if your account ID is a string because you consider the account name as the ID, there is three solutions.

The first one. An (U)Int32 is constructed of 4 octets. An ASCII string is formed by an array of octet. So, if your string is 1-4 characters, you'll be able to take their ASCII code an construct an (U)Int32. But it isn't realistic...

The second one. If all your account name are numbers, you'll be able to parse them to an (U)Int32. But it isn't realistic...

As you see, there isn't any reversible way to convert a string to an integer. You'll have to go with an hash function. The FNV-1(a) hash can generate a 32 bits hash, so it'll fit in an (U)Int32. The GetHashCode() function will also generate a 32 bits hash, but I'm not sure of the dispersion... It may be really bad. Anyway, when checking for the account, you'll have to hash every account in the database for finding the right one. It's not a really great solution.

The best solution is to go with the real AccID as it should be. The AccID is already an integer if you implemented it right.
01/04/2012 23:50 Kiyono#4
Oh, I considered the account ID and account name to be the same thing and I was actually referring to the account name which is [I think] also the account ID in the Project Manifest source. So how would I check to see if the account ID is implemented right? As I honestly don't know where to look.
01/05/2012 01:06 Lateralus#5
Quote:
Originally Posted by Kiyono View Post
Oh, I considered the account ID and account name to be the same thing and I was actually referring to the account name which is [I think] also the account ID in the Project Manifest source. So how would I check to see if the account ID is implemented right? As I honestly don't know where to look.
The account ID can't be the same as the account name, as the two are different types. I really don't understand what you're trying to do...
01/05/2012 11:58 Kiyono#6
Guess I'll need to clarify it about then.
I want to send the account name in the authresponse packet as one of the keys (authserver) and want to be able to convert it back to string in the gameserver but it seems to be... well not possible.
01/05/2012 12:09 Korvacs#7
Well its not that its not possible.

You would need to create a unique number based on the string by way of a method, i guess something like a checksum could work.

The problem is that you need the number to be unique, otherwise you will get issues when you get to the game server, and doing that with 4 bytes, or even 8 if you use both fields would be difficult.