/// <summary> /// Decodes a tag to a label. /// </summary> private string ReadLabel() { // by Pedro Martins string label = ""; uint tag = 0; // get tag bytes byte[] tagBytes = new byte[3]; _stream.Read(tagBytes, 0, 3); // resize tag byte array (add an empty byte) Array.Resize(ref tagBytes, 4); // read tag value (as uint32) from tag byte array tag = BitConverter.ToUInt32(tagBytes, 0); // convert to little endian tag = Utils.SwapBytes(tag) >> 8; // convert tag to label label += Convert.ToChar((((tag >> 18) & 0x3F) & 0x1F) | 64); label += Convert.ToChar((((tag >> 12) & 0x3F) & 0x1F) | 64); label += Convert.ToChar((((tag >> 6) & 0x3F) & 0x1F) | 64); label += Convert.ToChar(((tag & 0x3F) & 0x1F) | 64); // clean label label = Regex.Replace(label, "[^A-Z]+", ""); return(label); }
/// <summary> /// Encodes a label to tag. /// </summary> /// <param name="label">Label to write.</param> private void WriteLabel(string label) { // by Pedro Martins int tag = 0; for (int i = 0; i < label.Length; i++) { tag |= (0x20 | (label[i] & 0x1F)) << (3 - i) * 6; } uint sTag = Utils.SwapBytes(Convert.ToUInt32(tag)) >> 8; // hackhackhack // FIXME MemoryStream hehStream = new MemoryStream(); BinaryWriter hehWriter = new BinaryWriter(hehStream); hehWriter.Write(sTag); _stream.Write(hehStream.GetBuffer().Take(3).ToArray(), 0, 3); }