How to convert String (byte array as string) to short

byte array to string
java byte array to string utf-8
byte array to string c#
string to byte array golang
string to byte array python
add string to byte array java
string to byte array kotlin
java byte array to hex string

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

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.

3 ways to convert String to byte array in Java, Today, I am going to discuss one of the common tasks for programmers, converting a String to a byte array. You need to do that for multiple reasons e.g. for� The idea here is to create the byte stream from specified byte array. Then read all characters from the byte stream and return stream as a string. The following code example shows how to implement this. The solution automatically tries to determine the encoding used using byte order mark (BOM) in the byte stream. If not found, UTF-8 encoding is

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);

Convert String to Byte Array and Reverse in Java, A String is stored as an array of Unicode characters in Java. To convert it to a byte array, we translate the sequence of Characters into a sequence� The Encoding.GetString () method converts an array of bytes into a string. The following code snippet converts an ASCII byte array into a string and prints the converted string to the console. // Convert a byte array to a C# string. string str = Encoding.ASCII.GetString (bytes);

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

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

Convert between byte array/slice and string � YourBasic Go, CODE EXAMPLE When you convert between a string and a byte slice (array), you get a brand new slice that contains the same bytes as the string, and vice� We refer to the process of converting a byte array to a String as decoding. Similar to encoding, this process requires a Charset. However, we cannot just use any charset for decoding a byte array. We should use the charset that was used to encode the String into the byte array. We can convert a byte array to a String in many ways.

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

How to convert Byte[] array to String in Java, println(“Text [Byte Format] : ” + bytes);. At this point you are creating new String by using bytes values not byte Object location. So the result is right� I have an array of unsigned characters, produced by hash function. I want to convert it to string or ascii code or hexdecimal. The value at the moment are gibberish and hash are nonsense. I need to know how I can convert them. Any help would be much appreciated. The array a byte array or unsigned

C# Convert String to Byte Array, Main: The character representations from the input string were first converted to fit in one byte elements each. Info: We see that the integer 68 corresponds to the� How to convert String to byte array in Java ? To convert from String to byte array , use String.getBytes() method . Please note that this method uses the platform’s default charset . We can use String class getBytes() method to encode the string into a sequence of bytes using the platform’s default charset .

Convert byte[] Array to String in Java, Learn to convert byte[] to String and String to byte[] in java – with examples. Conversion between byte array and string may be used in IO� This is the snippet Convert a Byte Array to a String on FreeVBCode. The FreeVBCode site provides free Visual Basic code, examples, snippets, and articles on a variety of other topics as well.

Convert.ToByte Method (System), Converts the value of the specified object to an 8-bit unsigned integer, using the example defines a string array and attempts to convert each string to a Byte. Convert String to Byte[] Array 1. Using String.getBytes() Method. To convert a string to a byte array, just use String.getBytes() method. This method uses the default character encoding to encode this string into a sequence of bytes. Here is an example: // create a string (demo purpose only) String str = "Hey, there!"; // convert string to byte

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!
  • 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.