Методы кодирования файлов в формат Base64 на различных языках программирования

Чтобы закодировать файл в формат Base64, вы можете использовать различные языки программирования и библиотеки. Вот несколько популярных методов на разных языках:

  1. Python:

    • Использование модуля base64:

      import base64
      
      with open('file.txt', 'rb') as file:
       encoded_string = base64.b64encode(file.read())
       print(encoded_string.decode('utf-8'))
  2. Ява:

    • Использование библиотеки Apache Commons:

      import org.apache.commons.codec.binary.Base64;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      
      byte[] fileBytes = Files.readAllBytes(Paths.get("file.txt"));
      String encodedString = Base64.encodeBase64String(fileBytes);
      System.out.println(encodedString);
  3. С#:

    • Использование класса Convert:

      using System;
      using System.IO;
      
      byte[] fileBytes = File.ReadAllBytes("file.txt");
      string encodedString = Convert.ToBase64String(fileBytes);
      Console.WriteLine(encodedString);
  4. JavaScript (Node.js):

    • Использование встроенного класса Buffer:

      const fs = require('fs');
      
      fs.readFile('file.txt', (err, data) => {
       if (err) throw err;
       const encodedString = Buffer.from(data).toString('base64');
       console.log(encodedString);
      });
  5. PHP:

    • Использование функции base64_encode:
      $fileContents = file_get_contents('file.txt');
      $encodedString = base64_encode($fileContents);
      echo $encodedString;