static_cast<char> command for autohotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aldrinjohnom
Posts: 77
Joined: 18 Apr 2018, 08:49

static_cast<char> command for autohotkey

28 Nov 2019, 16:47

Hello, I am trying to convert a C++ Script into Autohotkey script, But I do not know what is the autohotkey translation of static_cast<char>. Does anyone know what is the counterpart of this command on autohotkey? This is the code I am trying to convert from C++ to autohotkey:

Code: Select all

int get_id(string game, string name)
{
    name = to_upper(name);              // convert to uppercase
    if (game != game_ts)
    {                                   // for TD and RA
        int i = 0;
        unsigned int id = 0;
        int l = name.length();          // length of the filename
        while (i < l)
        {
            unsigned int a = 0;
            for (int j = 0; j < 4; j++)
            {
                a >>= 8;
                if (i < l)
                    a += static_cast<unsigned int>(name[i]) << 24; // I donot know how to convert this to autohotkey
                i++;
            }
            id = (id << 1 | id >> 31) + a;
        }
        return id;
    }
    else
    {                                    // for TS
        const int l = name.length();
        int a = l >> 2;
        if (l & 3)
        {
            name += static_cast<char>(l - (a << 2)); // I donot know how to convert this to autohotkey
            int i = 3 - (l & 3);
            while (i--)
                name += name[a << 2];
        }
        return name;
    }
}
Developer of AJOM's DOTA2 MOD Master, Easiest way to MOD your DOTA2 without the use of internet :lol: . I created this program using Autohotkey thats why I love Autohotkey and the community!

GitHub:
https://github.com/Aldrin-John-Olaer-Manalansan
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: static_cast<char> command for autohotkey

28 Nov 2019, 20:45

I believe that this code will do what you want, but it's always good to double-check.

Some code:

Code: Select all

;==============================

;before:
a += static_cast<unsigned int>(name[i]) << 24

;after (to unsigned 32-bit integer):
a += (name[i] & 0xffffffff) << 24

;for reference (to signed 32-bit integer):
a += (name[i] << 32 >> 32) << 24

;==============================

;before:
name += static_cast<char>(l - (a << 2))

;after (to signed 8-bit integer):
name += (l - (a << 2)) << 56 >> 56

;for reference (to unsigned 8-bit integer):
name += (l - (a << 2)) & 0xff
Tests:

Code: Select all

;32-bit Int/UInt to 64-bit Int64
MsgBox, % Format("0x{:X} {1}", 0 << 32 >> 32) ;0x0 0
MsgBox, % Format("0x{:X} {1}", 0x7FFFFFFF << 32 >> 32) ;0x7FFFFFFF 2147483647
MsgBox, % Format("0x{:X} {1}", -0x80000000 << 32 >> 32) ;0xFFFFFFFF80000000 -2147483648
MsgBox, % Format("0x{:X} {1}", -1 << 32 >> 32) ;0xFFFFFFFFFFFFFFFF -1
MsgBox, % Format("0x{:X} {1}", 0x80000000 << 32 >> 32) ;0xFFFFFFFF80000000 -2147483648
MsgBox, % Format("0x{:X} {1}", 0xFFFFFFFF << 32 >> 32) ;0xFFFFFFFFFFFFFFFF -1

;32-bit Int/UInt to 32-bit UInt
MsgBox, % Format("0x{:X} {1}", 0 & 0xFFFFFFFF) ;0x0 0
MsgBox, % Format("0x{:X} {1}", 2147483647 & 0xFFFFFFFF) ;0x7FFFFFFF 2147483647
MsgBox, % Format("0x{:X} {1}", -2147483648 & 0xFFFFFFFF) ;0x80000000 2147483648
MsgBox, % Format("0x{:X} {1}", -1 & 0xFFFFFFFF) ;0xFFFFFFFF 4294967295
MsgBox, % Format("0x{:X} {1}", 0x80000000 & 0xFFFFFFFF) ;0x80000000 2147483648
MsgBox, % Format("0x{:X} {1}", 0xFFFFFFFF & 0xFFFFFFFF) ;0xFFFFFFFF 4294967295

;8-bit Char/UChar to 8-bit UChar
MsgBox, % 0 << 56 >> 56 ;0
MsgBox, % 127 << 56 >> 56 ;127
MsgBox, % -128 << 56 >> 56 ;-128
MsgBox, % -1 << 56 >> 56 ;-1
MsgBox, % 128 << 56 >> 56 ;-128
MsgBox, % 255 << 56 >> 56 ;-1
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
aldrinjohnom
Posts: 77
Joined: 18 Apr 2018, 08:49

Re: static_cast<char> command for autohotkey

29 Nov 2019, 04:40

jeeswg wrote:
28 Nov 2019, 20:45
;before:
a += static_cast<unsigned int>(name) << 24

;after (to unsigned 32-bit integer):
a += (name & 0xffffffff) << 24

;for reference (to signed 32-bit integer):
a += (name << 32 >> 32) << 24

maybe

Code: Select all

name[i]
is equivalent to

Code: Select all

asc(substr(name,i-1,1))
jeeswg wrote:
28 Nov 2019, 20:45
;==============================

;before:
name += static_cast<char>(l - (a << 2))

;after (to signed 8-bit integer):
name += (l - (a << 2)) << 56 >> 56

;for reference (to unsigned 8-bit integer):
name += (l - (a << 2)) & 0xff
I think adding a "number to a string" will produce either a "blank" or its like "adding something to zero"



Thank you for helping me out, Can you discuss what static_cast is and how you come up on those translation to autohotkey?
Developer of AJOM's DOTA2 MOD Master, Easiest way to MOD your DOTA2 without the use of internet :lol: . I created this program using Autohotkey thats why I love Autohotkey and the community!

GitHub:
https://github.com/Aldrin-John-Olaer-Manalansan
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: static_cast<char> command for autohotkey

29 Nov 2019, 05:14

When translating code from other languages to AHK, you don't have to translate line for line, AHK can often do things more simply:

Code: Select all

vText := "abc"
Loop Parse, vText
	MsgBox, % A_LoopField " " Ord(A_LoopField)

asc(substr(name,i-1,1))
That's probably about right. Note: Ord is more forwards compatible than Asc.
When translating, remember that AHK uses 1-based indexes, many other languages use 0-based indexes.
(In some situations, NumGet can be useful, but not necessarily in this case.)

To stick more closely to the code, perhaps you could do something like this:

Code: Select all

;q:: ;string to array of ord values
oMap := Object()
vText := "abcde"
Loop Parse, vText
	oMap[A_Index-1] := Ord(A_LoopField)
vOutput := ""
for vKey, vValue in oMap
	vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
return
But note, I haven't inspected the code too closely. That might work as is, or it might need changes.

AHK v1 typically returns a blank string if maths functions/operators fail.
AHK v2 typically throws on errors, and shows an error message.
I'd recommend doing some tests.
Whether you use AHK v1/v2, it might be better to write the code in AHK v2 with #Warn on.

Casting, in simple terms, is converting between types, e.g. the Char -1, as a UChar, is 255.
You can use &, bitwise-and, to essentially chop off the leading hex digits from hex numbers.
<< and >> (bitshift left/right) essentially multiply/divide by powers of 2, with one key exception:
Bitwise operation - Wikipedia
https://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift
in a right arithmetic shift, the sign bit (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand.
AHK stores integers as 64-bit signed integers. If you use <<, and if the sign bit becomes 1, that makes the number negative.
So, you make the number bigger, some numbers become negative, and then you make the number smaller again. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Lpanatt, pond_pop and 324 guests