PDA

View Full Version : Why can we assign the literals to numbers but not to variables (Answer: use nu



bassmaster
02-12-2007, 06:05 AM
Why can we assign the literals to numbers but not to variables ?


·· ($mybyte )
· ($41)






Post Edited (bassmaster) : 2/12/2007 12:58:03 AM GMT

Mike Green
02-12-2007, 06:22 AM
You don't need to cast anything. All values are integers and all method parameters and result are 32-bit integers. Just put "tv.out(mybyte)".

codeking
02-12-2007, 06:23 AM
bassmaster said...
tv.out($mybyte )


The prop tool thinks that $mybyte is a hex number, not a var. Since M,Y,B, and T aren't used in hex, it's giving you that error. tv.hex(mybyte) will print (Whatever the hex of A is) on the screen. I think what your think is that tv.hex changes mybyte into a hex. Hex, binary, numbers, and ASCII are all the same, just visualized in a different form. mybyte := "A", mybyte := $(hex of A), mybyte := (Number of A), and mybyte := %(binary of A) are all the same. Just like : - ) http://forums.parallax.com/images/smilies/smile.gif and : smile : http://forums.parallax.com/images/smilies/smile.gif are the same.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Theron Luhn

"There are two things that are infinate: Human Stupidity and the universe. I'm not sure about the latter." - Mark Twain

Kaio
02-12-2007, 06:23 AM
Use tv.out(mybyte) as in the instruction before.

Use $ only to specify hex values like in tv.out($41).

Kaio
02-12-2007, 06:45 AM
$ is not an operator, it's only an indicator used on constants to specify the used value format.

$ for hex values
% for binary values

If none indicator is given it's a decimal value.

Please have a look at page 158 in propeller manual.

Mike Green
02-12-2007, 06:46 AM
The problem is that you're thinking of "$" and "%" as operators and they're not. They're part of the syntax of numbers. A "$" introduces a hex literal and a "%" introduces a binary literal. The compiler also accepts "%%" to introduce a literal in base 4 (nibble literal?). This is much like the use of a double quote to introduce a string or character constant. The error message is correct.

bassmaster
02-12-2007, 07:03 AM
Ok You are right, my bad, still not understading why I can do this with a number but not a variable byte. see page 312:

Hexadecimal indicator: used to indicate a value is being expressed in hexadecimal
(base-16).

Why can "value" not be the value of a variable byte

Post Edited (bassmaster) : 2/12/2007 12:07:34 AM GMT

bassmaster
02-12-2007, 07:19 AM
Fixed: FYI ( Just expected the compiler to do this for me or have hex dec bin built in like in pbasic)

hex(mybyte)


PUB hex(value)
i = 0

value <<= (8) << 2
repeat 3
·outbyte(i) := (lookupz((value <-= 4) & $F : "0".."9", "A".."F"))
·i := i +1

Post Edited (bassmaster) : 2/12/2007 12:45:52 AM GMT

Kaio
02-12-2007, 07:26 AM
Mike Green said...

All values are integers and all method parameters and result are 32-bit integers.

Mike Green
02-12-2007, 07:41 AM
bassmaster,
Yeah. The operators and statements are so useful and varied that it's easy to forget that Spin is really a fairly low level language, much more like Small C than anything else. It doesn't really have floats, doesn't have structures, no macros. When you compare it to the instruction set, it's pretty much 1:1 for expressions, looser for control statements.
Mike

bassmaster
02-12-2007, 07:48 AM
yep, this is not hard to roll my own, or in this case modify existing hex from tv_text

bassmaster
02-12-2007, 07:55 AM
just found all I needed in numbers.spin!!!!!!!!!!!!!!

PUB ToStr(Num, Format): StrAddr
{{Convert long Num to z-string using Format; returns string address.
PARAMETERS: Num = 32-bit signed value to translate to ASCII string.
Format = Indicates output format: base, size, grouping, etc. See "FORMAT SYNTAX" for more information.
RETURNS: Actual length of output string, not including null terminator.}}
BCXToText(Format >> 19 & 7, Format >> 13 & $3F, Format >> 12 & 1, Format >> 11 & 1, Format >> 5 & $3F, BinToBCX(Num, Format & $1F #> 2 <# 16))
StrAddr := @StrBuf