encode string help

Welcome Forums General PowerShell Q&A encode string help

Viewing 3 reply threads
  • Author
    Posts
    • #216246
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      Hi all,

      Hope you’re all fine and safe at home 😉

      I subscribe here because I need help.

      I need to encode a string like :

      AZERTY123456789012345 to AZERTY1234xxxxxxx2345

      So I need keep their 4 first and their 4 last digits, others betwen need to be replace by “x”

      I try to find a way to tell : in the string, count between 4th first to 4th last and replace it.

      I’ve this run but is it possible to do it by an other way, more efficient and adaptable (like 16-18 digits string) ? 

      $DTR = “AZERTY123456789012345” #My string need to be encode
      $CODE = (“$DTR”).Substring(10,7) #Select between 4first and 4last digits in sub var
      $CODE2 = “$CODE” -replace “\d”,”x” # replace digits by “X” in the sub var
      $CNUMB = (“$DTR”).Replace(“$CODE”, “$CODE2”) # replace the sub var digits by sub var “x”

      Thank you four your help,

      Mika.

       

    • #216249
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Nothing really sophisticated or elegant or flexible but working. Is it always the same structure of the string or does it vary?

      'AZERTY123456789012345' -replace '(?<=AZERTY\d{4})\d+(?=\d{4})',('X'*('AZERTY123456789012345'.Length -14))
      • #216279
        Participant
        Topics: 1
        Replies: 2
        Points: 15
        Rank: Member

        Hi Olaf,

        thanks for your answers 🙂

        the structure of string could vary (some caracters or not before digits) and numbers can count 15 or 16 or 18.

        Mika.

         

    • #216273
      Participant
      Topics: 12
      Replies: 524
      Points: 1,220
      Helping Hand
      Rank: Community Hero
      function Encode-String {
      
          [CmdletBinding(ConfirmImpact='Low')] 
          Param(
              [Parameter(Mandatory=$false)][String]$DTR = 'AZERTY123456789012345',
              [Parameter(Mandatory=$false)][Int]$CountOfPrefixDigitsToKeep = 4,
              [Parameter(Mandatory=$false)][Int]$CountOfSuffixDigitsToKeep = 4,
              [Parameter(Mandatory=$false)][Char]$FillerCharacter = 'X'
          )
      
          Begin { }
      
          Process {
      
              # Identify Numerical/non numerical characters
              $NonNumericalPart = ([Char[]]$DTR | foreach { if ([Int]$_ -le 47 -or  [Int]$_ -ge 58) {$_} }) -join '' 
              $NumericalPart    = ([Char[]]$DTR | foreach { if ([Int]$_ -ge 48 -and [Int]$_ -le 57) {$_} }) -join '' 
              Write-Verbose "Identified Non-numerical part as '$NonNumericalPart', and Numerical part '$NumericalPart'"
      
              # Concatenate and output the desired string
              $NonNumericalPart +
              $NumericalPart.Substring(0,$CountOfPrefixDigitsToKeep) + 
              [String]$FillerCharacter*($NumericalPart.Length-($CountOfPrefixDigitsToKeep+$CountOfSuffixDigitsToKeep)) + 
              $NumericalPart.Substring(($NumericalPart.Length-$CountOfSuffixDigitsToKeep),$CountOfSuffixDigitsToKeep)
          } 
      
          End { }
      }
      

      This will work for different size strings like those containing 15,16,17,18 digits:

      Encode-String 'AZERTY1234567890123456' -Verbose # 16
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '1234567890123456'
      AZERTY1234XXXXXXXX3456
      

      It can handle different count of digits in the beginning (Prefix) as in:

      Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 6 -Verbose 
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
      AZERTY123456XXXXX2345
      

      or the end (Suffix) as in:

      Encode-String -DTR 'AZERTY123456789012345' -CountOfSuffixDigitsToKeep 7 -Verbose 
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
      AZERTY1234XXXX9012345
      

      Notice that this function does not care where the digits are located in the string. For example, ‘AZERTY123456789012345’ and ‘1A2Z3E4R5T6Y789012345’ will produce the same output:

      Encode-String 'AZERTY123456789012345' -Verbose 
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
      AZERTY1234XXXXXXX2345
      
      Encode-String '1A2Z3E4R5T6Y789012345' -Verbose 
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
      AZERTY1234XXXXXXX2345
      

      Finally, this function does not do any error checking. In a production environment you may want to anticipate all manners of bad input, like

      Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 16 -Verbose 
      VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
      Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
      Parameter name: length"
      At line:21 char:9
      +         $NonNumericalPart +
      +         ~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ArgumentOutOfRangeException
      
      • This reply was modified 1 month, 3 weeks ago by Sam Boutros.
    • #216282
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      Hi Sam,

      wooh it’s so much than I need, it’s amazing 😉

      Thanks a lot both of us for your help.

       

      Mika

Viewing 3 reply threads
  • You must be logged in to reply to this topic.