Click here to Skip to main content
15,071,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to convert type String to byte in JAVA not Byte[] or byte[] (Array) but byte

I have the following code:

byte dealershipID = billingID.getBytes();


(billingId is type String)

But I get the error: Required Byte[] found byte.

I have also tried Byte.ValueOf(myString)

but I get the error: java.Lang.Byte found required byte

help will be much appreciated, something that seems simple causing a lot of frustration
Posted
Updated 6-Mar-14 20:48pm
v2
Comments
phil.o 7-Mar-14 2:52am
   
Not clear: the getBytes() method returns an array of bytes, and there's nothing you can change about that. You dealershipID has to be an array of bytes.
So that brings now this essential question: what do you want to get from the original string? Maybe try to put on some verbose example about an original string and what you expect from it in terms of a single byte (which can only hold integer values between 0 and 255).

You cannot convert a string to a single byte, because strings are almost always bigger than one byte (to put it in perspective: one byte can at most encode one character).

If you only want a single byte, convert the string to a byte array and choose one of those bytes, ignoring the rest:
Java
byte[] tempArray = billingID.getBytes();
byte dealershipID = tempArray[0];

However, this is almost certainly meaningless. Please have another look at your requirements and rethink this.
   
You cannot convert a string to a byte: a byte is an eight bit quantity, that can hold values between 0 and 255 - a string is a collection of characters of variable length. Since a character is a seven, eight or sixteen bit quantity, you cannot fit a string into a byte except in very special circumstances (i.e. when the string contains a single character in the correct character set).
   
Comments
pyiannakis 7-Mar-14 4:27am
   
I have the following code:

String billingID = "007";

//set the TLVTable
Tag USER_TAG = Tag.defineTag(0x1402, byte.class, new OctetEncoder(), -1);

TLVTable myTable = new TLVTable();
byte dealershipIDarray = billingID.getBytes();



myTable.set(USER_TAG, dealershipIDarray);

sms.setTLVTable(myTable);


this is what I need to achieve as per example here:

http://sourceforge.net/p/smppapi/discussion/84651/thread/6c382c76/

I have no idea how the code was able to run because I get an error on getBytes() that it is unsupported

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900