Author Topic: Tiny Function to unsign a byte from READBYTE value  (Read 2245 times)

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
I'd written a simple map editor in BlitzPlus which writes out my mapdata as Bytes and when I read it into GLB the values are in the range -127...+127 and I needed them in the range 0...255. Here is a quick and easy way to do that which although not rocket science does help keep your code looking cleaner.

Function Code
Code: GLBasic [Select]
//      .------------------------------------.
//      |  Unsign READBYTE value to 0...255  |
//      `------------------------------------'
FUNCTION UnsignByte: value%
        IF value% < 0 THEN value% = 256 + value%
        RETURN value%
ENDFUNCTION

Usage
Code: GLBasic [Select]
signed_byte% = UnsignByte(signed_byte%)

Hope you find it useful =)

http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Nice - but look up READUBYTE in the docs  ;)
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Noob alert here lol. I guess it's still handy for poeple to see how it's converted even though maybe I should read the docs more next time =D
« Last Edit: 2011-May-08 by spicypixel »
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
I only noticed because someone else posted about it a while ago  =D
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4143
  • On the shoulders of giants.
    • View Profile
    • My Apps
I think we've all been caught out by that one in the past, especially us old Blitz users.
I came. I saw. I played.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Yep, I'm the original one that got caught out with the problem of reading bytes from a file, Thanks Gernot for fixing it so fast back then :-)

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Yep, I'm the original one that got caught out with the problem of reading bytes from a file, Thanks Gernot for fixing it so fast back then :-)

Hehe guess we won't be the first, OR the last lol =)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.