Using Loops Log Out | Topics | Search
Moderators | Edit Profile

X-Ways Support Forum » Advanced Features » Using Loops « Previous Next »

Author Message
Top of pagePrevious messageNext messageBottom of page Link to this message

Jared Myers
Username: jmyers36

Registered: 1-1997
Posted on Wednesday, Aug 5, 2009 - 19:13:   

I am trying to use Loops with interpreting variable length data. The scenario is I have 32 byte entries starting with a known hex value. What I would like to do is if the value equals = hex 0x03 then
move 1
char16[15] "15 Characters"
but if the value equals = hex 0x01 or hex 0x2 then start back at the beggining of the template and if value equals = hex 0x00 to end.

I have been playing with this for a while and at this point I don't know if what I am trying to do is even possible.

Any help would be greatly appreciated.

Jared
Top of pagePrevious messageNext messageBottom of page Link to this message

Douglas S. Lacey
Username: thx99

Registered: 11-2005
Posted on Wednesday, Aug 5, 2009 - 19:32:   

You could set up three "IFEQUAL" statements within a larger loop:

{(start of loop}

IFEQUAL <variable> 0x00
ExitLoop
Else
EndIf

IFEQUAL <variable> 0x03
Move 1
...
JumpTo LoopRestart
Else
EndIf

IFEQUAL <variable> 0x02
...
JumpTo LoopRestart
Else
EndIf

IFEQUAL <variable> 0x01
...
JumpTo LoopRestart
Else
EndIf

Label LoopRestart
}(end of loop)

This way, no matter what value matches, it either exits the loop (when 0) or jumps past the other IFEQUAL sections and restarts the loop. Not sure if this fits with what you're trying to do but I hope it helps nonetheless.
Top of pagePrevious messageNext messageBottom of page Link to this message

Jared Myers
Username: jmyers36

Registered: 1-1997
Posted on Wednesday, Aug 5, 2009 - 21:10:   

Thanks Douglas...your idea pointed me in the right direction

I set it up like so.

char16[15] "First 15"
{
IFEQUAL hex 0x01
move 1
char16[15] "Next 15"

IFEQUAL hex 0x02
ExitLoop
Else

IFEQUAL hex 0x03
ExitLoop
Else

IFEQUAL hex 0x00
ExitLoop
Else

IFEQUAL hex 0x01
JumpTo LoopRestart
Else
EndIf

}[18]

Unfortunately I can't get anything after the first { bracket to read properly. I am not sure if I am using the Else and EndIf correctly. Basically I know that there will at least one char16[15] and then depending on the length of the file name it could be more entries. I am trying to make it so that if there is another entry that it gets read and if not that the template ends.

Jared
Top of pagePrevious messageNext messageBottom of page Link to this message

Douglas S. Lacey
Username: thx99

Registered: 11-2005
Posted on Wednesday, Aug 5, 2009 - 21:49:   

A few observations:

- Each IFEQUAL statement needs an Else statement and must be closed with an EndIf statement. For example:

IFEQUAL hex 0x01
move 1
char16[15] "Next 15"
Else
EndIf

- The "JumpTo" command is used in conjunction with a "Label" command; JumpTo jumps immediately to another point in the script identified by the Label command, which must also be present. FYI, the term "LoopRestart" is one I assigned arbitrarily as a name for the label. It's not a WinHex command, just in case you thought that it was.

- The ExitLoop command does just that, it exits the loop at that point and does not start the process over. Do you mean to exit the loop if the value is 0x00, 0x02, or 0x03?

- "char16[15]" isn't a WinHex script command I'm familiar with. What did you intend for that command to do?

- With this portion of the script, you are not reading any data to compare with your IFEQUAL commands. The way you've written the IFEQUAL statements, "hex" is a variable that needs to be read before you can make a comparison. Otherwise, "hex" will always be zero.

- You've indicated that you want the loop to run 18 times. Are there 18 32-byte entries, or could there be fewer or more in any given file? Are the 32-byte entries one after another in the file you're processing? If not, you'll have to add commands to move the current position to the start of the next entry or else you'll continue reading the same one over and over.

I hope I'm not confusing you more than helping. Script writing can be tricky and it's often hard to fully describe to someone what you're trying to accomplish.

If you want to take the conversation off-board, feel free to email me at dltech@cox.net.

Doug
Top of pagePrevious messageNext messageBottom of page Link to this message

Jared Myers
Username: jmyers36

Registered: 1-1997
Posted on Thursday, Aug 6, 2009 - 19:22:   

Doug,

