4 bytes to blitzmax int
BlitzMax Forums/BlitzMax Programming/4 bytes to blitzmax int
| ||
I'm working with flash-based sockets, with a blitzmax-based server. Reading an int in flash does not read the integers sent by "writeint" properly, so i'm trying to get a function in flash to read the 4 bytes sent by the server, and convert it into a blitzmax based int. I figured blitzmax writes an int using bytes something like this: int_from_bytes(0,0,0,1) Function Int_From_Bytes(b3:Byte,b2:Byte,b1:Byte,b0:Byte) Print (((((b3 Shl 8) + b2) Shl 8) + b1) Shl 8) + b0 End Function but I don't quite have it. Any clues? |
| ||
according to the stream mod its more about how its stored than how its written...Method WriteInt( n ) Local q:Int=n WriteBytes Varptr q,4 End Method you could just create your own int write routine that mirrors the flash storage method |
| ||
It's probably just a matter of byte order. You need to match whatever flash-based sockets expect. BlitzMax defaults to the endianness your CPU uses, LittleEndian for Intel and BigEndian for PowerPC. You can specify which one to use. testfile$ = "data_12345678.bin" TS:TStream = WriteStream( testfile ) WriteInt( TS, $12345678 ) CloseStream TS TS = ReadStream( testfile ) DefaultEndian = ReadInt( TS ) CloseStream TS TS = BigEndianStream( ReadStream( testfile ) ) BigEndian = ReadInt( TS ) CloseStream TS TS = LittleEndianStream( ReadStream( testfile ) ) LittleEndian = ReadInt( TS ) CloseStream TS DeleteFile testfile Print Print " Test endianness:" Print Print " Default " + Hex( DefaultEndian ) Print " Little " + Hex( LittleEndian ) Print " Big " + Hex( BigEndian ) |