public static byte[] ToCBOR(this ulong val) { //Major Type 0 (MajorType.UNSIGNED_INT) MemoryStream ms = new MemoryStream(); byte[] header = new ItemHeader(MajorType.UNSIGNED_INT, val).ToByteArray(); ms.Write(header, 0, header.Length); return ms.ToArray(); }
public static byte[] ToCBOR(this string val) { MemoryStream ms = new MemoryStream(); byte[] header = new ItemHeader(MajorType.TEXT_STRING, (ulong)val.Length).ToByteArray(); ms.Write(header, 0, header.Length); ms.Write(Encoding.UTF8.GetBytes(val),0, Encoding.UTF8.GetByteCount(val)); //Major Type 3 (MajorType.TEXT_STRING) return ms.ToArray(); }
public ItemHeader ReadHeader () { ItemHeader header = new ItemHeader (); header.tags = ReadTags (); ulong size = 0; byte b = (byte)buffer.ReadByte (); if (b == 0xFF) { header.breakMarker = true; return header; } header.majorType = (MajorType)(b >> 5); b &= 0x1f; header.additionalInfo = (ulong)b; if (b >= 24 && b <= 27) { b = (byte)(1 << (b - 24)); header.value = readUnsigned (b); } else if (b > 27 && b < 31) { throw new Exception (); } else if (b == 31) { header.indefinite = true; } return header; }