Core Function Hex

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
 
(8 intermediate revisions by one user not shown)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
  
Returns a string representation of an integer type converted to hexadecimal
+
Returns a string representation of an integer type converted to hexadecimal.
  
 
=== Parameters ===
 
=== Parameters ===
Line 15: Line 15:
 
==== length ====
 
==== length ====
  
Optional; Number of characters to be returned (up to 8) for integer.
+
Optional; Number of characters to be returned (up to 16) for (64-bit) integer.
 
Characters are truncated from the left-hand side if length is too small.
 
Characters are truncated from the left-hand side if length is too small.
 +
 +
If this is -1 then it will return the hex with no leading zeros (unless of course the value converts to something like 01EB)
 +
 +
Default: -1
  
 
=== Return Value ===
 
=== Return Value ===
Line 26: Line 30:
 
=== Remarks ===
 
=== Remarks ===
  
The function only works with numbers that fit in a 32 bit signed integer OR 64 bit signed integer.
+
The function works with numbers that fit in a 32 bit signed integer OR 64 bit signed integer.
 +
 
 +
It is best to cast numbers with (int) to make sure its going to be a int.
  
 
=== Example ===
 
=== Example ===
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$result = Hex(1033)
+
$result = Hex(1033);
println($result) ; prints "409"
+
println($result); // prints "409"
 +
 +
$result = Hex(1033, 4);
 +
println($result); // prints "0409"
 +
 +
$result = Hex(1033, 8);
 +
println($result); // prints "00000409"
 +
</syntaxhighlight>
 +
 
 +
Example with cast on a $variable
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$var = 1033;
 +
$result = Hex((int)$var);
 +
println($result); // prints "409"
  
$result = Hex(1033, 4)
+
$result = Hex((int)$var, 4);
println($result) ; prints "0409"
+
println($result); // prints "0409"
  
$result = Hex(1033, 8)
+
$result = Hex((int)$var, 8);
println($result) ; prints "00000409"
+
println($result); // prints "00000409"
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 18:34, 2 September 2015

Hex( <expression>, <length> )

Contents

Description

Returns a string representation of an integer type converted to hexadecimal.

Parameters

expression

The expression to convert.

length

Optional; Number of characters to be returned (up to 16) for (64-bit) integer. Characters are truncated from the left-hand side if length is too small.

If this is -1 then it will return the hex with no leading zeros (unless of course the value converts to something like 01EB)

Default: -1

Return Value

Success: Returns a string of length characters, zero-padded if necessary for integer.

Failure: Returns "" (blank string).

Remarks

The function works with numbers that fit in a 32 bit signed integer OR 64 bit signed integer.

It is best to cast numbers with (int) to make sure its going to be a int.

Example

$result = Hex(1033);
println($result); // prints "409"
 
$result = Hex(1033, 4);
println($result); // prints "0409"
 
$result = Hex(1033, 8);
println($result); // prints "00000409"

Example with cast on a $variable

$var = 1033;
$result = Hex((int)$var);
println($result); // prints "409"
 
$result = Hex((int)$var, 4);
println($result); // prints "0409"
 
$result = Hex((int)$var, 8);
println($result); // prints "00000409"
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox