|
I have a macro that implements a conversion algorithm. At one point in that algorithm I have to add leading zeros to make sure that a hex value has 8 digits, and not less. What I've done is a little clunky, and it only zeropads from 7 digits to 8, but in my particular case it's sufficient:
Anyone have any suggestions? I see that eval does not have anything obvious here like a |
|
How about There may be some small errors in there as I havn't had tiem to test that. In essence, it prepends a bucn hof zeros, finds the differenc between the lenght of the string with all the zeros and the total lenth you want (I chose 3 in the example), and then uses substr() to return only that many digits from the right end of the string. Not bad. #1) - the 'tonumber' on the end ends up removing all the hard-earned zeros. #2), in my case my values are in hex so I can't use any of the int() stuff. However you're close. This is similar and works very well. | eval myVal="1A09" | eval initialLength=len(tostring(myVal)) | eval myVal="0000000000".tostring(myVal) | eval myVal=substr(myVal,initialLength,10)
(26 Apr '12, 16:13)
sideview ♦
|
|
You can use rex to first pad the number with "enough" zeroes, then to trim it to the length you require. I broke it into two parts since rex's sed mode doesn't seem to like concatenated commands; I don't know whether you consider this cleaner, but it does allow for variable-length numbers. Let me know if this is useful.
1
Awesome. No, I like that better than mine. I'll leave the question open for another couple days in case someone has a third way.
(26 Apr '12, 15:17)
sideview ♦
|