CreateBankStream Littleendian

BlitzMax Forums/BlitzMax Programming/CreateBankStream Littleendian

BLaBZ(Posted 2013) [#1]
How would I convert a bankstream to be littleendian?

The following doesn't work for me -

file=LittleEndianStream(CreateBankStream(bank))


Thanks


BLaBZ(Posted 2013) [#2]
nvm...


Floyd(Posted 2013) [#3]
This works for me:
b:TBank = CreateBank( 4 )

PokeInt b, 0, $12345678

Print 
Print Hex( PeekInt( b, 0 ) )

Local TS:TStream = BigEndianStream( CreateBankStream( b ) )

Print Hex( ReadInt( TS ) )

I'm on an Intel PC, so little-endian is the default. The program converts the stream to big-endian and the displays these numbers:

12345678
78563412

I guess your second post implies this was already solved.


BLaBZ(Posted 2013) [#4]
Yes and no, that part works but I'm getting an exception later when calling ReadLong(). Works when using OpenStream but not from a BankStream.


Floyd(Posted 2013) [#5]
You're not running off the end of the bank are you? ReadLong needs eight bytes available.

Or maybe it's a data alignment issue. If you are on a PowerPC then maybe a Long needs to be eight-byte aligned.


BLaBZ(Posted 2013) [#6]
A more complete story of what I'm attempting to do...
http://blitzmax.com/Community/posts.php?topic=101238

I don't see why this would be any different from OpenStream.


Floyd(Posted 2013) [#7]
I can't think of anything but memory alignment, which would not be an issue with a stream, but might be with bank.

What if you read the Long a byte at a time and build the result manually:

Function MyReadLong:Long( stream:TStream )
	Local b:Byte, val:Long, n:Int
	For n = 1 To 8
		b = ReadByte( stream )
		val = ( val Shr 8 ) | ( Long(b) Shl 56 )
	Next
	Return val
End Function



BLaBZ(Posted 2013) [#8]
Ok I figured out why this isn't working, silly but I still don't have a solution

LoadBank needs a URL, I was passing the contents in AS the URL so the bank wasn't storing properly.

So the question is how to I "create a url" for a string that I can pass into LoadBank.


TomToad(Posted 2013) [#9]
You could use CStrings and CreateStaticBank. Just remember that the bank needs to be 1 element longer than the string length because of the 0 at the end.

SuperStrict
Local Numbers:String = "1234"
Local MyString:Byte Ptr = Numbers.ToCString()
Local Bank:TBank = CreateStaticBank(MyString,Numbers.Length+1)
For Local i:Int = 0 To 3
	Print PeekByte(Bank,i)
Next
MemFree MyString