You are currently in the Forum Archive. All content within this area is Read-Only and cannot be modified. Active Forums can be found here.
Reply
Honored Resident
Haravikk Mistral
Posts: 95

Convert "unsigned" integer to string

I've been working on some convenient code snippets for one of my LSL projects so that I can use LSL's integers as if they were unsigned, and with a bit of a trickery I think I've got myself up and running without too much trouble.

However, one function has been giving me problems; outputting the "unsigned" value as a string. I know how to output an unsigned value manually in other languages by simply dividing by 10 and producing single base 10 digits using modulo 10, however, I'm unsure how to do this for a value that is using the left-most bit as getting the correct value is rather complicated.

I do have a solution, but it's pretty horrible as it involves ignoring the left-most bit, and performing a retroactive correction of each digit, not pretty at all! I'm wondering if anyone knows a more compact method of converting an integer to a base 10 string as if it were unsigned?

I expect something to do with breaking the value into smaller pieces such as 16-bit segments may do the trick, but so far the math of how to get the correct values out of such a solution has defeated me. I'm dreading that it'll be something simple that I've managed to completely miss, but then it usually is the simple things that I completely forget

Rolig Loon
Posts: 24,632
Registered: ‎10-05-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

I don't have a good answer for you, except to suggest that you move this thread over to the Scripting forum where it's more likely to attract attention.

Not as dumb as I look
Ewer Short
Posts: 658
Topics: 0
Blog Posts: 0
Ideas: 0
Solutions: 2
Registered: ‎10-05-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

Would this be what you need:   http://wiki.secondlife.com/wiki/LlAbs

I hope that helped.  If not, sorry, I'm at a loss.

Ewer Short
Posts: 658
Topics: 0
Blog Posts: 0
Ideas: 0
Solutions: 2
Registered: ‎10-05-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

what about :  http://wiki.secondlife.com/wiki/LlFabs

Since float gives you a much higher ranger than integer?

then typecast to a string and strip off the decimal and trailing 0's

Ewer Short
Posts: 658
Topics: 0
Blog Posts: 0
Ideas: 0
Solutions: 2
Registered: ‎10-05-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

Apparently I had no clue what you were talking about, and your example is way above my head.. Sorry

Hopefully someone who knows what you are doing can help.

Honored Resident
Limpy Hax
Posts: 85

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

...........................

.....................................

..................................(blink)

.....................

................................i can pee for a whole minute

Easily Distracted..Casual Observer :smileyhappy:
Ewer Short
Posts: 658
Topics: 0
Blog Posts: 0
Ideas: 0
Solutions: 2
Registered: ‎10-05-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

LOL, that was my first reaction, I should have just gone with my gut on that feeling, and shut my lips.

Honored Resident
Haravikk Mistral
Posts: 95

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

To understand the solution, basically there's only a problem if the hi-bit (left-most or sign bit) is set, since LSL will see such an integer as negative, but I want to treat it as if that bit were positive. If the value isn't negative, then you can just cast to string normally.

Otherwise, the code will remove the hi-bit and cast what is left. It then combines this base 10 value, and combines it with the base 10 value of the hi-bit (2147483648) by adding the digits.

So say for example the value that I wanted to store was 2147493648. In LSL's signed integers this will be stored as -2147473648 since it is larger than a 32-bit integer can normally hold.

To get this into base 10 format (to output as a string) I therefore have to remove the sign bit, which leaves me with 10000. This of course isn't the correct value, so I then go through, manually adding the digits, so the right-most 0 is added to 8, the next is added to 4, the third to 6 and so-on, performing any carry as necessary (just like any manual addition).

This gives the correct value; it isn't pretty, but at least it works!

Honored Resident
Haravikk Mistral
Posts: 95

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

Ack well, I'd been hoping there may be a simpler, cleverer way to solve it but these seem the only options. I went with manually adding the hi-bit's base 10 digits with carry route, as it was the easiest to do in LSL.

Here's the function I'm using:

string unsignedToString(integer uint) {

     string s;

     {

          if (uint < 0) {

               string t = (string)(uint & 0x7FFFFFFF);

               // Pad string to correct length

               integer x = llStringLength(t);

               while (x < 10) {

                    t = "0" + t;

                    ++x;

               }

               // Take each base 10 digit and combine with the

               // corresponding digit in hibit string

               integer i = -1; x = -x;

               integer tc; integer c = 0;

               do {

                    tc = (integer)llGetSubString(t, i, i) +

                         (integer)llGetSubString(

                              "2147483648", i, i) + c;

                    c = tc / 10;

                    tc = tc % 10;

                    s = (string)tc + s;

               } while ((--i) >= x);

          } else s = (string)uint;

     }

     return s;

}

Any optimisations would certainly be welcome, but it seems about as lean as it's likely to get. As an aside though, is there a better syntax highlight option for LSL? The plaintext one is rubbish, and editing it is an absolute pain as nothing seems to copy/paste into it properly.

Void Singer
Posts: 7,475
Solutions: 64
Registered: ‎05-28-2009

Re: Convert "unsigned" integer to string

Reply to Haravikk Mistral - view message

ack! good point... I stole the fetch from my hex converter.

ETA:
bitwise AND is a bit faster than Less-Than

- Farewell to your ports and good luck to you all