티스토리 뷰
반응형
[c#/wpf/winform/unity] Byte to String / String to Byte 로 변환 컨버팅
#static으로 손쉽게 사용이 가능하다
public static class ByteConverter
{
public static object ByteToObject(byte[] buffer)
{
try
{
using (MemoryStream stream = new MemoryStream(buffer))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
stream.Position = 0;
return binaryFormatter.Deserialize(stream);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
return null;
}
public static byte[] ObjectToByte(object obj)
{
try
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, obj);
return stream.ToArray();
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
return null;
}
// 바이트 배열을 String으로 변환
public static string ByteToString(byte[] buffer) {
string str = Encoding.Default.GetString(buffer);
return str;
}
// String을 바이트 배열로 변환
public static byte[] StringToByte(string str) {
byte[] buffer = Encoding.UTF8.GetBytes(str);
return buffer;
}
}
#ByteToString #StringToByte #c# #wpf #winform #unity
반응형
'c#' 카테고리의 다른 글
[wpf/c#] mvvm 을 활용한 런처(전원 관리 및 파일 관리) 만들기(2/3) (0) | 2022.03.02 |
---|---|
[c#/wpf/unity] 나의 아이피 주소 가져오기 헬퍼구현 (0) | 2022.03.02 |
[c#/wpf/winform] UDPHelper구현(server/client) (0) | 2022.03.02 |
[wpf/c#] mvvm 을 활용한 런처(전원 관리 및 파일 관리) 만들기(1/3) (0) | 2022.03.02 |
[c#/wpf] UpdateSourceTrigger=PropertyChanged (mvvm 사용 시 바인딩 객체값이 변경되면 바로 반영하기) (0) | 2022.02.24 |
댓글
반응형