Hello,
I need to convert strings (in my case countries) into unique code number.
I tried the function CharToInt, but it gives me identical numbers for different countries (for example 65 for australia, azerbaidjan, austria, argentina etc.).
Can someone explain me why ? Is there a solution to convert a string into a unique code ?
Thank you
Solved! Go to Solution.
Would you consider using ISO country codes? You would first standardize the spelling of each country and then use a FIND & REPLACE Tool (you can use a join, but I think that the F&R tool is a better choice) to convert the countries to codes.
https://en.wikipedia.org/wiki/ISO_3166-1
CharToInt is returning the first character of the string's ASCII code.
While not guarenteed to be unique, you could use the MD5_ASCII function to get the hashcode of the string which may be unique!
CharToInt only looks at the first character of a string in its conversion. All strings beginning with the same first character will be converted to the same integer.
May I suggest a join to accomplish this? This can codify your countries based on a list from an outside source or based on one that you provide.
Thank you, I didn't think of ISO code!
Thank you for the explanation !
I tried the MD-ASCII function, and it returns a code but with letters also!
So I guess there is no function to convert directly a string to a unique code number...
As a bonus, here is a formula:
tonumber(tostring(chartoint(left(soundex([String]),1)))+right(soundex([String]),3))
It will convert the string into a soundex key (e.g. Canada becomes C520) and then convert the C to 67 and will then put the 67 in front of the 520. 67520 is the result.
Great! That will help me a lot to convert other kind of string datas !
Thank you :)
If you want the MD5 Hash as a number:
Hextonumber(md5_ascii([Field1]))
One warning about Mark's soundex approach, if the beginning of the string is too similar, then the numbered code will not be unique. Dominica and Dominican Republic would both be 71552 and United States and United Kingdom would both be 85532.