CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2005
    Posts
    52

    Request an algorithm, convert a string to a unique int

    I need to convert a string to a unique integer, and better it's thread safe. Is that possible? How to?

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    Re: Request an algorithm, convert a string to a unique int

    I assume you mean convert a string to an int?
    well there are multiple ways http://www.codeguru.com/forum/showthread.php?t=231054
    I don't know how thread safe those are. If you want a thread safe conversion, just build one. it isn't hard, if it doesn't have floating precision.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  3. #3
    Join Date
    Dec 2005
    Posts
    52

    Re: Request an algorithm, convert a string to a unique int

    thanks, but I didn't think that work for my case.

    I mean "unique", that means the resulting integer should be unique from different strings, even from different character sequence.
    I know it may be impossible, since length of string is limitless, however, integer gets maximum value.

  4. #4
    Join Date
    Jun 2008
    Posts
    592

    Re: Request an algorithm, convert a string to a unique int

    I don't understand what you mean by unique. Maybe you can explain more or show an example in Pseudocode.

    someone else might know what you're asking
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  5. #5
    Join Date
    Dec 2005
    Posts
    52

    Re: Request an algorithm, convert a string to a unique int

    Quote Originally Posted by Joeman View Post
    I don't understand what you mean by unique. Maybe you can explain more or show an example in Pseudocode.

    someone else might know what you're asking
    I mean that convert a string to an int, which would be unique, different from other ints generated by different strings

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Request an algorithm, convert a string to a unique int

    Quote Originally Posted by LifeIsSuffering View Post
    thanks, but I didn't think that work for my case.

    I mean "unique", that means the resulting integer should be unique from different strings, even from different character sequence.
    I know it may be impossible, since length of string is limitless, however, integer gets maximum value.
    Maybe if you tell us what you're trying to really achieve by doing this, then maybe you will get other alternatives from persons here. What is your real goal here?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2010
    Posts
    8

    Re: Request an algorithm, convert a string to a unique int

    It is impossible have a unique key for all strings.

    Just sum all the values of string's elements (as char), or multiply, or 1st + 2nd*10 + 3rd*100 + 4th*1000... (I mean first second third as the strings first char second char when you think them as a char array. Of course starting with zero)

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Request an algorithm, convert a string to a unique int

    Are you looking to convert the string "12345" into the integer 12345, or are you simply trying to come up with an arbitrary integer for the strong "ABC" (eg, a hash function)?

    If the latter, consider: There are at most 2^32-1 possibly ints (unless you use a BigInt library). There are more possible strings (bounded only by the memory of the machine, really). So by the pigeonhole principal, it's impossible to assign a different integer to each string.

    However, you can design a function which assigns distinct integers to distinct strings with high probability, which is what you'd do when designing a hash function, for instance.

  9. #9
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Request an algorithm, convert a string to a unique int

    What you are looking for is a hash functions. There are cryptographic hash functions which have a very high probability that a string is unique (very high meaning e.g. 2^80 in the case of SHA-1). Either use one of the common hash functions (e.g. SHA-1, MD5) or google for hash algorithms, there are plenty of simpler ones.
    Last edited by treuss; January 9th, 2010 at 04:31 AM.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  10. #10
    Join Date
    Dec 2005
    Posts
    52

    Re: Request an algorithm, convert a string to a unique int

    thanks Lindley and treuss, a hash algorithm may be what I'm looking for...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured