Jump to content

Ring/Lessons/Check Data Type and Conversion

From Wikibooks, open books for an open world

Check Data Type and Conversion

[edit | edit source]

In this chapter we are going to learn about the functions that can be used for

  • Checking Data Type
  • Checking Character
  • Conversion



Check Data Type

[edit | edit source]

The next functions can be used to check the data type

  • isstring()
  • isnumber()
  • islist()
  • type()
  • isnull()



IsString() Function

[edit | edit source]

Using the IsString() function we can know if the value is a string or not

Syntax:

	IsString(value) ---> 1 if the value is a string or 0 if not

Example:

	see isstring(5) + nl +		# print 0
	    isstring("hello") + nl	# print 1



IsNumber() Function

[edit | edit source]

Using the IsNumber() function we can know if the value is a number or not

Syntax:

	IsNumber(value) ---> 1 if the value is a number or 0 if not

Example:

	see isnumber(5) + nl +		# print 1
	    isnumber("hello") + nl	# print 0



IsList() Function

[edit | edit source]

Using the IsList() function we can know if the value is a list or not

Syntax:

	IsList(value) ---> 1 if the value is a list or 0 if not

Example:

	see islist(5) + nl +		# print 0
	    islist("hello") + nl +	# print 0
	    islist([1,3,5]) 		# print 1

pair: Data Type; Type()

Type() Function

[edit | edit source]

We can know the type of a value using the Type() Function.


Syntax:

	Type(value) ---> The Type as String

Example:

	    see Type(5) + nl +		# print NUMBER
	    Type("hello") + nl +	# print STRING
	    Type([1,3,5]) 		# print LIST



IsNULL() Function

[edit | edit source]

We can check the value to know if it's null or not using the IsNULL() function

Syntax:

	IsNULL(value) ---> 1 if the value is NULL or 0 if not

Example:

	    see isnull(5) + nl +	# print 0
	    isnull("hello") + nl +	# print 0
	    isnull([1,3,5]) + nl +	# print 0
	    isnull("") + nl +		# print 1
	    isnull("NULL")		# print 1



Check Character

[edit | edit source]

The next functions can be used to check character

  • isalnum()
  • isalpha()
  • iscntrl()
  • isdigit()
  • isgraph()
  • islower()
  • isprint()
  • ispunct()
  • isspace()
  • isupper()
  • isxdigit()

IsAlNum() Function

[edit | edit source]

We can test a character or a string using the IsAlNum() Function

Syntax:

	IsAlNum(value) ---> 1 if the value is digit/letter or 0 if not

Example:

	see isalnum("Hello") + nl +	# print 1
	    isalnum("123456") + nl +	# print 1
	    isalnum("ABCabc123") + nl +	# print 1
	    isalnum("How are you")  	# print 0 because of spaces

IsAlpha() Function

[edit | edit source]

We can test a character or a string using the IsAlpha() Function

Syntax:

	IsAlpha(value) ---> 1 if the value is a letter or 0 if not

Example:

	see isalpha("Hello") + nl +	# print 1
	    isalpha("123456") + nl +	# print 0
	    isalpha("ABCabc123") + nl +	# print 0
	    isalpha("How are you")  	# print 0

IsCntrl() Function

[edit | edit source]

We can test a character or a string using the IsCntrl() Function

Syntax:

	IsCntrl(value) ---> 1 if the value is a control character (no printing position) or 0 if not

Example:

	See iscntrl("hello") + nl +	# print 0 
	    iscntrl(nl)			# print 1

IsDigit() Function

[edit | edit source]

We can test a character or a string using the IsDigit() Function

Syntax:

	IsDigit(value) ---> 1 if the value is a digit or 0 if not

Example:

	see isdigit("0123456789") + nl +	# print 1
	    isdigit("0123a") 			# print 0

IsGraph() Function

[edit | edit source]

We can test a character or a string using the IsGraph() Function

Syntax:

	IsGraph(value) ---> 1 if the value can be printed (Except space) or 0 if not

Example:

	see isgraph("abcdef") + nl +	# print 1
	    isgraph("abc def") 		# print 0


