16进制转化为字符串 ---.net core webapi
namespace Demo02.Utility._16ToString { public static class TXT { /// <summary> /// 向文本里面写入配置信息 /// </summary> /// <param name="configPath">文件路径</param> /// <param name="times"></param> /// <param name="days"></param> /// <param name="delPath">写入内容</param> public static void WriteConfigToTxt(string delPath,string filePath) { try { String configPath = Path.GetDirectoryName(filePath); //判断该路径下文件夹是否存在,不存在的情况下新建文件夹 if (!Directory.Exists(configPath)) { Directory.CreateDirectory(configPath); } //定义文件信息对象 FileInfo finfo = new FileInfo(filePath); if (!finfo.Exists) { FileStream fs; fs = File.Create(filePath); fs.Close(); finfo = new FileInfo(filePath); } //先将文本清空 System.IO.File.WriteAllText(filePath, string.Empty); //创建只写文件流 using (FileStream fs = finfo.OpenWrite()) { //根据上面创建的文件流创建写数据流 StreamWriter w = new StreamWriter(fs); //设置写数据流的起始位置为文件流的末尾 w.BaseStream.Seek(0, SeekOrigin.End); //写入内容 w.Write(DateTime.Now + "\n" +"\n" + delPath + "\n\r"); //清空缓冲区内容,并把缓冲区内容写入基础流 w.Flush(); //关闭写数据流 w.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } } /// <summary> /// 读取txt文件,并返回文件中的内容 /// </summary> /// <returns></returns> public static string ReadTxTContent(string contentPath) { try { if (File.Exists(contentPath)) { string strCon = string.Empty; using (StreamReader sr = new StreamReader(contentPath)) { string line; // 从文件读取并显示行,直到文件的末尾 while ((line = sr.ReadLine()) != null) { strCon += line + " "; } } return strCon; } return null; } catch (Exception e) { Console.WriteLine(e.Message); return null; } } ///<summary> /// 从16进制转换成汉字 /// </summary> /// <param name="hex"></param> /// <param name="charset">编码,如"utf-8","gb2312"</param> /// <returns></returns> public static string UnHex(string hex, string charset) { if (hex == null) throw new ArgumentNullException("hex"); hex = hex.Replace(",", ""); hex = hex.Replace("\n", ""); hex = hex.Replace("\\", ""); hex = hex.Replace(" ", ""); if (hex.Length % 2 != 0) { hex += "20";//空格 } // 需要将 hex 转换成 byte 数组。 byte[] bytes = new byte[hex.Length / 2]; for (int i = 0; i < bytes.Length; i++) { try { // 每两个字符是一个 byte。 bytes[i] = byte.Parse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); } catch { // Rethrow an exception with custom message. throw new ArgumentException("hex is not a valid hex number!", "hex"); } } System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset); return chs.GetString(bytes); } } }
using Demo02.Model; using Demo02.Utility._16ToString; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Runtime.InteropServices; namespace Demo02.Controllers { /// <summary> /// 16进制转化字符串 /// </summary> [ApiVersion("1.0")] [Route("[controller]/V{version:apiVersion}")] [ApiController] public class ToStringController : ControllerBase { private readonly ILogger<ToStringController> _logger; /// <summary> /// 构造函数 /// </summary> /// <param name="logger"></param> public ToStringController(ILogger<ToStringController> logger) { _logger = logger; } /// <summary> /// 获取用户信息 /// </summary> /// <returns></returns> [HttpGet()] public string Get(string path,string newFilePath) { string pathInfo = String.Empty; pathInfo = TXT.UnHex(TXT.ReadTxTContent(path), "utf-8"); TXT.WriteConfigToTxt(pathInfo, newFilePath); return pathInfo; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了