当前位置:网站首页>UWP 转换 IBuffer 和其他类型
UWP 转换 IBuffer 和其他类型
2022-08-04 15:38:00 【林德熙】
本文告诉大家在 UWP 如何转换 IBuffer 为 string 和 stream 类
byte 数组转 IBuffer
使用下面代码可以将 byte 数组转 IBuffer
using System.Runtime.InteropServices.WindowsRuntime;
byte[] byteList = xx;
IBuffer buffer = byteList.AsBuffer();
这里的 AsBuffer 是扩展方法所以需要使用 using 的方法
using System.Runtime.InteropServices.WindowsRuntime;
IBuffer buffer = xx;
var byteList = buffer.ToArray();
string 转 IBuffer
可以使用两个方式,第一个方式是将 string 转换为 byte 数组,请看代码
using System.Text;
byteList = Encoding.UTF8.GetBytes(str);
从数组转 string 的方法请看下面
using System.Text;
var str = Encoding.UTF8.GetString(byteList);
转换为 byte 数组就可以使用上面的方法转换为 IBuffer 但是在这样可以看到方法的代码有些多,可以使用下面的方法快速转换
通过 CryptographicBuffer 类可以将 string 转换为 IBuffer 请看代码
using Windows.Security.Cryptography;
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
str = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer);
边栏推荐
猜你喜欢
随机推荐
ITSM软件与工单系统的区别是什么?
【已解决】allure无法生成json文件和AttributeError: module ‘allure‘ has no attribute ‘severity_level‘
Codeforces Round #811 A~F
For循环控制
图解 SQL,这也太形象了吧!
7 天学个Go,Go 结构体 + Go range 来学学
录音文件识别
性能提升400倍丨外汇掉期估值计算优化案例
小程序|炎炎夏日、清爽一夏、头像大换装
【Harmony OS】【FAQ】Hongmeng Questions Collection 2
Codeforces Round #811 A~F
Legal education combined with VR panorama, intuitively feel and learn the spirit of the rule of law
成员变量与局部变量的区别有哪些
To ensure that the communication mechanism
重构指标之如何监控代码圈复杂度
NUS颜水成等发布首篇《深度长尾学习》综述
保证通信的机制有哪些
MySQL select加锁分析
从-99打造Sentinel高可用集群限流中间件
#夏日挑战赛# HarmonyOS 实现一个滑块验证