IsLower() Function

[edit | edit source]

We can test a character or a string using the IsLower() Function

Syntax:

	IsLower(value) ---> 1 if the value is lowercase letter or 0 if not

Example:

	see islower("abcDEF") + nl + 	# print 0
	    islower("ghi")		# print 1


IsPrint() Function

[edit | edit source]

We can test a character or a string using the IsPrint() Function

Syntax:

	IsPrint(value) ---> 1 if the value occupies a printing position or 0 if not

Example:

	see isprint("Hello") + nl + 		# print 1
	    isprint("Nice to see you") + nl +   # print 1
	    isprint(nl)				# print 0


IsPunct() Function

[edit | edit source]

We can test a character or a string using the IsPunct() Function

Syntax:

	IsPunct(value) ---> 1 if the value is a punctuation character or 0 if not

Example:

	see ispunct("hello") + nl +	# print 0
	    ispunct(",") 		# print 1


IsSpace() Function

[edit | edit source]

We can test a character or a string using the IsSpace() Function

Syntax:

	IsSpace(value) ---> 1 if the value is a white-space or 0 if not

Example:

	see isspace("   ") + nl +	# print 1
	    isspace("test") 		# print 0


IsUpper() Function

[edit | edit source]

We can test a character or a string using the IsUpper() Function

Syntax:

	IsUpper(value) ---> 1 if the value is an uppercase alphabetic letter or 0 if not

Example:

	see isupper("welcome") + nl +	 # print 0 
	    isupper("WELCOME") 		 # print 1


IsXdigit() Function

[edit | edit source]

We can test a character or a string using the IsXdigit() Function

Syntax:

	IsXdigit(value) ---> 1 if the value is a hexdecimal digit character or 0 if not

Example:

	see isxdigit("0123456789abcdef") + nl +  # print 1
	    isxdigit("123z") 			 # print 0



Conversion

[edit | edit source]

The next functions can be used for conversion

  • number()
  • string()
  • ascii()
  • char()
  • hex()
  • dec()
  • str2hex()
  • hex2str()

Number() Function

[edit | edit source]

We can convert strings to numbers using the Number() function or the + operator.

Syntax:

	Number(string) ---> Number
	0 + string ---> Number

Example:

	see number("5") + 5 + nl 	# print 10
	see 0 + "10" + 2		# print 12

String() Function

[edit | edit source]

We can convert numbers to strings using the String() function or the + operator.

Syntax:

	String(number) ---> String
	"" + number ---> String

Example:

	see string(5) + 5 + nl 		# print 55
	see "" + 10 + 2			# print 102


Ascii() Function

[edit | edit source]

We can get the ASCII code for a letter using the Ascii() function

Syntax:

	Ascii(character) ---> ASCII Code

Example:

	See ascii("m") + nl + 	# print 109
	    ascii("M") 		# print 77

Char() Function

[edit | edit source]

We can convert the ASCII code to character using the Char() function.

Syntax:

	Char(ASCII Code) ---> character

Example:

	See char(109) + nl + 	# print m
	    char(77) 		# print M


Hex() Function

[edit | edit source]

We can convert decimal to hexadecimal using the Hex() function.

Syntax:

	Hex(decimal) ---> hexadecimal

Example:

	See hex(10) + nl + 	# print a
	    hex(200)		# print c8

Dec() Function

[edit | edit source]

We can convert hexadecimal to decimal using the Dec() function

Syntax:

	Dec(hexadecimal) ---> decimal

Example:

	See dec("a") + nl + 	# print 10
	    dec("c8")		# print 200

Str2hex() Function

[edit | edit source]

We can convert string characters to hexadecimal characters using the Str2hex() function.

Syntax:

	Str2hex(string) ---> hexadecimal string

Example:

	See str2hex("hello")  	# print 68656c6c6f


Hex2str() Function

[edit | edit source]

We can convert hexadecimal characters to string using the Hex2str() function

Syntax:

	Hex2Str(Hexadecimal string) ---> string

Example:

	See hex2str("68656c6c6f")  	# print hello