By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
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
  1.  
  2.  
  3.  
  4.  Public Function nationalIDPassportToCompanyID(ByVal intintegergiven As Integer, ByVal strstringgiven As String) As String
  5.  
  6.         Dim reply As String = ""
  7.  
  8.         Dim strid As String = intintegergiven
  9.  
  10.  
  11.  
  12.  
  13.  
  14.         Dim i As Integer
  15.  
  16.         Dim intlen As Integer = strid.Length
  17.  
  18.         If intintegergiven > 0 Then 'integer id given. so use it
  19.  
  20.             strid = addACharacterToString(True, strid, "0", 7) 'this function pads 0 as needed
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.         Else 'string given so encrpty it first convert the characters to 7 digits then use the above to convert it to numbers
  31.  
  32.             'first convert any letters to numbers
  33.  
  34.             intlen = strstringgiven.Length
  35.  
  36.             Dim strchar As String
  37.  
  38.             Dim intchar As Integer
  39.  
  40.             Dim strallnum As String = ""
  41.  
  42.             For i = 0 To intlen - 1
  43.  
  44.                 strchar = strstringgiven.Substring(i, 1)
  45.  
  46.                 intchar = AscW(strchar)
  47.  
  48.                 If intchar < 48 Or intchar > 57 Then 'not a number. so replace it with its numerical value
  49.  
  50.                     strallnum = strallnum & intchar
  51.  
  52.  
  53.  
  54.                 Else 'number. so use it as it is.
  55.  
  56.                     strallnum = strallnum & strchar
  57.  
  58.                 End If
  59.  
  60.  
  61.  
  62.  
  63.  
  64.             Next
  65.  
  66.  
  67.  
  68.             'now all is numbere here. convert it to 7 digits somehow now
  69.  
  70.  Select Case strallnum.Length
  71.  
  72.                 Case Is < 7 'the convert number is not 7 digits so pad 0 to its left
  73.  
  74.                     strallnum = addACharacterToString(True, strallnum, "0", 7)
  75.  
  76.                 Case Is > 7 'convert the digit to 7 HERE NOW.
  77.  
  78.  
  79.  
  80.             End Select
  81.  
  82.  
  83.  
  84.         End If
  85.  
  86.  
  87.  
  88. REPLY=STRALLNUM
  89.  
  90.  
  91.  
  92.         Return reply
  93.  
  94.     End Function 
  95.  
  96.  
  97.  
  98.  


== 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
Share this Question
Share on Google+
4 Replies


Rabbit
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

Rabbit
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

!NoItAll
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

Post your reply

Sign in to post your reply or Sign up for a free account.