Last active
July 30, 2017 16:44
-
-
Save fushihara/2728cad6f1d004bd549665dd51bacd15 to your computer and use it in GitHub Desktop.
nodejs-sjis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
あ﨑い |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
あⅢい |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
あ\い |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
あ~い |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=[あ~い] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/Fushihara/status/891699476334731264 で使うためのコード