PB 2.90 - Read/Handle Bytes correct up to 255

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

I am frustrated about the situation that we cant put vals >127 into a byte and get it correct back... In my actual game project i need to read a lot of datas (bytes) with a val up to 255... i could save the data as word or long but this makes our exe bigger... I still hope that fred will fix this very imporatant point in the next release! Maybe var.ub or what ever... Please Fred! :wink:

Mhhh, here is a small example how to save your datas up to 255 - declared as word into a data section and read/handle it back as byte with correct vals up to 255... I am not very happy with this result but it works! Hope this help you...

Code: Select all

;------------------------------------
;
; Read/Handle Bytes correct up to 255
;
;------------------------------------
;
    OpenConsole()
    ;
    For i = 0 To 11                 ; 12-1 = 11
       Read ourbyte.b               ; Here we read our data as byte!!! (saved as word!)
       Read temp.b                  ; Read dummy byte temporary - We dont need this val
       ;     
       If ourbyte 127 we will put it in a new var And calc the correct val! 
       Else 
          xourbyte = ourbyte         ; xourbyte is now the variable with the correct val! :wink: 
       EndIf
       ;
       PrintN (Str(xourbyte))
    Next
    ;
    Sleep_ (8000)  
    ;
    CloseConsole()
    ;   
End
;
;--------- TEST DATA SECTION ---------
;
DataSection:              ; 12 data
    ;
    Data.w   0,  30,  60,  90, 120, 127 ; Your byte data must be saved as word!!!
    Data.w 128, 160, 190, 220, 250, 255 ; I am not happy with this, but it works! Wanted .b up to 255 ! Please!!!
    ;
EndDataSection:

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Mhhhh sorry! its a very bad example and i think its nothing spectacular... You can write:

Code: Select all


;------------------------------------
;
; Read/Handle Bytes correct up to 255
;
;------------------------------------
;
    OpenConsole()
    ;
    For i = 0 To 11                 ; 12-1 = 11
       Read xourbyte.w               ; Here we read our data as byte!!! (saved as word!)
       PrintN (Str(xourbyte))
    Next
    ;
    Sleep_ (8000)  
    ;
    CloseConsole()
    ;   
End
;
;--------- TEST DATA SECTION ---------
;
DataSection:              ; 12 data
    ;
    Data.w   0,  30,  60,  90, 120, 127 ; Your byte data must be saved as word!!!0
    Data.w 128, 160, 190, 220, 250, 255 ; I am not happy with this, but it works! Wanted .b up to 255 ! Please!!!
    ;
EndDataSection:
Its the same... i dont want to handle vals up to 255 as word... ;((


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Ohoh... please dont blame me... If you really want save only bytes up to 255 in your datasection, and if you have vals >127 you must calculate "Your>127Val - 256" and put this -Val into your datasection ;(

Code: Select all

;------------------------------------------
;
; Read/Handle Real Bytes correct up to 255
;
;------------------------------------------
;
    OpenConsole()
    ;
    For i = 0 To 11                 ; 12-1 = 11
       Read ourbyte.b               ; Here we read our data as byte!!! (saved as word!)
       ;     
       If ourbyte 127 we will put it in a new var And calc the correct val! 
       Else 
          xourbyte = ourbyte         ; xourbyte is now the variable with the correct val! :wink: 
       EndIf
       ;
       PrintN (Str(xourbyte))
    Next
    ;
    Sleep_ (8000)  
    ;
    CloseConsole()
    ;   
End
;
;--------- TEST DATA SECTION ---------
;
DataSection:              ; 12 data
    ;
    Data.b   0,  30,  60,  90, 120, 127   ; Your byte data must be saved as word!!!0
    Data.b -128, -96, -66, -36, -6, -1    ; I am not happy with this, but it works! Wanted .b up to 255 ! Please!!!
    ;                                    
EndDataSection:


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

I think byte variables above -1 should be unsigned, negatives signed. I don't think it would be trobling, would it?

Regards,

PS: I like the stars method, it makes you aim to get a new one XD

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi @ all
I think if someone will code a program and need a variable or datasection diclared as byte and want to use/write something like...

Code: Select all

        myvariable.b = 255
                                       
        DataSection:              
            Data.b   0,  30,  60,  90, 120, 127 
            Data.b 128, 160, 190, 220, 250, 255 
        EndDataSection:
...the compiler should check the vals and handle it automatical by using intern .b or .ub - i think this should not be the work of the coder...!? Or do you have fun to save the topic example like the following to handle the vals correct as byte...?

Code: Select all

        DataSection:             
            Data.b   0,  30,  60,  90, 120, 127   
            Data.b -128, -96, -66, -36, -6, -1  
        EndDataSection:
I prefer to use/have vals between -255 up to 255 when diclared as byte! What do you think? Btw. is it easy for you to read my .b -128 in the source to be in reality 255 ?? :wink:)

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Byte are 8 bit signed variables and there is no way to get -255 from them.. Use word variables, what is the problem ? To get a unsigned byte, do that:

UnsignedByte.w = bytes & $FF

Bye,

Fred - AlphaSND
Post Reply