user2521732
user2521732

Reputation: 68

How do i convert from ASCII to Decimal

I have this code:

final byte[] bslval = data1.getByteArray(HRPService.BSL_VALUE);

runOnUiThread(new Runnable() 
{
    public void run() 
    {
        if (bslval != null) 
        {
            try 
            {
                Log.i(TAG, "BYTE BSL VAL =" + bslval[0]);
                //TextView bsltv = (TextView) findViewById(R.id.BodySensorLocation);
                EditText bsltv = (EditText) findViewById(R.id.EditSensorLocation);
                String bslstr = new String(bslval, "UTF-8");
                bsltv.setText(bslstr);                                
            } 
            catch (Exception e) 
            {
                Log.e(TAG, e.toString());
            }

The EditText does show the value as a "d" but not as "64" (which I want). How do I convert this to a decimal instead?

Upvotes: 0

Views: 857

Answers (2)

Henry
Henry

Reputation: 43798

Something like this:

String bslstr = String.valueOf(bslval[0] & 0xff);

This assumes, the value is actually in bslval[0]. The & 0xff is there to get positive results for byte values between (byte)0x80 and (byte)0xff.

Upvotes: 2

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

Use the simpler string concatenation if you only want to set the first byte value bslval[0] as text.

bsltv.setText(bslval[0] + "");

Upvotes: 0

Related Questions