Retrieves a null terminated substring from an array of bytes. - Java java.lang

Java examples for java.lang:byte Array to String

Description

Retrieves a null terminated substring from an array of bytes.

Demo Code

/**//from   w w w. j a va 2s .  c o  m
 *******************************************************************************
 * Copyright (C) 1996-2004, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
//package com.java2s;

public class Main {


    /**
     * Retrieves a null terminated substring from an array of bytes.
     * Substring is a set of non-zero bytes starting from argument start to the 
     * next zero byte. If the first byte is a zero, the next byte will be taken as
     * the first byte.
     * @param str stringbuffer to store data in, data will be store with each
     *            byte as a char
     * @param array byte array
     * @param index to start substring in byte count
     * @return the end position of the substring within the character array
     */
    static int getNullTermByteSubString(StringBuffer str, byte[] array,
            int index) {
        byte b = 1;

        while (b != 0) {
            b = array[index];
            if (b != 0) {
                str.append((char) (b & 0x00FF));
            }
            index++;
        }
        return index;
    }
}

Related Tutorials