Zero Fill (filled zeros or leading zeros)

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
mba_wiz
Newbie
Posts: 2
Joined: Tue Sep 30, 2008 5:48 am
Location: Portland OR

Zero Fill (filled zeros or leading zeros)

Post by mba_wiz » Tue Sep 30, 2008 6:46 am

Thank you M.Scheduler staff for a great piece of software, but I feel like I've been working on this project for decades! Here is the challenge: The code below works well, but does not conform to the specific data format that I-and our SQL Database- wants.

Code: Select all

Random>9999,var1
Let>var1=var1

VBSTART
VBEND
Let>DecVal=%var1%
VBEval>Hex(%DecVal%),HexVal1

Random>99,var2
Let>var2=var2

VBSTART
VBEND
Let>DecVal=%var2%
VBEval>Hex(%DecVal%),HexVal2


Random>9,var3
Let>var3=var3

VBSTART
VBEND
Let>DecVal=%var3%
VBEval>Hex(%DecVal%),HexVal3


Let>WLN_NOCRLF=0
WriteLn>C:\randomstr.txt,nWLNRes,;A1%Hexval1%A2%hexval2%A3%hexval3%
This code will generate something like this: ;A12649A23FA36.

I, and SQL need the string to be ;A1#####A2#####A3###. As you have probably guessed, the A1,A2,and A3 characters are field identifiers that are required for SQL. The bytes immediately after these identifiers are the data (in Hexadecimal notation).

I need to add leading zero's to each data field so that the field complies with a specialized validation procedure we have. This is not as easy as it looks (i think). It is impossible to just add zero's to the string because of the Random numbers and the subsequent hex conversion.

Is it possible to code an "IF" statement to accomplish the correct format? Something like...IF the hex value is less than 5 characters long, THEN fill with "leading" Zero's from the left till there are five characters exactly.

I don't know where to begin.

I hope someone understands what I'm looking for javascript:emoticon(':?')

Thanks in advance,
Mba.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Sep 30, 2008 7:45 am

Just use VBScript's Right function. E.g.:

Right("00000" & some_string,5)

Will yield a 5 character string with leading zeros.

So, try this:

Code: Select all

VBSTART
VBEND

Let>testData=A123

//get the prefix
Mid>testData,1,2,prefix

//Get the rest
Length>testData,lentestData
Mid>testData,3,lentestData,number_part

//add leading zeros
VBEval>Right("00000" & "%number_part%",5),number_part

//Put back together
Let>field=%prefix%%number_part%
MessageModal>field
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mba_wiz
Newbie
Posts: 2
Joined: Tue Sep 30, 2008 5:48 am
Location: Portland OR

Zero Fill (filled zeros or leading zeros)

Post by mba_wiz » Tue Sep 30, 2008 7:00 pm

Hi Marcus, Thanks for the quick response, and not to mention the suggested VBscript. With a little reading, many messageModals and minor tweaking the script works wonders.

For other readers, here is the revised script with only the "A1" values.

Code: Select all

Random>10,var1
Let>var1=var1

VBSTART
VBEND
Let>DecVal=%var1%
VBEval>Hex(%DecVal%),hexval1
Let>testData=A1%hexval1%

Mid>testData,1,2,prefix

Length>testData,lentestData

Mid>testData,3,lentestData,number_part

VBEval>Right("0000" & "%number_part%",4),number_part
Let>A1field=%prefix%%number_part%

Let>WLN_NOCRLF=0
WriteLn>C:\randomhexstr.txt,nWLNRes,;%A1field%
Thanks again, and rest assured I will posting more questions as I grasp the capabilities of your fine software.

Mba.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts