Jump to content
UBot Underground

Recommended Posts

Christmas is coming, so I thought I would give you a little bit of a UBot specific 'gift'.

 

I have seen many threads where people had issues with combining JavaScript coding with UBot.

 

I chose a simple example (derived from a programming need I had to transform DECIMAL into HEXADECIMAL) of how to create a UBot custom function to change a variable's value from DEC to HEX representation.

Such a function is not natively available in UBot, but it is in JavaScript.

 

So here we go:

 

We start the UBot code, by adding the new DEFINE, with two input parameters:

  • the value to be translated from DEC to HEX and
  • the length of the resulting HEX number.
set(#var_DecVAL, 100, "Global")
set(#var_HexLEN, 4, "Global")

set(#var_HexVAL, $Dec2Hex(#var_DecVAL, #var_HexLEN), "Global")

divider

define $Dec2Hex(#var_DecVAL, #var_HexLEN) {
    return(#var_HexSTR)
}

 

For instance, the number 100 expressed in DECIMAL, is equivalent with 64 expressed in HEX (powers of 16).

 

As you can see, we have an initial length of 3 characters for the decimal value, but we will end with only 2 characters in the hex result.

 

By default the JavaScript function that we shall use would have taken care of this automatically.

However, in certain instances, there may be a need to PAD the HEX result with leading zeros, for instance to express the HEX forcefully in a 4 chars length format, like this:  0064

 

 

The FIRST thing is how to feed the UBot variable's value into the JavaScript per se.  Many people struggle with this, so I thought it is a good thing to detail it a little bit.

 

The JavaScript code would deal with reading the external values by reading the content of the UBot values like this:

 

var DecVAL = {#var_DecVAL};
var HexLEN = {#var_HexLEN};

 

Notice that the variable names are surrounded by the curly brackets.  That basically means that there will be a replacement of the variable's name with its content/value within the UBot code, prior to executing the said code.

 

The above is actually equivalent to:

var DecVAL = 100;
var HexLEN = 4;

... given the selected test values we just fed those variables at the start of the program

 

So, in general, whenever you want to pass a variable's value to a JavaScript code, you need to use those curly brackets.

 

If you only have STD UB version, you cannot actually write in Code View, so you will have to drag'n'drop the variables when editing the javascript node, from the existing bunch of variables in the left menu. 

When you drop them there, UBot will know what you mean, so it will automatically surround them with the curly brackets in Code View.

 

I do not want to lecture you on the topics unrelated directly to UBot, so I won't explain the JavaScript code in detail (you can learn that on your own) but I will tell you briefly what the following code means.

 

First thing we shall need to program a way to determine what to do IF the user (YOU, the UBot coder) would by some mistake, try to force a shorter HEX than the translate function would normally return.

 

Say the DEC is 100, the HEX would be 64 but you have called the function from your main bot loop like this:

set(#var_HexVAL, $Dec2Hex(100, 1), "Global")

... by mistake.

 

Without provisions to make the code return a good answer, you may end up with a wrong result like 4 (truncated 64) for instance...

 

As HEX numbers will ALWAYS be shorter or equal in length with their equivalent DEC numbers, I decided to force the result to have at least the length of the DEC input. 

Of course, if the length you specify when calling the function will be larger than that, then the result will be padded with leading zeros, BUT in case you ask for less length than the correct translation, the result will fail to truncate and just return the correct answer:

 

We shall first determine the length of the actual DEC input:

 

var DecLEN = DecVAL.toString().length;

The JavaScript function toString() will translate any number/string it is applied to into a string type variable, so that we could then be able to apply the length function to determine its actual length in this combo: toString().length

We will apply this combo to the actual value we want to, which we have previously read from UBot variable into our JavaScript variable: DecVAL

 

Thus, the whole JavaScript code becomes:

var DecLEN = DecVAL.toString().length;

For our example where the DEC input was 100, this JavaScript variable will now hold the value 3
(there are three characters in 100)

 

We shall now compare this value with the function call parameter that should setup the output length, specifically what we previously read into our HexLEN JavaScript variable, that now holds the value 4 in our test example.

 

So we shall branch the program with an IF logic, like this:

 

    if (HexLEN >= DecLEN)
    {

    }
    else
    {

    }

Beware that, if you have STD license, you will type or copy/paste this code ^^^^ above as is (in the Node View of the javascript node), HOWEVER, if you code directly, the curly brackets within the IF have to be escaped like this below:

 

    if (HexLEN >= DecLEN)
    \{

    \}
    else
    \{

    \}

The logic employed will be simple.

 

If the forced length is larger or equal to the native length, we shall PAD the result accordingly, BUT if it is shorter, we will get the result directly as is.

 

Here is how to employ the PADding though:

 

The HEX numbers are combinations of digits multiplied by powers of 16, pretty much the same way the decimal numbers are combinations of digits multiplied by powers of 10.

 

In DEC, 110 = 1*10^2 + 1*10^1 + 0*10^0 = 1*100 + 1*10 + 0*1 = 110 (DEC)

 

To calculate the same visual representation (110) but knowing it is a HEX number, we will have to use powers of 16 instead of 10 to deconstruct it:

 

In HEX, 110 = 1*16^2 + 1*16^1 + 0*16^0 =  1*256 + 1*16 + 0*1 = 272 (DEC)

 

To construct a HEX starting from a DEC number will require an inverse process:

 

64 (HEX) = 6*16^1 + 4*16^0 = 96 + 4 = 100 (DEC)

 

However, to truly represent HEX numbers correctly, you have to be aware that the corresponding digits above 9 are represented by letters:

 

Dec --> Hex

    0 --> 0

    1 --> 1

    2 --> 2

    3 --> 3

    4 --> 4

    5 --> 5

    6 --> 6

    7 --> 7

    8 --> 8

    9 --> 9

  10 --> A

  11 --> B

  12 --> C

  13 --> D

  14 --> E

  15 --> F

 

With all this background knowledge now, you will easier understand what the next JavaScript does:

 

Math.pow(16, HexLEN)

.. which calculates the number resulting as the power of 16 taken HexLEN times, in our case 4 times, or in other words, 16^4 = 16*16*16*16 = 65536 (DEC)

 

HOWEVER, in HEX, the same number will be represented by this string: 10000

 

This is important because, if we shall limit our results to max 4 digits, everything above that (leftmost digits except the last 4 rightmost ones) will be irrelevant.

 

So we can basically add the actual number we want to transpose with this value and we shall have padded with zeroes any blanks within the resulting string, which we will then be able to cut off down to only the rightmost 4 characters we need WITHOUT altering the correct result (because the added 10000 is in fact affecting only the upper digits, beyond the scope of our trimming).

 

Here is the JavaScript code to do that:

        var DecSTR = DecVAL+Math.pow(16, HexLEN);

followed by:

        var HexSTR = DecSTR.toString(16).slice(-HexLEN);

...that will remove all the extra higher positioned unwanted digits.

 

Of course, this is only one branch of the IF, while the other one shall NOT use the padding at all, hence no slicing either.

 

The function toString() accepts parameters such as 2, 8, 16, 10 but defaults to 10 if no parameter given.

The use of toString(16) here, basically is the heart of our JavaScript translation, as it performs natively (for JavaScript) what we needed.

 

All the other logic is only validation of exceptions from rule and formatting, to be honest.

 

Back to our code now, the whole IF function will look like this:

 

    if (HexLEN >= DecLEN)
    \{
        var DecSTR = DecVAL+Math.pow(16, HexLEN);
        var HexSTR = DecSTR.toString(16).slice(-HexLEN);
    \}
    else
    \{
        var DecSTR = DecVAL;
        var HexSTR = DecSTR.toString(16);
    \}

There is one last step to take, in order to beautify the result, which basically will transform lower case HEX symbols into capitals (a --> A, ... f --> F) like this:

 

var HexSTR = HexSTR.toUpperCase();

 

Now, we have all the bricks needed to build our custom function for UBot.

 

However, here is a thing to be aware of.

 

If there would have been no IF in the JavaScript code, we could have used the $eval function of UBot directly.

However, that will NOT WORK with the JavaScript IF.

 

We have to RUN JAVASCRIPT first and only return the results with $eval.

 

Here is the complete code for you to study now:

set(#var_DecVAL, 110, "Global")
set(#var_HexLEN, 4, "Global")

set(#var_HexVAL, $Dec2Hex(#var_DecVAL, #var_HexLEN), "Global")

divider

define $Dec2Hex(#var_DecVAL, #var_HexLEN) {

    run javascript("
var DecVAL = {#var_DecVAL};
var HexLEN = {#var_HexLEN};
var DecLEN = DecVAL.toString().length;
    if (HexLEN >= DecLEN)
    \{
        var DecSTR = DecVAL+Math.pow(16, HexLEN);
        var HexSTR = DecSTR.toString(16).slice(-HexLEN);
    \}
    else
    \{
        var DecSTR = DecVAL;
        var HexSTR = DecSTR.toString(16);
    \}
var HexSTR = HexSTR.toUpperCase();
")

    set(#var_HexSTR, $eval("HexSTR"), "Global")

    return(#var_HexSTR)
}

 

Enjoy!

 

Happy Holidays ALL!

  • Like 5
Link to post
Share on other sites

P.S.

 

I tried to upload the file .ubot as well, but the Forum rejected.. Sorry guys!

Anyway, the code is there, it's short (copy/paste if you need it)

  • Like 1
Link to post
Share on other sites

 

This is one of the best Tutorials i have ever seen here Thanks man great work !

Lot's of good tutorials on here.

 

Must not have seen the one about how to solve flash capthca's that I put out...

 

Much more applicable to botting more complex sites. Or the many others from members like TJ, and JohnB, and Gogetta, and all the rest.

 

Anywho, not a competition... :)

 

Merry Christmas to all my fellow Ubotters!

Link to post
Share on other sites
Must not have seen the one about how to solve flash capthca's that I put out...

 

Yeah, mate, great one too!  I liked it, btw... KUDOS!

 

It's not a competition though, like you said...  I just wanted to freely share this one with my fellow UBotters, on XMAS.

 

Usually, mine are explained in as many details as possible and in simple words, so that people from all the walks of life, any level of programming experience and sometimes even poor knowledge of English, even... could understand and apply to their bots.

 

My goal is to teach them how to fish, not handing them the fish per se; as such, many advanced users may find them too deeply detailed, when they have understood things much quicker.

But even the most savvy coders may find a gem or two, I bet.

 

Cheers!

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...