Copyright © 1998 T. H. Merrett

Class bytechars

public  class  bytechars

{
        // Methods
    public static int byte2hex(byte[] b, int start, int length); §byte2hex
    public static int byte2int(byte[] b); §byte2int1
    public static int byte2int(byte[] b, int start); §byte2int2
    public static short byte2short(byte[] b, int start); §byte2short
    public static String byte2string(byte[] b); §byte2string1
    public static String byte2string(byte[] b, int length); §byte2string2
    public static String byte2string(byte[] b, int start, int length); §byte2string3
    public static byte[] int2byte(int in); §int2byte1
    public static byte[] int2byte(int[] in); §int2byte2
    public static String readString(); §readString1
    public static String readString(int length); §readString2
    public static void short2byte(short s, byte[] array, int at); §short2byte
    public static void string2byte(String s, byte[] b, int start, int length); §string2byte
}
This class is an incomplete collection of conversion routines between byte arrays and strings and various integers.


Methods

byte2hex

public static String byte2hex(byte[] b, int start, int length)
Parameters:
b - the byte array to be converted to hexadecimal string
start - the position in the byte array where conversion is to start
length - the number of elements of the byte array to be converted
Returns:
The byte array is converted to type String containing pairs of hexadecimal symbols and the result of the conversion is returned.

byte2int

public static int byte2int(byte[] b)
Parameters:
b - the byte array to be converted to int (twos complement)
Returns:
The first four bytes of the array are converted to type int and the result of the conversion is returned.

byte2int

public static int byte2int(byte[] b, int start)
Parameters:
b - the byte array to be converted to int (twos complement)
start - the position in the byte array where conversion is to start
Returns:
The four bytes of the array, beginning at start, are converted to type int and the result of the conversion is returned.

byte2short

public static short byte2short(byte[] b, int start)
Parameters:
b - the byte array to be converted to short (twos complement)
start - the position in the byte array where conversion is to start
Returns:
The two bytes of the array, beginning at start, are converted to type short and the result of the conversion is returned.

byte2string

public static String byte2string(byte[] b)
Parameters:
b - the byte array to be converted to String: expects ascii
Returns:
The byte array is converted to type String and the result of the conversion is returned.

byte2string

public static String byte2string(byte[] b, int length)
Parameters:
b - the byte array to be converted to String: expects ascii
length - the number of elements of the byte array to be converted
Returns:
The byte array is converted to type String and the result of the conversion is returned.

byte2string

public static String byte2string(byte[] b, int start, int length)
Parameters:
b - the byte array to be converted to String: expects ascii
start - the position in the byte array where conversion is to start
length - the number of elements of the byte array to be converted
Returns:
The byte array is converted to type String and the result of the conversion is returned.

int2byte

public static byte[] int2byte(int in)
Parameters:
in - the integer to be converted to byte array (twos complement)
Returns:
The integer is converted to a four-byte array and the result of the conversion is returned.

int2byte

public static byte[] int2byte(int[] in)
Parameters:
in - an array of integers to be converted to byte array (twos complement)
Returns:
The integers are converted to an array, four bytes per integer, and the result of the conversion is returned.

readString

public static String readString()
Throws:
IOException (I-§2.29)
If an I/O error occurs.
Returns:
System.in is read, using stream I/O (this is not suitable for block access to files), and the resulting string is returned.

readString

public static String readString(int length)
Parameters:
length - the minimum length of the string returned
Throws:
IOException (I-§2.29)
If an I/O error occurs.
Returns:
System.in is read, using stream I/O (this is not suitable for block access to files), and the resulting string is returned, padded by blanks if shorter than length.

short2byte

public static void short2byte(short s, byte[] array, int at)
Parameters:
s - the short to be converted to byte array (twos complement)
array - output: the byte array containing the result
at - the position in the output byte array where the converted short is to start

string2byte

public static void string2byte(String s, byte[] b, int start, int length)
Parameters:
s - the string to be converted to byte array
b - output: the byte array containing the result
start - the position in the output byte array where the converted string is to start
length - the number of characters converted and. hence, bytes output

1 Java cs420 Document (HTML written by T. H. Merrett on August 26, 1998)
Please send any comments or corrections to tim@cs.mcgill.ca