Convert bytes to int, keep the negative number

This is a discussion on Convert bytes to int, keep the negative number within the C Programming forums, part of the General Programming Boards category; Hi, I have written a function which read some bytes then convert it to int type and I want it ...

  1. #1
    umm
    umm is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    umm
    umm is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    umm
    umm is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    umm
    umm is offline
    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,831
    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-20-2009, 11:37 PM
  2. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  3. Replies: 26
    Last Post: 11-30-2007, 02: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, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21