Integer Validation

Hi

Using the below logic to check whether the value is integer or not and converting the value to decimal if not.

int_chck = sprintf(substr($i, 1, 2))
    o_cnt = 'if [ isnumber $int_chck ]; then echo $int_chck; else echo $((0x$int_chck)); fi'

Thanks,
Dines

The above mix of shell and awk code is not valid syntax in either.

I fixed this more than a half hour before you started this thread in my suggested awk code in your other thread after you said you wanted to treat two digits as decimal if both digits are decimal and as hex otherwise (instead of always treating the two characters as hex digits):

		if((cnt = substr($i, 1, 2)) ~ "[0-9][0-9]") {
			# Use decimal if both characters are decimal digits.
			cnt = cnt + 0
		} else {# Otherwise, use hexadecimal.
			cnt = sprintf("%d", "0x" cnt) + 0
		}

Is this not working for you?

In a shell (any shell but csh/tcsh) you can use

case $val in
[0-9]|[0-9][0-9]*)
;;
*) val=`printf "%d" 0x"$val"`
;;
esac

I suppose a few regex would do it even more neatly. If you parse zero, then it should be optional white space, optional sign, optional white space and then zero. If you parse not zero, does the parse need to run to the end of the string? Are parentheses OK for negative? What about commas, one radix symbol with, or without, nonzero digits after? There are so many different 'rules'!

This is all related to a small part of another thread: Replace Special Character With Next Present Byte

This is a very specific awk (not shell) issue where exactly two characters are to be converted to a number using base 10 if both characters are decimal digits and using base 16 if one or both characters are hexadecimal digits.

I'm closing this thread, because the issue was resolved in the other thread.