0

Hello here i want to convert Byte array ie 0x3eb to short so i considered 0x3eb as a string and tried to convert to short but its throwing Numberformat Exception...someone please help me

import java.io.UnsupportedEncodingException;
public class mmmain
{

    public static void main(String[] args) throws UnsupportedEncodingException 
    {
        String ss="0x03eb";
        Short value = Short.parseShort(ss);
        System.out.println("value--->"+value);
    }
}


Exception what im getting is 
Exception in thread "main" java.lang.NumberFormatException: 
For input string: "0x3eb" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:491)
    at java.lang.Short.parseShort(Short.java:117)
    at java.lang.Short.parseShort(Short.java:143)
    at mmmain.main(mmmain.java:14)

even i tried converting 0x3eb to bytes by

byte[] bytes = ss.getBytes();

but i didnt found any implementation for parsing bytes to short.

Thanks in advance

4 Answers 4

2

See the doc of parseShort:

Parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.

The string to be parsed should only contain decimal characters and sign characters, it can not contains the 0x prefix.

Try:

String ss="3eb";
Short value = Short.parseShort(ss, 16);
Sign up to request clarification or add additional context in comments.

Comments

1

Since the string value that you're using is a hexadecimal value, to convert it into short, you need to remove the 0x using a substring and pass the radix as below:

Short.parseShort(yourHexString.substring(2), 16)

Here 16 is the radix. More info in the doc here.

Update

Since the OP asked for some more clarification, adding the below info.

The short datatype can only have values between -32,768 and 32,767. It can't directly hold 0x3eb, but it can hold the equivalent decimal value of it. That's why when you parse it into the short variable and print, it shows 1003, which is the decimal equivalent of 0x3eb.

7 Comments

bro after converting it is showing o/p as 1003 for 3eb. but thing is I want to take "0x3eb " from file and then i need to update this hexadecimal value in main code....so if i convert 3eb it is showing 1003...when there is a need of updation i need to update is as "0x3eb" ....how can i achieve this..? for example--> static final short PRODUCT_ID1 =0x3eb;
@Ravikiran do you mean that you only need to get the hex value as a string, update the value and then use it again as a hex string itself at some other place? If that's the case, you can convert the hex string to int using Integer.parseInt(hexString.substring(2), 16), then update the value and then convert it back to hex string using Integer.toHexString(updatedInt)
Bro i have some textfile in that user will store inputs in hexadecimal form(ie 0x3eb). I will fetch this data using fileinputstream and store the data as a string. Now actually i want to update this string(ie 0x3eb) into my code ie as Short(hexadecimal byte) for this variable --> static final short PRODUCT_ID1 = 0x3eb; so how can i achieve this..? the same data that i have received from user text file i have to update to the above variable PRODUCT_ID1 .
@Ravikiran the short datatype can only have values between -32,768 and 32,767. It can't directly hold 0x3eb, but it can hold the equivalent decimal value of it. That's why when you parse it into the short variable and print, it shows 1003, which is the decimal equivalent of 0x3eb. Does this make it clear?
No worries at all, @Ravikiran :) Please accept the answer if it helped you answer your query. Thanks!
|
1

You have to cut "0x" from the beginning:

short.parseShort(yourHexString.Substring(2), 16)

Comments

1

Follow this document this may help you String to byte array, byte array to String in Java

1 Comment

Link-only answers are not useful, as the URL may become stale over time, which would render your answer useless to future readers. You should add an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.