読者です 読者をやめる 読者になる 読者になる

【Processing】Base64

3846先輩やパッチギがBase64エンコード・デコードするのが好きみたいなので
取り敢えずエンコードだけProcessingで書いてみました

テキトーに書いてるので、可読性とか一切無いです


↓走らせるとこんな感じ
f:id:kkrnt:20140730005308j:plain

String bin(char alf)
{
  int dec = (int)alf;
  String bin = "";
  int cnt = 0;
  while (dec>=1)
  {
    cnt++;
    bin += (char)(dec%2+48);
    dec /= 2;
  }

  int s_cnt = cnt;
  while (s_cnt%4!=0)
  {
    s_cnt++;
  }

  for (int i=0; i< (s_cnt-cnt); i++)
  {
    bin += '0';
  }

  char [] str = bin.toCharArray();
  bin = "";
  for (int i=str.length-1; i>=0; i--)
  {
    bin += str[i];
  }
  return bin;
}

void setup()
{
  String str = "ABCDEFG";
  String bin = "";
  char [] alf = str.toCharArray();
  for (int i=0; i<alf.length; i++)
  {
    bin += bin(alf[i]);
  }
  
  char [] six_bin = bin.toCharArray();
  int size = six_bin.length;
  while (size%6!=0)
  {
    size++;
  }
  
  char [] six = new char[size];
  for (int i=0; i<six_bin.length; i++)
  {
    six[i] = six_bin[i];
  }

  for (int i=six_bin.length; i<size; i++)
  {
    six[i] = '0';
  }

  String [] six_line = new String[size/6];
  for (int i=0; i<size/6; i++)
  {
    six_line[i] = "";
    for (int j=0; j<6; j++)
    {
      six_line[i] += six[i*6+j];
    }
  }

  String base64 = "";

  for (int i=0; i<size/6; i++)
  {
    base64 += alf(six_line[i]);
  }

  int base_size = size/6;
  while (base_size%4!=0)
  {
    base_size++;
  }

  for (int i=0; i< (base_size-size/6); i++)
  {
    base64 += "=";
  }

  print(str + " ⇔ " + base64);

  exit();
}

char alf(String six_line)
{
  if (six_line.equals("000000"))
  {
    return 'A';
  }
  if (six_line.equals("000001"))
  {
    return 'B';
  }
  if (six_line.equals("000010"))
  {
    return 'C';
  }
  if (six_line.equals("000011"))
  {
    return 'D';
  }
  if (six_line.equals("000100"))
  {
    return 'E';
  }
  if (six_line.equals("000101"))
  {
    return 'F';
  }
  if (six_line.equals("000110"))
  {
    return 'G';
  }
  if (six_line.equals("000111"))
  {
    return 'H';
  }
  if (six_line.equals("001000"))
  {
    return 'I';
  }
  if (six_line.equals("001001"))
  {
    return 'J';
  }
  if (six_line.equals("001010"))
  {
    return 'K';
  }
  if (six_line.equals("001011"))
  {
    return 'L';
  }
  if (six_line.equals("001100"))
  {
    return 'M';
  }
  if (six_line.equals("001101"))
  {
    return 'N';
  }
  if (six_line.equals("001110"))
  {
    return 'O';
  }
  if (six_line.equals("001111"))
  {
    return 'P';
  }
  if (six_line.equals("010000"))
  {
    return 'Q';
  }
  if (six_line.equals("010001"))
  {
    return 'R';
  }
  if (six_line.equals("010010"))
  {
    return 'S';
  }
  if (six_line.equals("010011"))
  {
    return 'T';
  }
  if (six_line.equals("010100"))
  {
    return 'U';
  }
  if (six_line.equals("010101"))
  {
    return 'V';
  }
  if (six_line.equals("010110"))
  {
    return 'W';
  }
  if (six_line.equals("010111"))
  {
    return 'X';
  }
  if (six_line.equals("011000"))
  {
    return 'Y';
  }
  if (six_line.equals("011001"))
  {
    return 'Z';
  }
  if (six_line.equals("011010"))
  {
    return 'a';
  }
  if (six_line.equals("011011"))
  {
    return 'b';
  }
  if (six_line.equals("011100"))
  {
    return 'c';
  }
  if (six_line.equals("011101"))
  {
    return 'd';
  }
  if (six_line.equals("011110"))
  {
    return 'e';
  }
  if (six_line.equals("011111"))
  {
    return 'f';
  }
  if (six_line.equals("100000"))
  {
    return 'g';
  }
  if (six_line.equals("100001"))
  {
    return 'h';
  }
  if (six_line.equals("100010"))
  {
    return 'i';
  }
  if (six_line.equals("100011"))
  {
    return 'j';
  }
  if (six_line.equals("100100"))
  {
    return 'k';
  }
  if (six_line.equals("100101"))
  {
    return 'l';
  }
  if (six_line.equals("100110"))
  {
    return 'm';
  }
  if (six_line.equals("100111"))
  {
    return 'n';
  }
  if (six_line.equals("101000"))
  {
    return 'o';
  }
  if (six_line.equals("101001"))
  {
    return 'p';
  }
  if (six_line.equals("101010"))
  {
    return 'q';
  }
  if (six_line.equals("101011"))
  {
    return 'r';
  }
  if (six_line.equals("101100"))
  {
    return 's';
  }
  if (six_line.equals("101101"))
  {
    return 't';
  }
  if (six_line.equals("101110"))
  {
    return 'u';
  }
  if (six_line.equals("101111"))
  {
    return 'v';
  }
  if (six_line.equals("110000"))
  {
    return 'w';
  }
  if (six_line.equals("110001"))
  {
    return 'x';
  }
  if (six_line.equals("110010"))
  {
    return 'y';
  }
  if (six_line.equals("110011"))
  {
    return 'z';
  }
  if (six_line.equals("110100"))
  {
    return '0';
  }
  if (six_line.equals("110101"))
  {
    return '1';
  }
  if (six_line.equals("110110"))
  {
    return '2';
  }
  if (six_line.equals("110111"))
  {
    return '3';
  }
  if (six_line.equals("111000"))
  {
    return '4';
  }
  if (six_line.equals("111001"))
  {
    return '5';
  }
  if (six_line.equals("111010"))
  {
    return '6';
  }
  if (six_line.equals("111011"))
  {
    return '7';
  }
  if (six_line.equals("111100"))
  {
    return '8';
  }
  if (six_line.equals("111101"))
  {
    return '9';
  }
  if (six_line.equals("111110"))
  {
    return '+';
  }
  if (six_line.equals("111111"))
  {
    return '/';
  }
  return '\0';
}