public Byte[] toByteArray() { unsafe { Byte[] resultado = new Byte[sizeof(FormatoIdioma)]; FormatoIdioma idioma = new FormatoIdioma(); idioma.versao = (Byte)this.versao; } unsafe { Byte[] resultado = new Byte[sizeof(FormatoIdioma)]; fixed(byte *pSrc = resultado) { FormatoIdioma *idioma = (FormatoIdioma *)pSrc; idioma->versao = (Byte)this.versao; this.nome = this.nome.PadRight(16, '\0'); for (int i = 0; i < this.nome.Length; i++) { idioma->nome[i] = (byte)this.nome[i]; } stringToByteArray(idioma->texto, this.texto, 16); idioma->crc = this.crc; } return(resultado); } }
public void FromBytesToFormatoIdioma(Byte[] dados) { unsafe { fixed(byte *pSrc = dados) { FormatoIdioma *idioma = (FormatoIdioma *)pSrc; this.versao = idioma->versao; this.nome = String.Empty; for (int i = 0; i < TAM_MAX_FRASE; i++) { this.nome += (char)idioma->nome[i]; } this.texto.Clear(); for (int indiceFrase = 0; indiceFrase < QTD_FRASES; indiceFrase++) { String Aux = string.Empty; for (int i = 0; i < TAM_MAX_FRASE; i++) { Aux += (char)idioma->texto[(indiceFrase * TAM_MAX_FRASE) + i]; } this.texto.Add(Aux); } } } }
public unsafe void AtualizarCRC() { Byte[] dados = toByteArray(); fixed(byte *pSrc = dados) { FormatoIdioma *parametros = (FormatoIdioma *)pSrc; this.crc = CalcularCRC(dados); } }
public bool VerificarCRC(byte[] dados) { unsafe { fixed(byte *pSrc = dados) { FormatoIdioma *parametros = (FormatoIdioma *)pSrc; return(parametros->crc == CalcularCRC(dados)); } } }
private unsafe UInt16 CalcularCRC(Byte[] dados) { Byte[] dadosCRC = new byte[dados.Length - sizeof(UInt16)]; fixed(byte *pSrc = dados) { FormatoIdioma *parametros = (FormatoIdioma *)pSrc; Array.Copy(dados, 0, dadosCRC, 0, (int)¶metros->crc - (int)pSrc); Array.Copy(dados, ((int)¶metros->crc - (int)pSrc + sizeof(UInt16)), dadosCRC, (int)¶metros->crc - (int)pSrc, dados.Length - ((int)¶metros->crc - (int)pSrc + sizeof(UInt16))); return(CRC16CCITT.Calcular(dadosCRC)); } }
public void UpdateCRC() { unsafe { Byte[] dados = ToByteArray(); Byte[] dadosCRC = new byte[sizeof(FormatoIdioma) - sizeof(UInt16)]; fixed(byte *pSrc = dados) { FormatoIdioma *idioma = (FormatoIdioma *)pSrc; Array.Copy(dados, 0, dadosCRC, 0, (int)&idioma->crc - (int)pSrc); Array.Copy(dados, ((int)&idioma->crc - (int)pSrc + sizeof(UInt16)), dadosCRC, (int)&idioma->crc - (int)pSrc, sizeof(FormatoIdioma) - ((int)&idioma->crc - (int)pSrc + sizeof(UInt16))); this.crc = Util.CRC16CCITT.Calcular(dadosCRC); } } }
public byte[] ToByteArray() { unsafe { Byte[] resultado = new Byte[sizeof(FormatoIdioma)]; fixed(byte *pSrc = resultado) { FormatoIdioma *idioma = (FormatoIdioma *)pSrc; idioma->versao = (Byte)this.versao; idioma->crc = (UInt16)this.crc; Serializable.StringToByteArray(idioma->nome, this.nome, 16); Serializable.StringToByteArray(idioma->textos + (32 * 0), this.textos[0], 32); Serializable.StringToByteArray(idioma->textos + (32 * 1), this.textos[1], 32); Serializable.StringToByteArray(idioma->textos + (32 * 2), this.textos[2], 32); Serializable.StringToByteArray(idioma->textos + (32 * 3), this.textos[3], 32); } return(resultado); } }