Skip to content

Instantly share code, notes, and snippets.

@sugarflower
Last active July 2, 2019 16:26
Show Gist options
  • Save sugarflower/601d9cc65f18a66ee25719c04fcc9bee to your computer and use it in GitHub Desktop.
Save sugarflower/601d9cc65f18a66ee25719c04fcc9bee to your computer and use it in GitHub Desktop.

Pythonで区点コード(JIS)を得る。

文字をEUC-JPへエンコーディングし、バイト列からそれぞれ0xa0、0x02を引いたものがJISになる。

str = "あ"
code = str.encoding("euc-jp")
ku  = code[0] - 0xa0
ten = code[1] - 0x02

余談

JavaScriptでsjisからjisを求めるにはこうする。

uc8はuint8_tとして値を修正するファンクション(割愛)

function s2c( val ){
	hb = (val & 0xff00) >> 8;
	lb = val & 0x00ff;
	if( hb <= 0x9f ){
		hb = uc8(hb, -113);
	} else {
		hb = uc8(hb, -177);
	}
	hb = hb * 2 + 1;
	if(lb>0x7f) {
		lb = uc8(lb, -1);
	}
	if(lb>=0x9e) {
		lb = uc8(lb, -125);
		hb = uc8(hb, 1);
	} else {
		lb = uc8(lb, -31);
	}
	hb = uc8(hb, -32);
	lb = uc8(lb, -32);
	return hb*100 + lb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment