Good day gentlemen, I thought I'd pose a question on something I'm trying to find a better way around.
About a year ago at my former IT job I took the following VC++ code: MSDM | SYSAdmins and ported it to Nim for the activation tool I wrote for myself to make my life easier (didn't want to have to make sure necessary VC++ redistributables were installed every time I needed to run that).
My question pertains to the following code from that:
DWORD FirmwareTableProviderSignature;
FirmwareTableProviderSignature = 'ACPI';
How would that kind of assignment best be duplicated in Nim? I resorted to passing ACPI as a normal string to a function that iterated through each character and assigned the numerical value to the relevant byte in the int32... but it seems like a really dirty way of doing it and surely there must be a better way?
A char array is what is needed, but casts are tricky...
var a = ['A','C','P','I']
var v = (cast[ptr DWORD](addr a))[]
Just simple cast of array literal returns its address instead of content, I don't know how to overcome this.
var v = cast[DWORD](['A','C','P','I'])
The order of characters needed may be the opposite.
This works
var a = cast[ptr uint32](cast[int](['A','C','P','I']))[]
echo a
var b = cast[ptr uint32](cast[int]("ACPI".cstring))[]
echo b
Then maybe a template like this is worth to be in stdlib, such question may arise again.
template strData(str: string, T: expr = int): expr =
cast[T](cast[ptr uint32](cast[int](str.cstring))[])
# testing
import winlean, typetraits
var d = strData("ACPI", DWORD)
echo d, ' ', d.type.name # 1229996865 DWORD
var i = strData("ACPI")
echo i, ' ', i.type.name # 1229996865 int