Cómo convertir un número en hexadecimal

3399 vistas

La función que devemos usar es IntToStr añadiendo un '$' delante de la cadena que contiene el número hexadecimal:



delphi
  1. function hexaToInt(s : string) : integer;
  2. begin
  3.   if (s <> '') and (s[1] <> '$') then
  4.     Result := StrToInt('$' + s)
  5.   else
  6.     Result := StrToInt(s);
  7. end;



Ejemplos:



delphi
  1. ShowMessage(IntToStr(hexaToInt('10'))); // 16
  2. ShowMessage(IntToStr(hexaToInt('$10'))); // 16
  3. ShowMessage(IntToStr(hexaToInt('ABCD'))); // 43981



Atención: la función inversa es IntToHex, que devuelve la representación hexadecimal de un entero.