public static int parseInt(String s,
int radix)
throws NumberFormatExceptionParses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned.
An exception of type NumberFormatException is thrown if any of the following situations occurs: The first argument is null or is a string of length zero.
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') provided that the string is longer than length 1.
The integer value represented by the string is not a value of type int.
Examples: parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
int radix)
throws NumberFormatExceptionParses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned.
An exception of type NumberFormatException is thrown if any of the following situations occurs: The first argument is null or is a string of length zero.
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') provided that the string is longer than length 1.
The integer value represented by the string is not a value of type int.
Examples: parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
Creates a string representation of the integer argument as an unsigned integer in base 16.
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as hexadecimal digits: 0123456789abcdef
These are the characters '\u0030' through '\u0039' and 'u\0039' through '\u0066'. If the uppercase letters are desired, the String.toUpperCase() method may be called on the result:
Long.toHexString(n).toUpperCase()
* HexDump.java
*
* Copyright ?2000 Chad Gibbons
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//package net.sourceforge.util;import java.io.*;/** * HexDump provides a dump method thyat displays a byte array * using a traditional hex dump format. * * @author Chad Gibbons * @version 1.0 */public class HexDump
{
private static final int BYTE_COUNT = 16;
/**
* The dump method displays the provided byte buffer to the
* specified PrintStream.
*/
public static void dump(PrintStream out, byte[] buf)
{
int i;
int n;
for(n = 0; n < buf.length; n += HexDump.BYTE_COUNT)
{
StringBuffer outbuf = new StringBuffer();
String ofs = Integer.toHexString(n).toUpperCase();
for(int j = 0, k = 4 - ofs.length(); j < k; j ++ )
{
outbuf.append("0");
}
outbuf.append(ofs).append(" ");
for(i = n; i < n + HexDump.BYTE_COUNT && i < buf.length; i ++ )
{
int v = (buf[i] < 0) ? buf[i] + 256 : buf[i];
String s = Integer.toHexString(v).toUpperCase();
if(s.length() == 1)
{
outbuf.append("0");
}
outbuf.append(s).append(" ");
}
while(i++ < n + HexDump.BYTE_COUNT)
{
outbuf.append(" ");
}
for(i = n; i < n + HexDump.BYTE_COUNT && i < buf.length; i ++ )
{
int v = (buf[i] < 0) ? buf[i] + 256 : buf[i];
boolean visible = true;
if(v > 127 || Character.isISOControl((char)buf[i]))
{
visible = false;
}
if(visible)
{
outbuf.append((char)v);
}
else
{
outbuf.append(".");
}
}
while(i++ < n + HexDump.BYTE_COUNT)
{
outbuf.append(" ");
}
out.println(outbuf.toString());
}
}
public static void main(String[] argv) throws Exception
{
File f = new File(".");
System.out.println(f.getAbsolutePath());
FileInputStream fin = new FileInputStream("HexDump.class");
byte[] bcon = new byte[fin.available()];
fin.read(bcon);
fin.close();
new HexDump().dump(System.out,bcon);
}
}
{
return (""+"0123456789ABCDEF".charAt(0xf&b>>4)+"0123456789ABCDEF".charAt(b&0xF));
}
正确的应该如下:
byte[] bb=s1.getBytes("UTF16");
for(int i=2;i<bb.length;i++)
{
/*现在希望是在这里有段处理,能将十进制的bb[i],转成十六进制的,然后输出。*/
System.out.println(Integer.toHexString(bb[i]));
}或者用路人甲的方法