Int2Hex

From Second Life Wiki
Jump to navigation Jump to search

Summary

User-Defined Function: string uInteger2Hexadecimal( integer vIntBit );

Returns a string that is vIntBit converted to hexadecimal

  • vIntBit: source integer to convert


Terse

(Leading zeros removed)

  • LSO: 134 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string  vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
    }while (vIntBit = vIntBit >> 4 & 0x0FFFFFFF);
    return vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Labeled Terse

("0x" prefix, Leading zeros removed)

  • LSO: 140 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string  vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
    }while (vIntBit = vIntBit >> 4 & 0x0FFFFFFF);
    return "0x" + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Full

(Leading zeros included)

  • LSO: 153 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string  vStrRtn;
    integer vIntCnt = 8;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
        vIntBit = vIntBit >> 4;
    }while (vIntCnt = ~-vIntCnt);
    return vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Labeled Full

("0x" prefix, Leading zeros included)

  • LSO: 159 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string  vStrRtn;
    integer vIntCnt = 8;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
        vIntBit = vIntBit >> 4;
    }while (vIntCnt = ~-vIntCnt);
    return "0x" + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Portable Terse

(Sign prefix, Leading zeros removed)

  • LSO: 188 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string vStrSgn;
    if (vIntBit >> 31){
        vStrSgn = "-";
        vIntBit = -vIntBit;
    }
    string vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
    }while( vIntBit = vIntBit >> 4 & 0x0FFFFFFF );
    return vStrSgn + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Portable Labeled Terse

(Sign prefix, "0x" prefix, Leading zeros removed)

  • LSO: 194 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string vStrSgn;
    if (vIntBit >> 31){
        vStrSgn = "-";
        vIntBit = -vIntBit;
    }
    string vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
    }while( vIntBit = vIntBit >> 4 & 0x0FFFFFFF );
    return vStrSgn + "0x" + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Portable Full

(Sign prefix, Leading zeros included)

  • LSO: 207 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string vStrSgn;
    //-- Fixed Width alternates of the above line (208 Bytes LSO - 512 Bytes Mono)
    //-- string vStrSgn = " "; //-- Format: "-80000000" to " 7FFFFFFF"
    //-- string vStrSgn = "+"; //-- Format: "-80000000" to "+7FFFFFFF"
    if (vIntBit >> 31){
        vStrSgn = "-";
        vIntBit = -vIntBit;
    }
    integer vIntCnt = 8;
    string vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
        vIntBit = vIntBit >> 4;
    }while( vIntCnt = ~-vIntCnt );
    return vStrSgn + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Portable Labeled Full

(Sign prefix, "0x" prefix, Leading zeros included)

  • LSO: 213 bytes
  • MONO: 512 bytes
string uInteger2Hexadecimal( integer vIntBit ){
    string vStrSgn;
    //-- Fixed Width alternates of the above line (214 Bytes LSO - 512 Bytes Mono)
    //-- string vStrSgn = " "; //-- Format: "-0x80000000" to " 0x7FFFFFFF"
    //-- string vStrSgn = "+"; //-- Format: "-0x80000000" to "+0x7FFFFFFF"
    if (vIntBit >> 31){
        vStrSgn = "-";
        vIntBit = -vIntBit;
    }
    integer vIntCnt = 8;
    string vStrRtn;
    do{
        vStrRtn = llGetSubString( "0123456789ABCDEF", vIntBit & 0x0000000F, vIntBit & 0x0000000F ) + vStrRtn;
        vIntBit = vIntBit >> 4;
    }while( vIntCnt = ~-vIntCnt );
    return vStrSgn + "0x" + vStrRtn;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2010 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Notes

  • All Labeled formats can be cast directly to integer within LSL
  • Full formats are optimal for fixed width viewing, or packing into raw strings.
  • Portable formats are designed for compatibility with outside systems that may use different bit lengths
  • Suggest using the smallest compiled version that fits your needs

See Also

   llIntegerToHex() - function for integer/hex conversion