• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: n bytes numeric - is it unsigned ?

 
Pavel Tcheshmedjiev
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all !
First I'd like to congretulate you for this helpful forum.
Here is my question:
1. I have in the DB file format description some lines like this:
2 byte numeric, field length in bytes
So the first approach is to interpret the 2 byte number as generic Java short. In this case if the most senior bit is 1, the short variable value will be negative.
The second approach is to interpet the 2 byte number as unsigned short.
For example if hardcoded may look like this:
int number = raf.readUnsignedShort();
In this case we double the length of the Java variable - from short to int.
So my question is does it matter which approach is used ?
If Java generic short(signed) is used, does it fail the assignment ?
Thanks, Pafka
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pafka,
Welcome to this forum.
Could you please change your displayed name to meet the JavaRanch naming policy? You can change it here.
You can read the 2 byte value from the database file as a signed short. That is, you can use the readShort() method from any class that implements the java.io.DataInput interface (for example, RandomAccessFile).
 
Pavel Tcheshmedjiev
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, George.
The word "numeric" at once looked like unsigned to me
I entered my full name, I thought it is a bit longer
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pavel,
As long as the numeric values that are stored in this field are less than (2 to the 15th power) - 1 (i.e., 32,767) then whether you use readShort() or readUnsignedShort() you would get the same answer.
Originally posted by Pavel Tcheshmedjiev:
Thank you, George.
The word "numeric" at once looked like unsigned to me
I entered my full name, I thought it is a bit longer

Your last name is not that long. OK, you beat me by two letters, but you're 3 letters short compared to my fiancee. I don't think we'll be hyphenating our last names when we get married (28 letters if you count the hyphen).
 
Pavel Tcheshmedjiev
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Marinkovich:
Hi Pavel,
As long as the numeric values that are stored in this field are less than (2 to the 15th power) - 1 (i.e., 32,767) then whether you use readShort() or readUnsignedShort() you would get the same answer.

Your last name is not that long. OK, you beat me by two letters, but you're 3 letters short compared to my fiancee. I don't think we'll be hyphenating our last names when we get married (28 letters if you count the hyphen).

Hi George,
Yes, 32 767 seems OK for highest value(the first bit free from duty )
About names I absolutely agree again Long names rule
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic