0

When I assigned vlanid=0x960.(hexadecimal value) directly,It works very well. But when I use java function for same purpose,It get stored as decimal value. I attached the code here.

int vlanid,

short vlanid1;

vlanid= (int) jSpinner1.getModel().getValue();

vlanid1 = Short.parseShort(((Integer.toHexString(vlanid))),16);
stinepike
  • 54,068
  • 14
  • 92
  • 112
Sudhakar
  • 29
  • 1
  • 1
  • 4
  • What SpinnerModel are you using? – Stephen Carlson May 31 '13 at 06:02
  • 1
    And why can't you just cast it? short vlanid1 = (short)vlanid; – Stephen Carlson May 31 '13 at 06:03
  • 3
    Internally whatever value you assign to a short is stored as its binary representation, for example 0000 1110 0110 0010. This is interpreted as decimal in most contexts, but the value is the same no matter how you interpret it (hexadecimal, decimal, octave, binary, etc) For example, the numbers 16 and 0x10 are the same number, so if you assign 0x10 to a short and print it it will print 16. But if you explicitly print it as a hexadecimal number you get 0x10, the same number. – Patashu May 31 '13 at 06:06
  • print the vlanid after getValue. I think the problem is in there – stinepike May 31 '13 at 06:08
  • 1
    +1 @Patashu. To the OP:We only print numbers in base 10 normally (as opposed to base 2; binary) because it is easier for us to deal with. If you want it to print in base 16 (hexadecimal) just format the `single` as such. If this is not what are actually trying to do, please be clearer – Craig May 31 '13 at 06:15
  • vlanid is a decimal value,and vlanid 1 is an hexadecimal value then how can I cast it into short? – Sudhakar May 31 '13 at 06:36
  • @Sudhakar: they are both stored as binary values, they are simply display in a different base to make it easier to read (usually base 10; decimal) – Craig May 31 '13 at 06:38
  • I need to set this variable (Vlanid1) to another variable which must be in hexadecimal form. ether.vlan=(short)vlanid1; Now we vlanid1 should be in hexadecimal (0x960 like this form) – Sudhakar May 31 '13 at 06:44
  • There is no such thing as a 'hexadecimal value'. There are 'values', and there are hexadecimal *representations.* Not a real question. – user207421 May 31 '13 at 11:03

2 Answers2

3

It appears that you were not understanding the information given in the comments, so here is an example which shows you that it doesn't matter if it was initially stored as a "decimal" or "hexadecimal"

    short short1 = 0x10;
    short short2 = 16;
    short short3 = (short) (short1 + short2);
    System.out.println("Short1: " + short1 + ", " + Integer.toHexString(short1));
    System.out.println("Short2: " + short2 + ", " + Integer.toHexString(short2));
    System.out.println("Short3: " + short3 + ", " + Integer.toHexString(short3));

Output:

Short1: 16, 10
Short2: 16, 10
Short3: 32, 20

EDIT:

In response to your comment I have added an example of going from an int to a short using two different methods. What you seem to be getting caught on is that the int should be in hexadecimal value; this is not how values are stored (doesn't matter whether it is int, short, long, etc.). All of these types are stored internally as two's complement integers (See this post). Thus in the example below both int1 and int2 represent the same value

    int int1 = 0x01b213d4;  // Stored as: 1101100100001001111010100
    int int2 = 28447700;    // Stored as: 1101100100001001111010100

    short short1Cast = (short) int1;
    short short1Mask = (short) (int1 & 0xFFFF);
    short short2Cast = (short) int2;
    short short2Mask = (short) (int2 & 0xFFFF);

    System.out.println("Int1: " + int1 + ", " + Integer.toHexString(int1) + ", " + short1Cast + ", " + short1Mask);     
    System.out.println("Int2: " + int2 + ", " + Integer.toHexString(int2) + ", " + short2Cast + ", " + short2Mask);

Output:

Int1: 28447700, 1b213d4 | Short1: 5076, 5076
Int2: 28447700, 1b213d4 | Short2: 5076, 5076

I have kept both the cast method and mask method in here because you explicitly made reference to masking the integer value. Unless you have to do something special with bit masking, I would highly suggest using the easier cast approach as there is no functional difference in the output value.

Community
  • 1
  • 1
Craig
  • 1,390
  • 7
  • 12
  • I dont want to print or display the ans. I need to perform one more operation. Short tci; tci = vlanid1 &0x0FFFF; Here vlanid1 should be in hexadecimal value and it is stored in short.So only I fix my problem in my coding. – Sudhakar May 31 '13 at 17:08
0

The number is always stored as two-complement binary. However when you display this number you generally see the result of Short.toString(short) which produces a decimal by default.

The format for how you describe the number is not important and not stored. Only the value matters and this can be display as decimal (the default) or any other base you want from base 2 up to base 36.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130