Jump to content

[LE] Bitshifts or scripts to convert decimal to hex?


Recommended Posts

I'm working on a mod where I cast a spell on an NPC and it writes a "placeatme" script for them to disk using the papyrus user log functions. Ultimately I'll have it create script commands to equip them with whatever inventory they currently have. The purpose of the mod is so I can quickly get favorite companions setup between playthroughs. I know about the limits of placeatme.

 

I need to convert the decimal form ID's for the actors to a hex, e.g. -1627386526 --> 9F000D62, so I can write that out to the file (e.g. "player.placeatme 9F000D62". I've got a simply modulo formula that works for positive integers (< 0x80000001) and currently working on getting it to calc right with negative numbers (> 0x80000001).

 

I wondering is there some obscure script that already does this? Or is there an easier way to do this with bitshifts? I'm not that savvy with bitshifts and I see that there are ways to simulate them in papyrus.

Link to comment
Share on other sites

If you're using SKSE, I believe you can do a Math.LogicalAnd with #F (1111 in binary), convert that to a character & store it in a string, then use Math.RightShift to move the original number over four bits, convert that & insert it at the front of the string, and repeat as needed. This will not be a particularly fast process but it should work; I've used a version of it in Factorio to create memory cells.

Link to comment
Share on other sites

If you're using SKSE, I believe you can do a Math.LogicalAnd with #F (1111 in binary), convert that to a character & store it in a string, then use Math.RightShift to move the original number over four bits, convert that & insert it at the front of the string, and repeat as needed. This will not be a particularly fast process but it should work; I've used a version of it in Factorio to create memory cells.

 

Thank you! This is gives me another path to try, although I still have to wrap my head around exactly what bitshifts are doing.

Link to comment
Share on other sites

 

If you're using SKSE, I believe you can do a Math.LogicalAnd with #F (1111 in binary), convert that to a character & store it in a string, then use Math.RightShift to move the original number over four bits, convert that & insert it at the front of the string, and repeat as needed. This will not be a particularly fast process but it should work; I've used a version of it in Factorio to create memory cells.

 

Thank you! This is gives me another path to try, although I still have to wrap my head around exactly what bitshifts are doing.

 

 

 

For example, if you have the binary sequence 1011 (i.e, 11), and shift it right two bits, you get 0010, or 2. If you shift it left two bits, you get 101100, or 44. In essence what you're doing is reading off the last four bits, then shifting the entire sequence right so that those bits vanish and you get four new bits (that were originally bits 8, 7, 6, and 5) in their place, then repeating.

 

So, for example, take an integer represented in hex as 80000002. In binary that is 0100 0000 0000 0000 0000 0000 0000 0010. By LogicalAnd with binary 1111 (hex F), you get 0010 out, or 2, which you store in a string. Then you shift the whole sequence right four bits, to get 0000 0100 0000 0000 0000 0000 0000 0000. Now the LogicalAnd will give you 0, so you insert that at the front of the string, for '02'. Repeat this until you get to the binary sequence 0000 0000 0000 0000 0000 0000 0000 0100, which will LogicalAnd to 0100, or '8', and gets you back your hex number as a string. You don't even need to know in advance how many iterations to loop through; just use a conditional test on the bit-shifted number to see if it is zero. If it is, you can stop, because it won't change any more.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...