Skip to content

Instantly share code, notes, and snippets.

@fushihara
Last active July 30, 2017 16:44
Show Gist options
  • Save fushihara/2728cad6f1d004bd549665dd51bacd15 to your computer and use it in GitHub Desktop.
Save fushihara/2728cad6f1d004bd549665dd51bacd15 to your computer and use it in GitHub Desktop.
nodejs-sjis
const fs = require('fs')
const Iconv = require('iconv').Iconv;
function loadTextFileContent(filePath,encoding){
const iconv = Iconv(encoding, 'utf-8');
const contents = iconv.convert(fs.readFileSync(filePath)).toString();
return contents;
}
function check(fileName,encoding,check){
const bin = Buffer(fs.readFileSync(fileName)).toString("hex").toUpperCase();
try{
const iconv = Iconv(encoding, 'utf-8');
const contents = iconv.convert(fs.readFileSync(fileName)).toString();
const assert = check == contents ? "YES" : "NO";
console.log(`${fileName} bin=[${bin}] content=[${contents}] assert=[${assert}] isEqual=[${check}]`);
}catch(e){
console.log(`${fileName} bin=[${bin}] error ${e}`);
}
}
function check1(encoding){
console.log(`${encoding} でチェック`);
check("./sjis-1.txt",encoding,"あ﨑い");
check("./sjis-2.txt",encoding,"あⅢい");
check("./sjis-3.txt",encoding,"あ\\い");
check("./sjis-4.txt",encoding,"あ~い");
}
check1("Shift_JIS");
check1("CP932");
/*
Shift_JIS でチェック
./sjis-1.txt bin=[82A0FAB182A2] error Error: Illegal character sequence.
./sjis-2.txt bin=[82A0875682A2] error Error: Illegal character sequence.
./sjis-3.txt bin=[82A05C82A2] content=[あ¥い] assert=[NO] isEqual=[あ\い]
./sjis-4.txt bin=[82A0816082A2] content=[あ〜い] assert=[NO] isEqual=[あ~い]
CP932 でチェック
./sjis-1.txt bin=[82A0FAB182A2] content=[あ﨑い] assert=[YES] isEqual=[あ﨑い]
./sjis-2.txt bin=[82A0875682A2] content=[あⅢい] assert=[YES] isEqual=[あⅢい]
./sjis-3.txt bin=[82A05C82A2] content=[あ\い] assert=[YES] isEqual=[あ\い]
./sjis-4.txt bin=[82A0816082A2] content=[あ〜い] assert=[NO] isEqual=[あ~い]
*/
@fushihara
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment