negative HEX to bytes


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Location
    NC, USA
    Posts
    53

    Default negative HEX to bytes

    How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.

    Thanks

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cpayne View Post
    How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.

    Thanks
    What version of PBP are you using?
    Google 'Two's Complement arithmetic'

  3. #3
    Join Date
    Aug 2005
    Location
    NC, USA
    Posts
    53


    Did you find this post helpful? Yes | No

    Default

    I think its 2.50A PBPro

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cpayne View Post
    I think its 2.50A PBPro
    If you're using 2.50A, and an 18F, then you can use LONG's, which will handle signed integers, both in the math and in the display.
    But you want to take a -255 to 255, with a range of 512 integer values, and fit that into a byte value, which has a range of 0 to 255, a total of 256 integer values.
    Do you want to lose that 'resolution'? Or do you actually mean something else?

  5. #5
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cpayne View Post
    How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.
    I'm unsure of how you wish to implement the data, so I may be out of line here.

    But have you considered using an offset? Consider 127 to be 0, then manipulate the numbers from there. Negative values are 127-x; positive values (i.e., if x>127) are x-127.
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

Similar Threads

  1. Configuration bits in a HEX file gone missing?
    By grzes.r in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd February 2010, 01:10
  2. LP Instant Interrupts Error
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th December 2009, 09:13
  3. PIC16F877A pwm use for IR transmission
    By mcbeasleyjr in forum General
    Replies: 0
    Last Post: - 11th July 2009, 18:51
  4. bytes compressed and encrypted
    By RAYMON in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 3rd March 2009, 11:24
  5. Converting Bytes from hex to Dec
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 8th April 2005, 19:44

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts