444,234 Members | 1,877 Online
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

# convert any string to 7 digit

 100+ P: 228 Hi, I am looking for a way to do a conversion. Please don't worry about its use or possible application...i just want to know if it is possible and if, yes, away to do it. Concept: The function will accept a string. The string could be 7 digit number or any number of all chars and alphanumeric. Now, what I need to do is encode that given value to 7 digits. But if given value is 7 digit, it will be used as it is. If is less than 7, "0" will be added to its left (if say 1234, it will become 0001234). If it is longer than 7 digit, I want to convert it to 7 digit somehow then use it. Here is my approach so far: Expand|Select|Wrap|Line Numbers        Public Function nationalIDPassportToCompanyID(ByVal intintegergiven As Integer, ByVal strstringgiven As String) As String           Dim reply As String = ""           Dim strid As String = intintegergiven                   Dim i As Integer           Dim intlen As Integer = strid.Length           If intintegergiven > 0 Then 'integer id given. so use it               strid = addACharacterToString(True, strid, "0", 7) 'this function pads 0 as needed                           Else 'string given so encrpty it first convert the characters to 7 digits then use the above to convert it to numbers               'first convert any letters to numbers               intlen = strstringgiven.Length               Dim strchar As String               Dim intchar As Integer               Dim strallnum As String = ""               For i = 0 To intlen - 1                   strchar = strstringgiven.Substring(i, 1)                   intchar = AscW(strchar)                   If intchar < 48 Or intchar > 57 Then 'not a number. so replace it with its numerical value                       strallnum = strallnum & intchar                       Else 'number. so use it as it is.                       strallnum = strallnum & strchar                   End If                       Next                   'now all is numbere here. convert it to 7 digits somehow now    Select Case strallnum.Length                   Case Is < 7 'the convert number is not 7 digits so pad 0 to its left                       strallnum = addACharacterToString(True, strallnum, "0", 7)                   Case Is > 7 'convert the digit to 7 HERE NOW.                   End Select               End If       REPLY=STRALLNUM               Return reply       End Function          == Callers that call the function [e.g. button] know if to send number or string. == What I need is a lot like sha1 or md5 functions of PHP which convert any given input to a fixed length characters. The output is always string in my case as well. Anyone have a hint on how to do this? A sample to look for? Apr 15 '13 #1
4 Replies

 Expert Mod 10K+ P: 12,383 If it's 7 or less, prepend 7 zeroes and grab the right 7. For more than 7, you need to define what algorithm you want to use for that. You just need to be aware that truncating data will allow for the possibilities of collisions, i.e. two different inputs that result in the same output. Meaning the result is no longer unique. Which can be a problem if it needs to be unique. Otherwise, a simple way to truncate it is to just take the left for right 7 digits. Apr 15 '13 #2

 100+ P: 228 Yes, I figured that out but I just cant get around the problem and solve it. I thot of some logics but they all run the risk of repeating a value. Any idea? Apr 16 '13 #3

 Expert Mod 10K+ P: 12,383 With your requirements, it's probably impossible to truncate data and not repeat values. You can compress data with a losesess algorithm. But compression can't guarantee a specific length. You can try changing from base 10 to a higher base such as base 16 or more. But whether or not that works depends on if you want to allow for characters that are not digits and what your maximum value is. Apr 16 '13 #4

 100+ P: 296 "any number of characters down to 7 digits" Well there are many ways this can be done - but if you need something unique each time then you're out of luck. If the input is a variable number of characters then the output will also have to be a variable number of characters. Even if the input is fixed - if it is too large then uniqueness is not possible. This sounds like you've been assigned a kobayashi maru heuristic... Apr 17 '13 #5