Short Ptr to Byte Ptr?

BlitzMax Forums/BlitzMax Programming/Short Ptr to Byte Ptr?

Tibit(Posted 2005) [#1]
If I have a Short Ptr and want to give it to a function that accepts a Byte Ptr, how do I do?

And when I have a Array of bytes, and want to convert it to a 16bit strings, how do I do? (In other word, backwards)


Azathoth(Posted 2005) [#2]
For the string either String.FromCString:String( cString:Byte Ptr ) if its null terminated, or String.FromBytes:String( bytes:Byte Ptr,length )


Tom(Posted 2005) [#3]
Recasting pointers

Local s:Short = 45678
Local sp:Short Ptr = Varptr s

Print s
Print sp[0]

Local b:Byte Ptr = Byte Ptr sp 'Cast to byte ptr

Print b[0]+256*b[1] 'How a short is calculated



ImaginaryHuman(Posted 2005) [#4]
Local myshort:Short Ptr=10
Local mybytes:Byte Ptr=Byte Ptr(myshort)

also that math to calculate a short is slightly off. A short is 16-bit not 8-bit.

ie

Print b[0]+65536*b[1]

or you could do

Print b[0]+(b[1] Shl 16)

(or does it need to be asl arithmetic shift?)


Tibit(Posted 2005) [#5]
Thanks!!

I like it when the solution is this simple :)

@AngelDaniel, What did you mean by this?
Print b[0]+65536*b[1]

If I have a array of bytes, which actually is an array of shorts, then should I not simple convert the byte Ptr to a Short Ptr:
Local ShortPointer:Short Ptr = Short Ptr BytePointer

if b is an array of bytes, in this case 2 in length and it is therefore a short, or is there more to it?


Tom(Posted 2005) [#6]
Daniel: 2 bytes is 16bit

Proof is in the result. And yes, this also works:

Print b[0] + (b[1] Shl 8) 'Short

Wave: Correct, a Short is 2 bytes, nothing more to it except in Blitzmax Shorts are unsigned (0 to 65535)