투케이2K

263. (JavaScript) [string to byte] TextEncoder 사용해 string 문자열 데이터를 byte 배열로 변환 수행 본문

JavaScript

263. (JavaScript) [string to byte] TextEncoder 사용해 string 문자열 데이터를 byte 배열로 변환 수행

투케이2K 2023. 5. 8. 19:33
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : JavaScript

 

[소스 코드]

    <!-- [내부 자바스크립트 J쿼리 이벤트 지정] -->
    <script>
    	

        /*
        -----------------------------------------
        [요약 설명]
        -----------------------------------------
        1. TextEncoder : string 데이터를 UTF-8 바이트 스트림으로 변환합니다
        -----------------------------------------
        2. TextEncoder 를 사용해 string to byte 로 변환을 수행할 수 있습니다. (Uint8Array)
        -----------------------------------------
        3. 참고 사이트 : https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
        -----------------------------------------
        */






        // [html 최초 로드 및 이벤트 상시 대기 실시] 
        window.onload = function() {
            console.log("");
            console.log("=========================================");
            console.log("[window onload] : [start]");
            console.log("=========================================");
            console.log(""); 


            // [테스트 함수 호출]
            testMain();
        };





        // [자바스크립트 테스트 코드]
        function testMain() {
            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [start]");
            console.log("=========================================");
            console.log("");


            // [변수 선언 실시]
            var string = "hello"


            // [string to byte 변환 실시]
            var byte_array = new TextEncoder().encode(string);


            // [로그 출력]
            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [result]");
            console.log("-----------------------------------");
            console.log("[string] : " + string);
            console.log("-----------------------------------");
            console.log("[byte_array] : " + byte_array);
            console.log("=========================================");
            console.log("");

        };


    </script>
 

[결과 출력]

 

=========================================
[testMain] : [result]
-----------------------------------
[string] : hello
-----------------------------------
[byte_array] : 104,101,108,108,111
=========================================

 

반응형
Comments