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 6913569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:14:07+00:00 2026-05-27T09:14:07+00:00

I am receiving messages of 14 bytes via a socket’s InputStream . They look

  • 0

I am receiving messages of 14 bytes via a socket’s InputStream.
They look like this: a55a0b051000000000223664300c
and they have a specific pattern. The first two bytes are for synchronization and are always constant.

I am looking for the best way to verify this message.
First the length of 14 bytes would have to be verified, then the first 2 Bytes etc.

This is my approach until now:

public void handleMessage(String msg){

    if(msg.length() == 14){
        if(msg.substring(0, 1).equals("a5") && msg.substring(2, 3).equals("5a") ){

            //Determine tpye
            if(msg.substring(4, 5).equals("0b")){

                //Type = 05
                if(msg.substring(6, 7).equals("05")){

                    if(msg.substring(8, 15).equals("0b")){

                    }
                }

            }
            else if(msg.substring(4, 5).equals("6b")){
                //Type = 05
                if(msg.substring(6, 7).equals("05")){

                    if(msg.substring(8, 15).equals("0b")){

                    }
                }
            }
            else if(msg.substring(4, 5).equals("4b")){
                // ...
            }
            else if(msg.substring(4, 5).equals("ab")){
                // ...
            }
            else System.out.println("Error: telegram type");

        }else System.out.println("Error: Sync Bytes.");



    }else System.out.println("Error: Telegram length.");



}

But I want to avoid all those nested ifs and the redundant code that comes with them.
Any ideas on how to handle the string in a more efficient way?
Thanks!

  • 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-05-27T09:14:08+00:00Added an answer on May 27, 2026 at 9:14 am

    first as I said in the comments don’t convert to string when you don’t need to

    you can invert the guard causes and put an early return there to avoid those indents

    also a switch on top level might be prudent as this is more readable then if-else cascade (less prone to typos)

    public void handleMessage(byte[] msg, int off){
        if(msg.length()-off < 14){//just use the first 14 after offset          System.out.println("Error: Telegram length.");
            return;
        }
        if(msg[0+off]!=0xa5 || msg[0+off]!=0xa5 ){
            System.out.println("Error: Sync Bytes.");
            return;
        }
        //Determine tpye
        switch(msg[2+off]){
        case 0x0b:
            if(msg[3+off]==0x05){
    
                if(msg[4+off]==0x0b){
                }
    
            }
            break;
        case 0x6b:
            if(msg[3+off]==0x05){
    
                if(msg[4+off]==0x0b){
    
                }
            }
            break;
        case 0x4b:
            //...
            break;
        case 0xab:
            //...
            break;
        default:System.out.println("Error: telegram type");
            break;
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TCP socket client receiving messages (data) from a server. messages are
I have this bit of code that is receiving a series of messages: byte[]
I have a server application receiving messages from a JMS queue. And client applications
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have strange peoblems with sending-receiving sms messages in Android. I am using standart
My Python program is receiving ICMP destination unreachable messages from a raw socket. The
I have a question on sockets. I have this code: while(bytes = recv(sClient, cClientMessage,
i am receiving string messages over a socket connection and as i am receiving
I have a socket that is receiving streaming stock tick data. However, I seem
I am currently sending and receiving SIP messages across a network. When I want

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.