rounding off to the next nearest 0x100

Programming related discussions related to game research
happydance
Posts: 81
Joined: Sun Jul 10, 2016 11:07 am

rounding off to the next nearest 0x100

Post by happydance »

not sure what it is really is called but id like to now how to achieve this by bms script

like if the size it 0xF2DA it will output 0xF300
or if the size it 0x3A2DA it will output 0x3A300


really not sure how to do this

this is my closest miserable attempt to make one but it's still wrong...


set SIZE long 0xF2DA
xmath TEST2 "(SIZE & 0xF00) + 0x100"
xmath TEST3 "(SIZE & 0xF000) + TEST2"


it will output 0xF300 but if I input like 0x2F300 it will still output 0xF300
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: rounding off to the next nearest 0x100

Post by aluigi »

math SIZE x 0x100

Yeah it's really that easy :D
Acewell
Posts: 706
Joined: Fri Aug 08, 2014 1:06 am

Re: rounding off to the next nearest 0x100

Post by Acewell »

here try this :D

Code: Select all

set SIZE long 0x3A2DA
print "%SIZE|hex% :size"
xmath CHECK "SIZE % 256"
print "%CHECK|hex% :check"
if CHECK != 0
   xmath SIZE "(SIZE - CHECK) + 256"
   print "%SIZE|hex% :new size"
endif
print "%SIZE|hex% :final size"
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: rounding off to the next nearest 0x100

Post by aluigi »

@Acewell
Why? Isn't "math SIZE x 0x100" better?
Vuze
Posts: 16
Joined: Sun Dec 28, 2014 3:02 pm

Re: rounding off to the next nearest 0x100

Post by Vuze »

.
Could you kindly paste the source code for the math x command? I'd like to port it to a C# tool I'm working on.
Lucky coincidence this showed up on my twitter feed yesterday when I ran into the problem :mrgreen:
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: rounding off to the next nearest 0x100

Post by aluigi »

var1 is the number to round and var2 is the alignment (for example 0x100):

Code: Select all

if(var2 && ((u_int)var1 % (u_int)var2)) { var1 += (var2 - ((u_int)var1 % (u_int)var2)); }
Vuze
Posts: 16
Joined: Sun Dec 28, 2014 3:02 pm

Re: rounding off to the next nearest 0x100

Post by Vuze »

Thank you!
Acewell
Posts: 706
Joined: Fri Aug 08, 2014 1:06 am

Re: rounding off to the next nearest 0x100

Post by Acewell »

aluigi wrote:Isn't "math SIZE x 0x100" better?

oh wait, that "x" is a special operator isn't it?
i was thinking you meant "times", like 2 x 2 = 4 :)

first time i saw that "x" used, you never mentioned this solution to my "maintain a multiple of 4" problem here :P
viewtopic.php?f=13&t=3952

both ways work, but can you round down with your solution?
ah i see you have a "y" operator for that :)
Last edited by Acewell on Mon Apr 24, 2017 9:47 pm, edited 2 times in total.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: rounding off to the next nearest 0x100

Post by aluigi »

Eh, when I see long posts I can't focus on them and, in that case I just focused only on the script you posted almost ignoring the rest :)

Anyway you can find all the operators in quickbms.txt, 'x' is there.
happydance
Posts: 81
Joined: Sun Jul 10, 2016 11:07 am

Re: rounding off to the next nearest 0x100

Post by happydance »

thanks for the reply! gonna try some of the examples you guys gave.