투케이2K

63. (Flutter/플러터) [Mac] : [Dart] : 다트 - [convert] string to hex 인코딩 및 디코딩 수행 - encode , decode 본문

Flutter

63. (Flutter/플러터) [Mac] : [Dart] : 다트 - [convert] string to hex 인코딩 및 디코딩 수행 - encode , decode

투케이2K 2024. 5. 29. 20:09
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Dart

 

[소스 코드]

import 'package:flutter/material.dart';
import 'dart:developer';
import 'dart:convert';


// -----------------------------------------------------------------------------------------
// TODO [main] : [application 의 진입점 역할]
// -----------------------------------------------------------------------------------------
void main() {

  /**
   * ------------------------------------------------
   * [요약 설명]
   * ------------------------------------------------
   * 1. 필요 import : import 'dart:convert';
   * ------------------------------------------------
   * 2. convert 참고 사이트 : https://api.dart.dev/stable/3.4.1/dart-convert/dart-convert-library.html
   * ------------------------------------------------
   * 3. padLeft : 왼쪽으로 주어진 문자를 갯수만큼 채웁니다
   * ------------------------------------------------
   * */


  try {

    // [초기 변수 선언]
    var str_data = "hello";


    // [Hex 인코딩 수행]
    String hexEncode = utf8.encode(str_data).map((e) => e.toRadixString(16).padLeft(2, '0')).join();


    // [Hex 디코딩 수행]
    String hexDecode = "";

    for (int i=0; i<hexEncode.length/2; i++){
      int len = i*2; // 문자열 시작 주소 (0, 2 ..)

      int i_value = int.parse(hexEncode.substring(len, len + 2), radix:16);

      hexDecode += String.fromCharCode(i_value); // 문자열에 추가
    }


    // [로그 출력 수행]
    log("");
    log("-------------------------------------------------------");
    log("hexEncode :: ${hexEncode}");
    log("-------------------------------------------------------");
    log("hexDecode :: ${hexDecode}");
    log("-------------------------------------------------------");
    log("");

  }
  catch (e) {
    log("");
    log("-------------------------------------------------------");
    log("Catch :: ${e}");
    log("-------------------------------------------------------");
    log("");
  }

}
 

[결과 출력]


 
반응형
Comments