Page 1 of 1 1
Topic Options
#201252 - 2010-12-22 08:01 AM Chr0 and Null
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Okay... I know that Chr(0) is a no no in Kix... but with the following code, I'm wondering if it is working. I also tried creating a Null function. Would someone smarter than me look at this and provide your thoughts?

 Code:
? vartypename($chr0)
? val($chr0)
$chr0=chr0()
? vartypename($chr0)
? "[" + $chr0 + "]"
? val($chr0)
? "__________________________"
? vartypename($null)
? val($null)
$null=null()
? vartypename($null)
? "[" + $null + "]"
? val($null)


 Code:
function Chr0()
    dim $sc
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.addcode('chr0=chr(0)')
    $sc.run
    $chr0=$sc.codeobject.chr0
endfunction

function Null()
    dim $sc
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.addcode('nul=null')
    $sc.run
    $null=$sc.codeobject.nul
endfunction



Top
#201254 - 2010-12-22 08:47 AM Re: Chr0 and Null [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Add these to the list to check out too...

 Code:
function Cbyte($val)
    dim $sc
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.addcode('by=cbyte(' + $val + ')')
    $sc.run
    $cbyte=$sc.codeobject.by
endfunction

function CBool($val)
    dim $sc
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.addcode('bool=cbool(' + $val + ')')
    $sc.run
    $cbool=$sc.codeobject.bool
endfunction

Top
#201255 - 2010-12-22 09:15 AM Re: Chr0 and Null [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, I don't contend that I'm smarter - better looking perhaps.

NULL and CHR(0) are not actually no-nos in KiXtart, it's just that there is limited support in native variable types and no functions that you can manipulate them with complete success.

You can use KiXtart variable references to store NULL and CHR(0) and pass them on to automation objects that know how to handle them, just be aware that it is futile to use them with KiXtart intrinsics except in a very limited sense.

NULL is probably the easier to handle as it is a simple entitiy that is more of a state than a value. In KiXtart a NULL acts like an undefined value so behaviour will in most cases be what you'd expect.

Chr(0) however is a problem, and your example code demonstrates it quite well. When you catenate the strings in '? "[" + $chr0 + "]"' it's not actually doing what you think it is. It does not see $chr0 as an object with a Chr(0) in it, it sees it as a zero length string. You can demonstrate this better by trying for example to create and use a string like "A"+Chr(0)+"B"+Chr(0)+"C"

Top
#201256 - 2010-12-22 03:11 PM Re: Chr0 and Null [Re: Richard H.]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Dumb Question. In your example, what would chr(0) produce if it was working properly? What kind of variable would it be? (I've never been exposed to a language that supported it)

Do you think the CByte() and CBool() have any use?

Top
#201257 - 2010-12-22 03:37 PM Re: Chr0 and Null [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It's not a dumb question at all, and the answer is of course "it depends".

A string in some languages (C for example) doesn't actually exist a data type. "Strings" are represented simply as a fixed length buffer which contains a series of 1 or more bytes that form the string.

The problem then is how do you know where the "string" ends?

The solution was to use zero as a string terminator, and is where the "SZ" acronym comes from that you'll see in various places including registry types.

KiXtart has the same limitation, so Chr(0) cannot be a string or part of a string because it terminates the string.

Other languages include the string length as part of the object, so there is no need for a string terminator character and the problem is avoided.

The answer to your question about the type of variable is in this case probably a "byte array" - which KiXtart cannot handle natively though you can still hold it in KiXtart variable.

 Originally Posted By: Allen
Do you think the CByte() and CBool() have any use?


Yes. Probably \:\)

Routines which convert byte arrays to and from KiXtart arrays of integers would definately have been useful to solve problems raised in the past with things like internal representatin of dates, numbers, network addresses and SIDs.

Top
#201258 - 2010-12-22 03:52 PM Re: Chr0 and Null [Re: Richard H.]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Hmmmmm

"A"+ Chr0 +"B"+Chr0 + "C" ;produces ABC
"A"+ Chr(0) +"B" + Chr(0) + "C" ; also produces ABC

Now is kixtart just ignoring the Chr(0)?

When you say terminate the string in an environment where it recognizes chr(0)... would that mean something like
"A"+ Chr0 +"B"+Chr0 + "C" ; produces only A

sigh... (the less better looking one ;\) )

Top
#201259 - 2010-12-22 04:21 PM Re: Chr0 and Null [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
"A"+ Chr0 +"B"+Chr0 + "C" ;produces ABC
"A"+ Chr(0) +"B" + Chr(0) + "C" ; also produces ABC


Yes of course, because both of these are exactly the same as:
 Code:
"A" + "" + "B" + "" + "C"


 Quote:
When you say terminate the string in an environment where it recognizes chr(0)... would that mean something like
"A"+ Chr0 +"B"+Chr0 + "C" ; produces only A


Yes that's it. If you created the data in another language and then tried to use it in KiXtart only the "A" would be present in the string.

Here is a C program that you should be able to follow:
 Code:
#include <stdio.h>
#include <string.h>

int main()
{
 /* Declare a buffer of 1024 chars */
 char buffer[1024];

 /* Copy a "string" to the buffer */
 strcpy(buffer,"Hello world");

 /* Display the buffer contents */
 printf("Buffer contains '%s'\n",buffer);

 /* Put a Chr(0) into the buffer */
 buffer[5]=0;

 /* Now display the contents again */
 printf("After inserting Chr(0) start of buffer contains '%s'\n",buffer);
 printf("Buffer+6 contains '%s'\n",buffer+6);
}


And here is the output:
 Quote:
Buffer contains 'Hello world'
After inserting Chr(0) start of buffer contains 'Hello'
Buffer+6 contains 'world'

Top
#201260 - 2010-12-22 04:29 PM Re: Chr0 and Null [Re: Richard H.]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Thanks Richard. Much clearer now.
Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 456 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.062 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org