Initializing byte array

I’m trying to create a 256 element byte array as below:

static trailingZeroTable() as byte = array( _ -25, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0)

I am getting a type mismatch error “expecting byte but received int32”. From looking at the documentation it states that you can convert the first element and the rest will follow. So I tried the following

static trailingZeroTable() as byte = array( _ byte(-25), 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, _ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0)

with similar result. I tried using enclosing the first element with ctype as well with no luck. If I surround every element with ctype it works. Is there a simpler way to force all the elements to be a certain value without casting every one?

Make your own ‘Array’ function that’s typed to Byte

Function ArrayByte(ParameArray values As Byte) As Byte() return values End Function

Is it just that the first element is a negative number?
in byte storage (8bits) , -25 is the same as 230

Create a method and use that.

Function ByteArray ( ParamArray values() as byte) as byte
  Return values
End Function 

Thanks Will and Kem! Using a method as recommended works well. I have hit this problem several times with a need to force a type and this solution works well.

[quote=212286:@Jeff Tullin]Is it just that the first element is a negative number?
in byte storage (8bits) , -25 is the same as 230[/quote]

The winner !
Byte is an analog for Uint8
see http://documentation.xojo.com/index.php/Byte

Norman, you are correct I should have been using int8 instead. However, I still get the same error using int8 and will need to use the method suggested by Will and Kem.

curious
these fail in the same fashion

static foo as uint8 = Uint8(-25)
static foo as byte = byte(-25)

but these

static foo as uint8 = ctype(-25,uint8)
static foo as byte = ctype(-25,byte)

work, but not for the array
seems like that’d be a reportable bug

And it seems to have nothing to do with a negative value either as the following code will demonstrate:

[code] static foo1 as uint8 = Uint8(25) // this creates mismatch error
static foo2 as byte = byte(25) // this creates mismatch error
static foo3 as int8 = int8(25) // this creates mismatch error

static foo4 as uint8 = ctype(25,uint8) // no error here
static foo5 as byte = ctype(25,byte) // no error here
static foo6 as int8 = ctype(25, int8) // no error here

static foo7() as uint8 = array(uint8(25)) // this creates mismatch error
static foo8() as byte = array(byte(25)) // this creates mismatch error
static foo9() as int8 = array(int8(25)) // this creates mismatch error

static foo10() as uint8 = array(ctype(25,uint8)) // no error here
static foo11() as byte = array(ctype(25, byte)) // no error here
static foo12() as int8 = array(ctype(25, int8)) // no error here

static foo13() as uint8 = array(ctype(25, uint8), 1) // this creates mismatch error
static foo14() as byte = array(ctype(25, byte), 1) // this creates mismatch error
static foo15() as int8 = array(ctype(25, int8), 1) // this creates mismatch error

static foo16() as uint8 = array(ctype(25, uint8), ctype(1, uint8)) // no error here
static foo17() as byte = array(ctype(25, byte), ctype(1, byte)) // no error here
static foo18() as int8 = array(ctype(25, int8), ctype(1, int8)) // no error here[/code]

Casting with “ctype” works with the exception of multiple array values where you are forced to cast every value. Seems like compiler wants to convert to a uint32 value as a minimum maybe??

Added feedback case

I played around with this a bit myself and your bug report summarizes it pretty well
I’m sure Joe can sort this out once he’s had a chance to come up for air :slight_smile:
I think he’s swimming around in Windows internals and linkers and such at the moment

It seems this problem still exists 2 years later. :frowning:

byte arrays are sufficiently rare in Xojo that i’d ask what it is you need one for ?

Consider this method:

[code]// Returns next line of data from the socket. Assumes it is ascii (as it ought to be).
//
// Used when wanting a response line from a mail host.

dim linebytes() as byte, errcode As Integer, mb as xojo.Core.MemoryBlock

line = “”
errcode = me.gets (linebytes)

if (linebytes.Ubound>-1) then
mb = new xojo.Core.MemoryBlock (linebytes)
line = Xojo.Core.TextEncoding.ASCII.ConvertDataToText(mb, true).TrimRight ()
end if

return errcode[/code]

I’m forced into using a byte array due to the methods available. I want to end up with a line of type Text with as little overhead as possible, and which is guaranteed to be ASCII (remote hosts are such liars).

The gets() method has to take bytes from the socket and look for end-of-line delimiters. I soon found that building up a line of Text from individual characters by concatenation is a very slow business in Xojo, so whereas the above method is just used for simple dialog with a mail host, once I’m expecting a block of as it might be a few megabytes of text, I’d better gather them into an array of lines of text and join them once I’ve got it all. That way I was able to reduce the time taken to receive the few meg of text from several minutes down to several seconds.

if internally gets used a xojo.core.mutablememoryblock creating a memoryblock to pass back to you is simple
you might be able to avoid the byte array entirely

What would really be nice would be this method:

http://uk1.php.net/manual/en/function.fgets.php

which works with any file or socket. It’s probably more reliable than mine :slight_smile:

Already exists if you’re using the classic socket routines, binary stream etc
http://documentation.xojo.com/index.php/Readable.Read
implemented by http://documentation.xojo.com/index.php/Readable

For the new framework it also exists
xojo.io.binarystream - http://developer.xojo.com/xojo-io-binarystream$Read
xojo.net.tcpsockect - http://developer.xojo.com/xojo-net-tcpsocket$ReadData

httpsockets dont have this because their data is far more structured

None of those methods terminates on a newline. All the data I’m reading is line-oriented.

Since sockets are "line orient"ed I’d read the data into a mutable memoryblock as a buffer and the use its methods to find the line ending and return a new memoryblock out of that

thats generally how to handle sockets anyways because they may break your data into chunks that are not “one line”

Because the Xojo Reference says that I can use one. That should be cause enough to make it work.

creating one is easy

dim myArray() as byte

this one happens to be empty

initializing it to specific values is going to depend on what you have for data