[JS] UTF-8 변환 이후 BOM 관련 문제

SJ·2022년 6월 14일
0

문제

utf-8 변환으로 한글표기를 했을때 text뒤로 알 수 없는 ' '공백이 계속 붙어서 리턴되었다.

원인 분석

소스와 HTML 상으로는 특별한 문제가 없어 보이지만 log를 찍어보니 text뒤에 '\u0000'이란 text가 계속 붙어 나오는 것을 확인 할 수 있었다.

BOM(Byte order mark) 때문에 현상이 발생한다고 하는데
BOM이란 바이트 순서 표식으로 유니코드 문자 매직 넘버로서 문서의 가장 앞에 추가하여 텍스트를 읽는 프로그램에 여러 정보를 전달한다고 한다.

* 참고 사이트

링크텍스트

해결

utf convert함수

convertUTF8(word) {
	if (word !== undefined) {
    	var buf = new ArrayBuffer(word.length * 2);
        var bufview = new Uint8Array(buf);
        
        for(var i = 0, strLen = word.length; i < strLen; i++) {
        	bufView[i] = word.charCodeAt(i);
        }
        
        word = window.model.utf2Str(bufView);
        }
 
	return word;
}

utf2Str 함수

model.utf2Str = function(uint8) {
	var str = "", i = 0, len, c, c2, c3;
	len = uint8.length;
  	while (i < len) {
    	c = uint8[i++];
    	switch ( c >> 4) {
    	case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
        	//0xxxxxxx
        	str += String.fromCharCode(c);
        	break;
    	case 12: case 13:
        	// 110x xxxx  10xx xxxx
        	str += String.fromCharCode(((c & 0x1F) << 6 ) | (c2 & 0x3F));
        	break;
    	case 14:
        	// 1110 xxxx   10xx xxxx  10xx xxxx
        	c2 = uint8[i++]; c3 = uint8[i++];
        	str += String.fromCharCode(((c & 0x0F)  <<  12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
        	break;
    	}
	}


	return str;
}

해결 부분

convertUFT8(c.title).replaceAll('\u0000', '');
profile
효율적이고 효과적이게

0개의 댓글