ACE3/addons/common/functions/fnc_toHex.sqf

52 lines
1.1 KiB
Plaintext
Raw Normal View History

/*
2015-09-17 16:25:02 +00:00
* Author: commy2, esteldunedain
* Converts number to hexadecimal number
*
* Arguments:
* A number between 0 and 255 <NUMBER>
*
* Return Value:
2015-09-19 22:55:58 +00:00
* A hexadecimal number as string <STRING>
2015-09-17 16:25:02 +00:00
*
* Public: Yes
*/
2015-01-13 19:56:02 +00:00
#include "script_component.hpp"
2015-09-19 22:55:58 +00:00
params ["_number"];
2015-03-23 15:16:50 +00:00
2015-09-19 22:55:58 +00:00
_number = ((round abs _number) max 0) min 255;
2015-03-23 15:16:50 +00:00
2015-09-19 22:55:58 +00:00
if (isNil QGVAR(hexArray)) then {
2015-03-23 15:16:50 +00:00
GVAR(hexArray) = [];
2015-09-19 22:55:58 +00:00
private _minLength = 2;
2015-09-19 22:55:58 +00:00
2015-03-23 15:16:50 +00:00
for [{_i = 0;}, {_i < 256}, {_i = _i + 1}] do {
private _num = _i;
private _hex = ["", "0"] select (_i == 0);
2015-03-23 15:16:50 +00:00
while {_num > 0} do {
private _rest = _num mod 16;
2015-03-23 15:16:50 +00:00
_rest = switch _rest do {
case 10 : {"A"};
case 11 : {"B"};
case 12 : {"C"};
case 13 : {"D"};
case 14 : {"E"};
case 15 : {"F"};
default {str _rest};
};
_num = floor (_num / 16);
_hex = _rest + _hex;
};
2015-09-19 22:55:58 +00:00
2015-03-23 15:16:50 +00:00
while {count toArray _hex < _minLength} do {
_hex = "0" + _hex;
};
2015-09-19 22:55:58 +00:00
2015-03-23 15:16:50 +00:00
GVAR(hexArray) pushBack _hex;
};
};
2015-09-19 22:55:58 +00:00
GVAR(hexArray) select _number // return