-2

I have a hex string,st="9e". I need to convert it into byte and insert it in the byte array. But i am not able to cast from string to byte. I have used techniques like 1) Byte.valueOf(str); 2) new Byte(str); 3) Tried appending "0X" to str and then converting to byte. All these gave errors.

Can you tell me how to cast from string to byte.

S.K
  • 609
  • 1
  • 8
  • 14
  • possible duplicate of [Convert string to byte\[\]](http://stackoverflow.com/questions/228987/convert-string-to-byte) – Nishant Nov 16 '11 at 04:03
  • 1
    downvoting because a simple google search would've yielded the right answer – Dmitry B. Nov 16 '11 at 04:03
  • 1
    The straight forward ans has been given to you from all the people still you are confused. – Dinesh Prajapati Nov 16 '11 at 04:10
  • I don't think the OP is not looking for a byte array. She is looking for a single byte – parapura rajkumar Nov 16 '11 at 04:11
  • Your goal is not to *cast* the string to a byte, but rather to *parse* the hexadecimal-encoded string to an integral value and ensure that the value can be represented by a single byte. So long as you parse no more than two hex digits, with each digit representing four bytes, the value will fit into an octet ("signed" bytes notwithstanding). – seh Nov 16 '11 at 04:18
  • Dmitry Beransky,Drax..I think you dint get my question right.. I need to put 9e in the array. typecasting it yields -98 as the answer. – S.K Nov 16 '11 at 05:03
  • 1
    Seeing how many people didn't understand your question, perhaps you should consider making it more clear. And stop downvoting. People are answering exactly what you asked, it's not their fault you didn't express yourself correctly – Dmitry B. Nov 16 '11 at 05:31
  • Sorry, I meant that each hex digit represents four *bits*, not *bytes*. – seh Nov 16 '11 at 19:10

5 Answers5

1

Have you tried using String.getBytes()? It takes in a string and outputs it as a byte[]. Hope that helps.

Dan W
  • 5,718
  • 4
  • 33
  • 44
0

Something like

            String x = "2A"; 
    int intValue = Integer.parseInt(x, 16);
    byte b = (byte) intValue;

9e is too big for byte though as it amounts to a value bigger than 128 that a Byte can hold.

An unsigned byte can hold 0-255 but java only has signed byte that can hold -128 to 127 so you will have to suitably map the range yourself

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
  • The cast from `int` to `byte` may be truncating a much larger value that won't fit into a single byte. Including an assertion testing the integer value first and using a mask to capture only the lowest-order byte would be a more sane alternative. – seh Nov 16 '11 at 04:13
-1

Have you tried below code.

String st = "9e";
byte [] value = st.getBytes();
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
-1

You may be looking for something like this:

import java.util.*;
import java.lang.*;

class Main
{
        public static void main (String[] args)
        {
                String hex = "9e";
                int i = Integer.parseInt(hex, 16);
                System.out.println(Arrays.toString(intToBytes(i)));
        }

        private static byte[] intToBytes(int n) 
        {
                byte[] bytes = new byte[4];
                for(int i = 0; i < 4; i++)
                {
                        bytes[i] = (byte) (n | 0);
                        n >>= 8;
                }
                return bytes;
        }
}

Since bytes are signed integral types, you'll need to convert them to unsigned values.

blackcompe
  • 3,180
  • 16
  • 27