On Thu, 12 Aug 1999, Suvarna Ayyagari wrote:

> Hi,
> 
> I want to create a byte array using tclblend.The array may contain values 
> from 00 to FF
> 
> in Java I would do it as follows,
> 
> byte b[] = { (byte) 0xFF ,(byte) 0x80};
> 
> Can you please tell me how to implement this in tclblend ?
> 
> For values less than 7F I have no problems
> 
> I say set x [java::new {byte[]} {2} {0x50 0x7F}]
> 
> For values greater than 7F to FF, I have to cast it and I am having problems 
> at that point. Can you please help?
> 
> Thanks
> Suvarna
> 
> 


This is an interesting issue. What is really going on here is that
Java byte type is signed, so it the max and min values for a byte
type in Java are -128 and 127. The value 0x7F is 128 which is one
larger than the maximum size for a byte. So to answer your question,
Tcl Blend is doing the right thing by not allowing you to use an
integer value larger than 127 where Java expects a byte. This
would be the same as the following code, which is not allowed by
the Java compiler.

public class ByteError {
    public static void main(String[] argv) {
        byte b = 0xFF;
    }
}

% javac ByteError.java 
ByteError.java:3: Incompatible type for declaration. Explicit cast needed to convert 
int to byte.
        byte b = 0xFF;



To get things working in the Java code you could need to add
a cast to the byte type (a narrowing conversion).

public class ByteError {
    public static void main(String[] argv) {
        byte b = (byte) 0xFF;
    }
}



The interesting question is, how do we do this same kind of
conversion in Jacl or Tcl Blend code? Clearly, we need a way
to do narrowing conversions as defined by the Java spec. We
would want to do something like this Java example.

public class ByteConversion {
    public static void main(String[] argv) {
        int i = 0xFF;
        Integer I = new Integer(i);
        byte b = I.byteValue();
        System.out.println("b is " + ((int) b));
    }
}

// Should print "b is -1"



# A quick TclJava proc that does this would look like so

proc ByteConversion { byte } {
  set i [expr {int($byte)}]
  set I [java::new Integer $i]
  return [$I byteValue]
}

package require java
puts "b is [ByteConversion 0xFF]"


# This prints "b is -1"



Now that works, but using your own proc to do this conversion
is kind of ugly. I was thinking that a much better way to
do this conversion would be to extend the java::cast command
so that it supported these types of narrowing conversions.
Here was what I was thinking for the command syntax.

set byte [java::cast byte 0xFF]
puts "b is $byte"

# This prints "b is -1"


The only catch is that I am not sure how we would handle
widening conversions. I would think that the only widening
conversion we would even do would be a (int) to (long) but
that does not really do anything in terms of the string rep
because they would be exactly the same.

java::cast int 0xFF

and

java::cast long 0xFF

would both return the string "255".


Does anyone have any opinions on this issue?

later
mo

----------------------------------------------------------------
The TclJava mailing list is sponsored by WebNet Technologies.
To subscribe:    send mail to [EMAIL PROTECTED]  
                 with the word SUBSCRIBE as the subject.
To unsubscribe:  send mail to [EMAIL PROTECTED] 
                 with the word UNSUBSCRIBE as the subject.
To send to the list, send email to '[EMAIL PROTECTED]'. 
A list archive is at: http://www.findmail.com/listsaver/tcldallas/

Reply via email to