Base64 encode and decode in Scala

import java.util.Base64
import java.nio.charset.StandardCharsets

val plainText: String = "abcdefg"

val encoded: String = Base64.getEncoder.encodeToString(plainText.getBytes("UTF-8"))

val decoded: Array[Byte] = Base64.getDecoder.decode(encoded)

val str1 = new String(decoded)
val str2 = new String(decoded, StandardCharsets.UTF_8)

output

plainText: String = abcdefg
encoded: String = YWJjZGVmZw==
decoded: Array[Byte] = Array(97, 98, 99, 100, 101, 102, 103)
str1: String = abcdefg
str2: String = abcdefg

Scala – Convert Base64 to Hex String

import java.util.Base64
import org.apache.commons.codec.binary.Hex

val guid: String = "YxRfXk827kPgkmMUX15PNg=="
val decoded: Array[Byte] = Base64.getDecoder.decode(guid)
val hexString: String = Hex.encodeHexString(decoded)

output

guid: String = YxRfXk827kPgkmMUX15PNg==
decoded: Array[Byte] = Array(99, 20, 95, 94, 79, 54, -18, 67, -32, -110, 99, 20, 95, 94, 79, 54)
hexString: String = 63145f5e4f36ee43e09263145f5e4f36

แปลงไฟล์ PDF เป็น Base64String กับ .NET 6 WebApi

สร้างโปรเจ็กส์แบบ ASP.NET Core Web API

เพิ่ม API Controller ชื่อ Values (Controllers/ValuesController.cs)

using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi1.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ILogger<ValuesController> _logger;
        private IWebHostEnvironment _env;

        public ValuesController(ILogger<ValuesController> logger
            , IWebHostEnvironment env)
        {
            _logger = logger;
            _env = env;
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            string rootPath;
            if (!string.IsNullOrEmpty(_env.WebRootPath))
                rootPath = _env.WebRootPath;
            else
                rootPath = _env.ContentRootPath;
            _logger.LogInformation($"rootPath  = {rootPath}");


            #region Convert byte[] to Base64String
            string pdfPathIP = System.IO.Path.Combine(rootPath, "resource/testIP.pdf");
            _logger.LogInformation($"pdfPathIP = {pdfPathIP}");
            byte[] bytesIP = System.IO.File.ReadAllBytes(pdfPathIP);
            string pdfBase64 = Convert.ToBase64String(bytesIP);
            #endregion


            #region Convert Base64String to byte[]
            string pdfPathOP = System.IO.Path.Combine(rootPath, "resource/testOP.pdf");
            _logger.LogInformation($"pdfPathOP = {pdfPathOP}");
            byte[] bytesOP = Convert.FromBase64String(pdfBase64);
            System.IO.File.WriteAllBytes(pdfPathOP, bytesOP);
            #endregion

            return pdfBase64;
        }
    }
}

วางไฟล์ทดสอบไว้ที่ resource/testIP.pdf

ทดลองเรียก https://localhost:7034/values/5

จะได้ไฟล์ resource/testOP.pdf

C# Base64

นำสตริง 111 มาทำเป็น Base64 แล้ว ถอดกลับออกมา

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string plainText1 = "111";
            byte[] byteData1 = System.Text.ASCIIEncoding.ASCII.GetBytes(plainText1);
            string encodeText = System.Convert.ToBase64String(byteData1);
            Console.WriteLine(encodeText);

            byte[] byteData2 = System.Convert.FromBase64String(encodeText);
            string plainText2 = System.Text.ASCIIEncoding.ASCII.GetString(byteData2);
            Console.WriteLine(plainText2);
        }
    }
}

// MTEx
// 111