Sin más preámbulos, aquí va:
Algunas pueden hacerse con la clase Convert (http://msdn.microsoft.com/es-es/library/t2htb6f1(v=vs.90).aspx)
- De decimal a binario
Convert.ToString(d, 2); //devuelve un String
- De decimal a hexadecimal:
Convert.ToString(d, 16); //devuelve un String
- De binario a decimal:
int n = Int32.Parse(b);
n = Convert.ToInt32(n.ToString(), 2);
Convert.ToString(n, 10); //devuelve un String
- De binario a hexadecimal:
int n = Int32.Parse(b);
n = Convert.ToInt32(n.ToString(), 2);
Convert.ToString(n, 16); //devuelve un String
- De hexadecimal a decimal:
int n = Int32.Parse(h);
n = Convert.ToInt32(n.ToString(), 16);
Convert.ToString(n, 10); //devuelve un String
- De hexadecimal a binario:
int n = Int32.Parse(h);
n = Convert.ToInt32(n.ToString(), 16);
Convert.ToString(n, 2); //devuelve un String
Con arrays:
- De string a byte[]:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] mensaje = encoding.GetBytes(respuesta); //respuesta es un String
- De byte[] a short[]:
public static short[] ByteArrayToShortArray(byte[] bytes)
{
var shorts = Array.ConvertAll(bytes, b => (short)b);
return shorts;
}
- De short[] a string:
public static string deShortAString(short[] s) {
string rta = "";
for (int i = 0; i < s.Length; i++)
{
// Muestra el short en formato hexadecimal
rta += string.Format("[{0:X2}]", s[i]);
}
return rta;
}
Es todo por hoy, en el futuro, iré actualizando ésto para q quede completito :) Acepto sugerencias ...
No hay comentarios:
Publicar un comentario