0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JANコードからのEANコード文字列変換

Last updated at Posted at 2014-01-10

とあるソースを引用してJANコードをEANコード文字列に変換するものをVB.NETで作った。
その関数を残しておく。そもそも良くコード体系を理解しておらず、ほぼもとのソースを
VBに置き換えただけなのでどうもいけていない。

TODO 時間をみてちゃんと理解し作り直していきたい。

---------------------------------- 以下 ソース ------------------------------------------

        ' JANコードからバーコード生成用文字列(EAN)への変換処理
        Public Shared Function JanToEan(ByVal jan As String) As String
            '戻り値
            Dim result As String = String.Empty


            '対象文字列が数値以外ならここで抜ける
            For i As Integer = 0 To jan.Length - 1
                '文字コードチェック
                Dim ascNum = Asc(jan.Substring(i, 1))
                If ascNum < 48 Or ascNum > 57 Then
                    Return result
                End If
            Next

            '>>* 受け取った文字列長さを元に、処理を分ける
            If jan.Length = 8 Then
                '取得文字列の長さが8文字の場合

                '--コード変換処理(ascii文字のA~Jに変換)
                result = ":"
                For i As Integer = 0 To 3
                    result &= Chr(65 + CInt(jan.Substring(i, 1)))
                Next

                result &= "*"

                'ここからは(Ascii文字のa~jに変換)
                For i As Integer = 4 To 7
                    result &= Chr(97 + CInt(jan.Substring(i, 1)))
                Next

                result &= "+"


            ElseIf jan.Length = 13 Then
                '取得文字列の長さが13文字の場合

                'コード変換処理(ascii文字のA~Jに変換)
                result = jan.Substring(0, 1) & Chr(65 + CInt(jan.Substring(1, 1)))


                Dim first = CInt(jan.Substring(0, 1))

                For i As Integer = 2 To 6
                    Dim flg = False

                    Select Case i
                        Case 2
                            Select Case first
                                Case 0, 1, 2, 3
                                    flg = True
                                Case Else
                                    flg = False
                            End Select
                        Case 3
                            Select Case first
                                Case 0, 4, 7, 8
                                    flg = True
                                Case Else
                                    flg = False
                            End Select
                        Case 4
                            Select Case first
                                Case 0, 1, 4, 5, 9
                                    flg = True
                                Case Else
                                    flg = False
                            End Select
                        Case 5
                            Select Case first
                                Case 0, 2, 5, 6, 7
                                    flg = True
                                Case Else
                                    flg = False
                            End Select
                        Case 6
                            Select Case first
                                Case 0, 3, 6, 8, 9
                                    flg = True
                                Case Else
                                    flg = False
                            End Select
                        Case Else
                            flg = False
                    End Select

                    If flg Then
                        'flgがOnなら(ascii文字のA~Jに変換)
                        result &= Chr(65 + CInt(jan.Substring(i, 1)))
                    Else
                        'flgがOffなら(ascii文字のK~Tに変換)
                        result &= Chr(75 + CInt(jan.Substring(i, 1)))
                    End If
                Next

                result &= "*"
                'ここからは(Ascii文字のa~jに変換)
                For i As Integer = 7 To 12
                    result &= Chr(97 + CInt(jan.Substring(i, 1)))
                Next
                result &= "+"
            End If

            Return result

        End Function
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?