javascript에서 utf-8 문자열을 iconv() PHP 함수처럼 utf-16LE로 변환해야합니다.UTF-8에서 UTF-16LE 자바 스크립트
즉 :
iconv("UTF-8", "UTF-16LE", $string);
출력이 같아야 :
49 00 (e) 00 64 00 65 00 78 00
는 I 디코딩이 FUNC 발견 UTF- 16LE 그리고 잘 작동하지만 인코딩하는 방법을 모르겠습니다.
function decodeUTF16LE(binaryStr) {
var cp = [];
for(var i = 0; i < binaryStr.length; i+=2) {
cp.push(
binaryStr.charCodeAt(i) |
(binaryStr.charCodeAt(i+1) << 8)
);
}
return String.fromCharCode.apply(String, cp);
}
결론은 다운로드 할 수있는 이진 파일을 만드는 것입니다.
코드 :
function download(filename, text) {
var a = window.document.createElement('a');
var byteArray = new Uint8Array(text.length);
for (var i = 0; i < text.length; i++) {
byteArray[i] = text.charCodeAt(i) & 0xff;
}
a.href = window.URL.createObjectURL(new Blob([byteArray.buffer], {'type': 'application/type'}));
a.download = filename;
// Append anchor to body.
document.body.appendChild(a);
a.click();
// Remove anchor from body
document.body.removeChild(a);
}
가 왜 사용자가 UTF8 파일을 다운로드하지 않습니다? 파일에 다른 내용이 있지만 문자열의 텍스트가 있습니까? – Bergi
실행 파일을위한 특수 파일이기 때문에 korean과 같은 특수 문자가 변환되지 않으면 exe는 파일을 구문 분석 할 수 없습니다. 예 숫자 값이 많이 있습니다 (pack() 함수로 변환 됨). – Keveun