Thread: Unexpected Range for Signed Integers

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    Unexpected Range for Signed Integers

    I have a file with some data in it I converted to 16-bit signed integers with Matlab. I am running a C program that I want to read the file, and display each integer by reading the integers 8-bits at a time, and then I'm "shifting" each of these bytes, into a short integer, so I kinda get the 16-bit integer back as a short integer.

    My data values range from about -2000 through about 9000. The program does display the points from the file, but the negative points seem to be "wrapped" around to the upper limit of signed short integers. Once the data points start to decrease and become negative, 65535 becomes the new "0-point" and they decrease until like 63500 or something, (about 2000 below 0), and will climb back up and then go back to the 0 - 9000 range when they should be positive again.

    This is quite strange and I can not seem to figure out why it is happening. I know Matlab made my data signed, because I double checked my script that did that, and when I changed it to make my data into 16-bit unsigned integers, negative numbers became straight 0's after being run through the c program, as I would expect.

    Thanks in advance for any help offered!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This is quite strange and I can not seem to figure out why it is happening.
    I'm assuming that it is because that's exactly what you've told your code to do.

    If you want help though, you'll have to post your code.

    I changed it to make my data into 16-bit unsigned integers, negative numbers became straight 0's after being run through the c program, as I would expect.
    No. They did not do any such thing. "MatLab" may have done the conversion by truncating negatives. You may have written code to handle the case in your own C code. I don't know. I do know that negative values didn't "become strait 0's".

    Soma

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Maybe when you read 8-bits at a time, you should be using unsigned char.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    how are you displaying the values? printf? what is your format string?

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Thanks for the replies everyone. I figured this one out it seems. The problem was in my variable declarations. I had declared the variable holding the data point as an integer instead of a short integer. Since I was only putting 8-bits into the integer, the bit at the beginning of the integer that determines the int's sign was left unfilled as a 0. And the compiler thought the total 16-bits that should have been a short was an int that just happened to have the first 2 bytes all zeros. That means it can't be negative, because the sign bit can't equal 1, it's empty. It read it as one huge number instead is how I understand it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. Converting RAW data to ASCII Signed Integers 16-bit
    By Hansie in forum C++ Programming
    Replies: 6
    Last Post: 01-21-2008, 11:50 PM
  3. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  4. how to use signed integers(dynamic memory)
    By Juicehead in forum C Programming
    Replies: 4
    Last Post: 12-07-2002, 11:50 AM
  5. DIfference between signed and unsigned integers ?
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 09:27 AM

Tags for this Thread