이전 목록 다음
채택완료

인코딩 변환

2022-06-21 (화) 10:27:43 1,958

아래는 c#으로 파일 인코딩을 utf-8로 바꿔주는 역할을 합니다.

이걸 php으로 변환 가능 할 까요?

c# 이 윈도우 밖에 지원 안되서 어려움을 겪고 있습니다.

Copy
// Decompiled with JetBrains decompiler
// Type: EncodingChanger.Program
// Assembly: EncodingChanger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 96F72022-65C4-45AF-9226-92B80F08EF02
// Assembly location: D:\MDweb\apache\web\EncodingChanger.exe

using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;

namespace EncodingChanger
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      string str = HttpUtility.UrlDecode(args[0]);
      if (Program.GetEncoding(str) != Encoding.UTF8 && Program.GetEncoding(str) != Encoding.ASCII)
        Program.SaveToUTF_8(str);
      Process.GetCurrentProcess().Kill();
    }

    public static void SaveToUTF_8(string textFilePath)
    {
      using (StreamReader streamReader = new StreamReader(textFilePath, Program.GetEncoding(textFilePath), true))
      {
        using (StreamWriter streamWriter = new StreamWriter(textFilePath + "temp", false, (Encoding) new UTF8Encoding(false)))
        {
          streamWriter.Write(streamReader.ReadToEnd());
          streamWriter.Close();
          streamWriter.Dispose();
        }
        streamReader.Close();
      }
      File.Delete(textFilePath);
      File.Move(textFilePath + "temp", textFilePath);
    }

    private static Encoding GetEncoding(string filename)
    {
      using (StreamReader streamReader = new StreamReader(filename, Encoding.Default, true))
      {
        if (streamReader.Peek() >= 0)
          streamReader.Read();
        return streamReader.CurrentEncoding;
      }
    }
  }
}

답변 1개

채택된 답변
+20 포인트

답변을 작성하려면 로그인이 필요합니다.