Method

# Hex

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Returns as a `String</api/data_types/string>` the hexadecimal version of the number passed.

## Usage

``` xojo
result = Hex(value)
```

| Part   | Type                               | Description                                |
|--------|------------------------------------|--------------------------------------------|
| result | `String</api/data_types/string>`   | *value* converted to hexadecimal.          |
| value  | `Integer</api/data_types/integer>` | The number to be converted to hexadecimal. |

## Notes

If the value is not a whole number, the decimal value will be truncated.

You can specify binary, <span class="title-ref">Hex</span>, or octal numbers by preceding the number with the & symbol and the letter that indicates the number base. The letter b indicates binary, h indicates <span class="title-ref">Hex</span>, and o indicates octal.

VB Compatibility Note: VB rounds the value to the nearest whole number so the <span class="title-ref">Hex</span> function will probably be changed in a future release to do this as well.

## Sample code

Below are examples of various numbers converted to \`Hex\`:

``` xojo
Var hexVersion As String
hexVersion = Hex(5) ' returns "5"
hexVersion = Hex(75) ' returns "4B"
hexVersion = Hex(256) ' returns "100"
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

It is often useful to keep the same number of digits to represent a hexadecimal number, e.g. 8 characters for a 32 bits hexadecimal number. You can achieve such a task with the following code:

``` xojo
Function Hex32(value As Int32) As String
  Const prefix = "00000000" ' A 32-bits value is 8 digits long at max

  Return Right(prefix + Hex(value), 8)  ' We always return 8 characters
End Function
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For an `Int64</api/data_types/additional_types/int64>` integer, you can adapt it as:

``` xojo
Function Hex32(value As Int64) As String
  Const prefix="0000000000000000" ' A 64-bits value is 16 digits long at max

  Return Right(prefix + Hex(value), 16)  ' We always return 16 characters
End Function
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`&b</api/language/literals/b>`, `&c</api/language/literals/c>`, `&h</api/language/literals/h>`, `&o</api/language/literals/o>`, `&u</api/language/literals/u>` literals; `Bin</api/text/bin>`, `DecodeHex</api/text/encoding_text/decodehex>`, `EncodeHex</api/text/encoding_text/encodehex>`, `Oct</api/math/oct>`, `Integer.FromHex<integer.fromhex>`, `Integer.ToHex<integer.tohex>` functions.

</div>