The char16 [15] is to read 15 Unicode characters, it has been working in the template. As far as the ExitLoop command, yes I am trying to exit the loop if the hex value is 00,02,or 03. Basically I have a 32 byte entry that begins with 0x01 then I need to move one and read 15 Unicode characters. Then on the next line determine if there is a 0x01 and if so move 1 and then read the next set of 15 Unicode characters. As far as the 18 - 32 byte entries there could be up to 18 entries, but not always, just depending on the file name it could be anywhere from 1 entry to 18. I know that after the 15 Unicode characters the only possible hex values are 00,01,02,and 03. So if it's 01 I want it to continue reading the 15 Unicode characters until it hits a 00,02, or 03.

I have rewritten the last part as follows and it still isn't working:

char16[15] "First 15"
{
IFEQUAL hex 0x01
move 1
char16[15] "Next 15"
Else
EndIf

IFEQUAL hex 0x02
ExitLoop
Else
Endif

IFEQUAL hex 0x03
ExitLoop
Else

IFEQUAL hex 0x00
ExitLoop
Else
EndIf

IFEQUAL hex 0x01
JumpTo LoopRestart
Else
EndIf

}[18]
Top of pagePrevious messageNext messageBottom of page Link to this message

Douglas S. Lacey
Username: thx99

Registered: 11-2005
Posted on Thursday, Aug 6, 2009 - 20:11:   

Jared,

A few more observations:

- I'm still uncertain about the char16[15] command as I've never used it and am not familiar with its syntax, nor is it listed in the Help section for Scripts.

- You're missing an EndIf for the 0x03 IfEqual statement.

- The last IfEqual statement is unnecessary. You can scrap the whole section, add JumpTo LoopRestart into the 0x01 IfEqual area above after the char16[15] "Next 15" line, and add the line Label LoopRestart after the 0x00 IfEqual section.

- Again, "hex" is a variable that, at least in this part of your script, is not being assigned a value by a Read command. In other words, if your current position within the file is at the byte you're determining the value of, then you need to add a Read hex 1 command before the IfEqual hex 0x01 line.

- When you write "then I need to move one", I presume that you mean one byte?

- From your description, after you read the 15 Unicode characters, you may need to move a certain number of bytes in order to set the current position at the start of the next line before running the loop again. This would go right before the end of the loop.

So, here's a possible revision, though I can almost guarantee that it won't 100% correct (this assumes that your current position within the file is alread set to the byte you want to read; don't include my comments in the {}):

---

char16[15] "First 15"
{
Read hex 1 {reads in the byte}

IFEQUAL hex 0x01
move 1 {this may be unnecessary if the 15 Unicode characters immediately follow the single byte above since the Read command moves you forward already}
char16[15] "Next 15"
JumpTo LoopRestart {skip to end of loop}
Else
EndIf

IFEQUAL hex 0x02
ExitLoop
Else
Endif

IFEQUAL hex 0x03
ExitLoop
Else
EndIf

IFEQUAL hex 0x00
ExitLoop
Else
EndIf

Label LoopRestart

Move X {where X is the number of bytes you need to move to get to the next one-byte value you want to read; could be zero if it immediately follows the 15 Unicode characters}

}[18]

---

If 0x01 is really the only value that's important, then you could simplify the loop to the following:

char16[15] "First 15"
{
Read hex 1 {reads in the byte}

IFEQUAL hex 0x01
move 1 {this may be unnecessary if the 15 Unicode characters immediately follow the singe byte above since the Read command moves you forward already}
char16[15] "Next 15"
Else
ExitLoop
EndIf

Move X {where X is the number of bytes you need to move to get to the next one-byte value you want to read; could be zero if it immediately follows the 15 Unicode characters}

}[18]

Doug
Top of pagePrevious messageNext messageBottom of page Link to this message

Jared Myers
Username: jmyers36

Registered: 1-1997
Posted on Thursday, Aug 6, 2009 - 21:19:   

Doug,

I rewrote the loop to say
char16[15] "First 15" - this is a variable declaration in the template and does not work correctly inside the Loop as you pointed out.

{
hex 1 - I get an error message with the "Read" in front of hex

IFEQUAL hex 0x01
char16[15] - I now know that this produces 15 lines of unicode hex (example 0041 0042 0043). What I am wanting to do is to show the Unicode Characters for the Unicode values (example ABC).

Else
ExitLoop
EndIf
}
End

Thanks for your help so far on this Doug

Add Your Message Here
Post:
Username: Posting Information:
Only registered users may post messages here, i.e. you need to have an account.
Password:
Options: Enable HTML code in message
Automatically activate URLs in message
Action:
Forum operated by X-Ways Software Technology AG.