[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1153
  • Last Modified:

how to convert string to short

how can i convert string to short value in java.

thanks in advance
0
sjain1203
Asked:
sjain1203
3 Solutions
 
petmagdyCommented:

try this:

short shrt = (short) str.hashCode();

but u will lose prescision
0
 
sjain1203Author Commented:
hello!

thanks for your reply.

i am sorry, but i am not able to understand this code of yrs.

i need to convert string "fred" to it's short value.

can you please give me code for that.

thanks in advance
0
 
petmagdyCommented:
Offcourse I don't know why a string is converted to short, but here is an example to this:

String strValue = "fred";
short shrt = (short) strValue.hashCode();
System.out.println(shrt);

Cheers!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sjain1203Author Commented:
the value returned in not an equivalent to "fred". can you please see into this.


'shall highly appreciate.
0
 
petmagdyCommented:
offcourse it is not,
sjain the String is of length of 4 bytes or more it is variable length object, how do u want to fit it into a primitive short which is fixed length of 16 bits (2 bytes)?

so ur requirment is unlogical
0
 
sjain1203Author Commented:
can i somehow trim the string value or use little endian approach.

0
 
petmagdyCommented:
u can only take the first character of the String and put it into the short
0
 
aozarovCommented:
I am also not short what you are looking for and what little endian approach can be used here.
See if this match you needs:

String name = "fred";
int length = name.length();
short[] shortValues = new short[length];
for (int i = 0; i < length; i++)
    shortValues[i] = (short) name.charAt(i);

// now you can take any short index form that array (0 - length)
for (int i = 0; i < length; i++)
    System.out.print(shortValues[i] + " ");
0
 
patbinCommented:
u can refer the below site to get ur answer

http://www.cems.uwe.ac.uk/~lrlang/java_html/docs/uwejava/Convert.html

public static short stringToShort(java.lang.String s,
                                  short defaultValue)
Convert a string to a short. If the string is not a valid short value then the default value is returned. The string may be checked for validity by calling isValidShort ()
Example call:

short val = Convert.stringToShort (digitString, (short) defaultValue);

hope it will solve ur problem
0
 
CEHJCommented:
You can do something like

BigInteger bi = new BigInteger(1, "fred".toByteArray());
int i = bi.intValue();
short s = (short)bi.intValue();


but you probably don't want short really as it's a signed value
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now