Skip to content

Instantly share code, notes, and snippets.

@inoook
Created September 23, 2024 09:30
Show Gist options
  • Save inoook/7f9376530228b908ac34cfa015368b69 to your computer and use it in GitHub Desktop.
Save inoook/7f9376530228b908ac34cfa015368b69 to your computer and use it in GitHub Desktop.
uOSCのデータのbyte[] <-> Message変換
using System;
using System.IO;
using uOSC;
public static class uOSCUtils
{
/*
var bytes = uOSCUtils.ByteFromMsg(message);
Debug.LogWarning(bytes.Length);
Message msg = uOSCUtils.MsgFromBytes(bytes);
Debug.LogWarning($"recvMsg: {msg}");
*/
// https://github.com/hecomi/uOSC/blob/master/Assets/uOSC/Runtime/Core/Message.cs
static Parser parser_ = new Parser();
public static byte[] ByteFromMsg(Message msg)
{
using (var stream = new MemoryStream())
{
msg.Write(stream);
byte[] data = stream.GetBuffer();
int count = (int)stream.Position;
byte[] sendData = new byte[count];
Array.Copy(data, 0, sendData, 0, count);
//Debug.LogWarning(sendData.Length);
//Debug.LogWarning(string.Join(",", sendData));
return sendData;
}
}
public static Message MsgFromBytes(byte[] data)
{
int pos = 0;
parser_.Parse(data, ref pos, data.Length);
Message msg = parser_.Dequeue();
return msg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment