Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:40:52+00:00 2026-06-17T22:40:52+00:00

I am trying to take serial data from a TTL output device, pass it

  • 0

I am trying to take serial data from a TTL output device, pass it through a JY-MCU dongle and read it on my Android phone. The data is sent as a 4 digit ASCII decimal number followed by carriage return and line feed.

Using BluetoothChat, I can see the transmitted number correctly on the phone but I want to manipulate the number within the app and then send the result of the manipulation to the screen.

I only need to read the data, not send it.

Looking at the code in BluetoothChat.java I thought I would need to convert the string created in MESSAGE_READ to an Integer value, manipulate this integer number, convert it back to a string and send this result to the display.
Is this the right way to go about this? I have tried using Integer.parseInt() and String.valueOf() but without success.

I can post a simple section of the code showing what I am trying to do but thought I might be making a simple mistake which somebody can point out first.

Thanks for any suggestions
David

 byte[] readBuf = (byte[]) msg.obj;
 // construct a string from the valid bytes in the buffer
 String readMessage = new String(readBuf,0, msg.arg1);
 //remove any leading zero's
 String readMessages = readMessage.replaceAll("^0*", "");
 //remove any leading zero's
 // Declare Integer
 int newMessage;
 // give the integer a value
 newMessage = Integer.valueOf(readMessages);
 // try some simple division
 newMessage = newMessage / 100;
 // back to a string
 String finalMessage = Integer.toString(newMessage);
 // display the result
 mConversationArrayAdapter.add(mConnectedDeviceName+":  " + finalMessage);

Stacktrace

01-25 18:09:12.905: E/AndroidRuntime(5413): FATAL EXCEPTION: main
01-25 18:09:12.905: E/AndroidRuntime(5413): java.lang.NumberFormatException: Invalid int: “”
01-25 18:09:12.905: E/AndroidRuntime(5413): at java.lang.Integer.invalidInt(Integer.java:138)
01-25 18:09:12.905: E/AndroidRuntime(5413): at java.lang.Integer.parseInt(Integer.java:359)
01-25 18:09:12.905: E/AndroidRuntime(5413): at java.lang.Integer.parseInt(Integer.java:332)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:40:53+00:00Added an answer on June 17, 2026 at 10:40 pm

    The regular expression you are using is not correct as you can wonder by the fact that it returns “” (empty string) when something like “00” or “0” is the input. I suggest you to use the regular expression in the code below.

    For your convenience I give you a simple test program to check if the regexp fullfil your needs. Feel free to add more tests if you are in doubt.

    package stackoverflow.answers;
    
    public class ParsingTest {
    
        public static void main(String[] args) {
    
            System.out.println("Result is " + extractInteger("010"));
            System.out.println("Result is " + extractInteger("1010"));
            System.out.println("Result is " + extractInteger("0001"));
            System.out.println("Result is " + extractInteger("0000"));
            System.out.println("Result is " + extractInteger("11A0"));
    
        }
    
        private static Integer extractInteger(String s) {
            Integer i = null;
            String t = s.replaceFirst("^0+(?!$)", "");
            try {
                i = Integer.valueOf(t);
            } catch (Exception e) {
                e.printStackTrace();
                // to be sure you return always something
                // null could be other value returned
                // in that case use a finally
                return 0;
            }
            return i;
        }
    
    }
    

    Pay attention that I prefer to return 0 when the parsing has some problem, you could decide to return null or a different integer (-1?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to take data from Contact Form 7 WordPress plugin and pass it
I am trying to write an android app to read and write data from
net I am trying to take a value from a previous form typically in
I'm trying to take a CSV file, strip a few columns, and then output
I am trying to take the standard input from the console and send it
I'm trying to take a value that I'm pulling from a database and divide
I'm trying to take a value from a jQuery mobile slider after dragging it
Trying to take in several file names from the user at the command line
Trying to take an Array position(toString) and pass that to a new activity, then
I am trying to take input from the keyboard and then store it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.