« 今週のお買い物 (2007.01.07~01.13) | トップページ | ブラウザのproxy情報を自作アプリで利用する方法 »
2007.01.15
[converter] ISBN-10 -> ISBN-13 / ISBN-13 -> ISBN-10
「ISBN-10 を ISBN-13 に変換する処理」と「ISBN-13 を ISBN-10 に変換する処理」を C で書いてみました。やっつけで書いてる(static char とか使ってる)ので、再入可能じゃないといけないとか、競合する可能性のある複数のスレッドから同時に呼び出す可能性があるとか、そういった使い方をする場合は、それなりに書き直して使ってください。
「ISBN - Wikipedia」を参考にしつつ、実装しました。あと、「ISBN-13 ⇔ ISBN-10相互変換 - ただのにっき (2007-01-11)」を見て、抜けていた処理を追加したり(実装当初、ISBN-10 で check digit が 11 になったときのこととか、ISBN-13 で check digit が 10 になったときのこととかを失念してました)。
それでは、以下、ソースです。
/*
* convISBN.c - convert ISBN-10 to ISBN-13 / ISBN-10 to ISBN-13
* written by H.Tsujimura 15 Jan 2007
* Copyright (c) 2007 by H.Tsujimura <tsupo@na.rim.or.jp>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* History:
* $Log: /makeRSS.root/makeRSS/convISBN/convISBN.c $
*
* 3 07/01/15 2:18 tsupo
* bug fix
*
* 2 07/01/15 1:57 tsupo
* added 'invalid string checking'
*
* 1 07/01/15 1:32 tsupo
* Initial Revision
*/
#include <stdio.h>
#ifndef lint
static char *rcs_id =
"$Header: /makeRSS.root/makeRSS/convISBN/convISBN.c 3 07/01/15 2:18 tsupo $";
#endif
char *
convISBN10to13( const char *isbn10 )
{
static char isbn13[16];
unsigned long checkDigit = 0;
const char *p = isbn10;
char *q = isbn13;
int i;
*q = '9';
checkDigit += (*q++ - '0') * 1;
*q = '7';
checkDigit += (*q++ - '0') * 3;
*q = '8';
checkDigit += (*q++ - '0') * 1;
for ( i = 0; i < 9; i++ ) {
if ( !(*p) || (*p < '0') || (*p > '9') ) {
if ( *p == '-' ) {
p++;
i--;
continue;
}
fputs( "convISBN10to13(): invalid source string\n",
stderr );
return ( NULL );
}
if ( i % 2 == 0 )
checkDigit += (*p - '0') * 3;
else
checkDigit += (*p - '0') * 1;
*q++ = *p++;
}
checkDigit = (10 - (checkDigit % 10)) % 10;
*q++ = (char)(checkDigit + '0');
*q = '\0';
return ( isbn13 );
}
char *
convISBN13to10( const char *isbn13 )
{
static char isbn10[16];
unsigned long checkDigit = 0;
const char *p = isbn13;
char *q = isbn10;
int i;
p += 3;
for ( i = 10; i > 1; i-- ) {
if ( !(*p) || (*p < '0') || (*p > '9') ) {
if ( *p == '-' ) {
p++;
i++;
continue;
}
fputs( "convISBN13to10(): invalid source string\n",
stderr );
return ( NULL );
}
checkDigit += (*p - '0') * i;
*q++ = *p++;
}
checkDigit = (11 - (checkDigit % 11)) % 11;
*q++ = checkDigit == 10 ? 'X' : (char)(checkDigit + '0');
*q = '\0';
return ( isbn10 );
}
#ifdef TEST
int
main( int argc, char *argv[] )
{
const char *p = "406340613X";
const char *q = "9784063406139";
printf( "ISBN10: %s -> ISBN13: %s\n", p, convISBN10to13( p ) );
printf( "ISBN13: %s -> ISBN10: %s\n", q, convISBN13to10( q ) );
p = "4757519281";
q = "9784757519282";
printf( "ISBN10: %s -> ISBN13: %s\n", p, convISBN10to13( p ) );
printf( "ISBN13: %s -> ISBN10: %s\n", q, convISBN13to10( q ) );
return ( 0 );
}
#endif /* TEST */
投稿者: tsupo 2007.01.15 午前 02:12
| 固定リンク
|
|
| ![]()
|
|
アマゾンわくわく探検隊
トラックバック
この記事のトラックバックURL:
この記事へのトラックバック一覧です: [converter] ISBN-10 -> ISBN-13 / ISBN-13 -> ISBN-10:



