Thread: Convert bytes to int, keep the negative number

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Convert bytes to int, keep the negative number

    Hi,

    I have written a function which read some bytes then convert it to int type
    and I want it still keep the negative number, ex: 0xFFFF = -1

    my sample code:
    Code:
    FILE * fp;
    int value = 0;
    int read_num = 3;
    ....
    fread(&value, read_num, 1, fp);
    That is read 3 bytes, If it read 0xFFFFFF, the value is 16777215.
    But I want get the negative number -1.

    And, If read 2 (or 4 bytes), the 0xFFFF (or 0xFFFFFFFF) also need to convert to -1.

    Thank you!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    so check the value of 16th bit (when you read 2 bytes) or 24th bit whenyou read 3 bytes

    if this bit is 1 - fill the "unread bytes" with 0xFF if thats what you want

    if you just want replace 0xFFFF with -1


    Code:
    if(number == 0xFFFF) number = -1;
    is simplest way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Sorry I did not explain clear enough

    If the first bit is 1 (0x8000~0xFFFF)
    the value should be a negative number (-32768 ~ -1)

    If the first bit is 0 (0x0000~0x7FFF)
    the value should be a positive number (0 ~ 32767)

    Thank you!!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by umm View Post
    Sorry I did not explain clear enough

    If the first bit is 1 (0x8000~0xFFFF)
    the value should be a negative number (-32768 ~ -1)

    If the first bit is 0 (0x0000~0x7FFF)
    the value should be a positive number (0 ~ 32767)

    Thank you!!
    why not to read into short int and then assign to int?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Quote Originally Posted by vart View Post
    why not to read into short int and then assign to int?
    Because I need read 1~4 Bytes in different case.
    Thank you

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by umm View Post
    Because I need read 1~4 Bytes in different case.
    Thank you
    Then try to implement the algorithm I have suggested earlier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Quote Originally Posted by vart View Post
    Then try to implement the algorithm I have suggested earlier
    Thank you so much!!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,835
    Basically vart's method:
    Code:
    	if (((char *)&value)[read_num - 1] & 128)
    		for (i = read_num; i < sizeof(value); i++)
    			((char *)&value)[i] = 255;
    Works for read_num being 1, 2, 3, or 4 bytes... or even higher if whatever data type is value might be 64-bit integer. Algorithm sizes to fit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM