function value = vin_ch_to_num ( ch )

%*****************************************************************************80
%
%% vin_ch_to_num() converts a VIN character to a numeric value.
%
%  Discussion:
%
%    Assumes all characters are digits 0-9 or capital letters (A-Z) 
%    but excluding 'I', 'O' and 'Q'.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    23 May 2026
%
%  Input:
%
%    character CH: the character to be converted.
%    CH should be a character representing a digit '0' through '9',
%    or an upper case letter 'A' through 'Z', omitting 'I', 'O', and 'Q'.
%
%  Output:
%
%    integer VALUE: the corresponding VIN value.
%

%
%  Convert to uppercase.
%
  ch = upper ( ch );
%
%  Lookup the ASCII code for the letter.
%  '0' is 48,
%  'A' is 65.
%
  n = double ( ch );
%
%  '0' through '9':
%  '0'  '1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'  '9'
%   0    1    2    3    4    5    6    7    8    9
%
  if ( double ( '0' ) <= n && n <= double ( '9' ) )
    value = n - double ( '0' );
%
%  'A' through 'H' (skip 'I')
%  'A'  'B'  'C'  'D'  'E'  'F'  'G'  'H'  'I'
%   1    2    3    4    5    6    7    8    *
%
  elseif ( double ( 'A' ) <= n && n <= double ( 'H' ) )
    value = n - double ( 'A' ) + 1;
%
%  'J' through 'R' (skip 'O' and 'Q')
%  'J'  'K'  'L'  'M'  'N'  'O'  'P'  'Q'  'R'
%   1    2    3    4    5    *    7    *    9 
%
  elseif ( double ( 'J' ) <= n && n <= double ( 'N' ) )
    value = n - double ( 'J' ) + 1 ;

  elseif ( double ( 'P' ) == n )
    value = n - double ( 'J' ) + 1 ;  

  elseif ( double ( 'R' ) == n )
    value = n - double ( 'J' ) + 1 ;     
%
%  'S' through 'Z', and start at 2!
%  'S'  'T'  'U'  'V'  'W'  'X'  'Y'  'Z'
%   2    3    4    5    6    7    8    9 
%
  elseif ( double ( 'S' ) <= n && n <= double ( 'Z' ) )
    value = n - double ( 'S' ) + 2;
%
%  Character is not legal!
%
  else
    value = nan;
  end

  return
end

