기본 콘텐츠로 건너뛰기

추천 가젯

Base64 이론과 파이썬 예제

BASE64 개요 우리는 데이터를 처리할 때, 문자열(ASCII) 데이터를 사용하기도 하고 이진(Binary)데이터를 사용하기도 한다. 문자열 데이터든, 이진 데이터든, 최종적으로 컴퓨터는 이진 데이터로 변환해 처리하지만, 이진 데이터는 사람이 읽기 힘들고, 문자열을 주로 전송하는 프로토콜(SMTP, HTTP 등)에서 원활한 작동이 일어나지 않을 수 있다. 따라서 이진 데이터를 문자열 데이터로 변환하는 과정이 필요하다. 이 과정에서 이진 데이터를 16진수(HEX)로 변환하는 방법이 고안되었다.   예를 들어, '01000101     00110001     01001100'이라는 데이터가 있다. 이 이진 데이터는 사람이 읽기도 힘들고, 무슨 데이터를 뜻하는지 확인하기 어렵다. 따라서 이 데이터를 표기할 때 10진수나 16진수를 이용하여 표현한다. 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 1 0 0 1 1 0 0 4 5 3 1 4 C 0x45 0x31 0x4C 010001010011000101001100 -> 45 31 4C 위와 같이 3바이트에 해당하는 내용을 단 여섯 문자로 표현할 수 있으며,  16진수의 표기범위인 0~F를 직관적으로 이해할 수 있기에 파일 데이터를 분석할 때 주로 HEX 표기를 사용한다. 다만, 이렇게 Binary를 HEX로 변환하는 것은, 데이터의 분석을 조금 더 쉽게 하기 위함일 뿐이지, Binary 데이터를 문자열로 변환하는 목적으로는 잘 사용되지 않는다. 그러면 어떻게 Binary 데이터를 문자열로, 그것도 효율적으로 바꿀 수 있을까. ASCII 바로 Binary 데이터와 ASCII 문자를 서로 연결하는 사전을 만들어서, 해당 사전에 맞게 Binary 데이터를 ASCII 문자로 변환한다. 이전의 HEX 표기처럼 특정한 패턴의 Binary 데이터를 ASCII 문자에 대칭시키는 것이다. 다만 이러한 방법을 생각해 내도 문제가 발생한다. 1바이트는 256개의 값...

[과제] 정도전의 밀본 만들기



"linkedlist.h"

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)

struct linked_list {
 char passcode;
 struct linked_list* next;
};
typedef struct linked_list node;
typedef node* link;
"listlib.c"

#include "linkedlist.h"

link createnode(char passcode)
{
 link cur;
 cur = (node*)malloc(sizeof(node));
 if (cur == NULL)
  return NULL;

 cur->passcode = (char *)malloc(sizeof(char));
 cur->passcode = passcode;
 cur->next = NULL;

 return cur;
}

link append(link head, link cur)
{
 link nextnode = head;
 if (head == NULL)
 {
  head = cur;
  return head;
 }
 while (nextnode->next != NULL)
  nextnode = nextnode->next;
 nextnode->next = cur;

 return head;
}

int printlist(link head)
{
 int cnt = 0;
 link nextnode = head;
 while (nextnode != NULL)
 {
  printf("%3d번째 암호는 %c\n", ++cnt, nextnode->passcode);
  nextnode = nextnode->next;
 }
 return cnt;
}
"functionlib.c"

#include "linkedlist.h"

void function(int choice, link head, link cur)
{
 int input = 0;
 char ch;
 link nextnode = head;
 link pre = head;
 link post = NULL;


 switch (choice) {
 case 1:
  printf("몇 번째 단지에 집어넣으시겠습니까? : ");
  scanf("%d", &input);
  if (input < 2)
  {
   printf("정도전의 집 단지는 바꿀 수 없소.\n신성불가침의 영역이오.\n");
   break;
  }
  getchar();
  printf("무슨 암호를 넣으시겠습니까? : ");
  scanf("%c", &ch);
  cur = createnode(ch);
  for (int i = 0; i < input - 2; i++)
   pre = pre->next;
  post = pre->next;
  pre->next = cur;
  cur->passcode = ch;
  cur->next = post;
  break;
 case 2:
  printf("몇 번째 단지를 없애버릴까요? : ");
  scanf_s("%d", &input);
  if (input < 2)
  {
   printf("정도전의 집은 없앨 수 없소.\n");
   break;
  }
  for (int i = 0; i < input - 2; i++)
   pre = pre->next;
  pre->next = pre->next->next;  
  break;
 case 3:
  printlist(head);
  break;
 case 4:
  printf("몇 번째 단지의 정보가 궁금합니까? : ");
  scanf_s("%d", &input);
  for(int i=0; inext;
  printf("%d번째 단지의 암호는 %c입니다.\n", input, nextnode->passcode);
  break;
 default:
  printf("입력 오류 또는 종료 커맨드로 인한 종료.\n");
 }
}
"main.c"

#include "linkedlist.h"

link createnode(char passcode);
link append(link head, link cur);
int function(int choice, link head, link cur);
int printlist(link head);

int main()
{
 char passcode;
 link head = NULL;
 link cur = NULL;
 int choice = 0;

 printf("고려의 혁명을 위한 비밀문자를 입력하고 엔터 누르시오. 0을 누르면 끝입니다.\n");
 while(1)
 {
  scanf_s("%c", &passcode, 1);
  getchar();
  if (passcode == '0')
   break;
  cur = createnode(passcode);
  if (cur == NULL) exit(1);
  head = append(head, cur);
 }
 system("cls");
 while (choice != 5)
 {
  printf("암호가 완성되었습니다. 무얼 하시겠습니까?\n1.단지에 암호 삽입\n2.단지 제거\n3.전체 암호 검색\n4.n번째 단지 암호 검색\n5.종료\n선택하시오 : ");
  scanf_s("%d", &choice);
  function(choice, head, cur);
  printf("\n\n");
 }

 system("pause");
 return 0;
}
실행결과





댓글

가장 많이 본 글