在java中 如何将得到的String转换为Short型 急。。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在java中 如何将得到的String转换为Short型 急。。相关的知识,希望对你有一定的参考价值。

打印出来的String str =0x11;
下面如何将十六进制的str转换为十进制的

可以用基本数据类型的对象包装器来转换。

这在lang包中,系统自动加载。

public class Zh 

public static void main(String[] args) 
//建立String
String b="4";
//用基本数据类型的对象包装器将String转换为short
Short a=new Short(b);
//输出a
System.out.println(a);



这样就成功的将String转换为short,希望能帮到你。

参考技术A 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。
答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。
public static String byteToString(byte b)
byte high, low;
byte maskHigh = (byte)0xf0;
byte maskLow = 0x0f;

high = (byte)((b & maskHigh) >> 4);
low = (byte)(b & maskLow);

StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low));

return buf.toString();


private static char findHex(byte b)
int t = new Byte(b).intValue();
t = t < 0 ? t + 16 : t;

if ((0 <= t) &&(t <= 9))
return (char)(t + '0');


return (char)(t-10+'A');


未解决的疑问在java中不存在类似C中的无符号量,所以如果一个字节超过0x80其对应的整型值即为负值,但在高位右移4位后还是负值,且与对应的正值相差16,比如0xB2经过右移后的期望值是0x0B(11)但实际值是-5与预期的值相差16(这个16通过多次试验得出),对此现象为找到合理的解释。

问题2:“String a=”B2” --> byte b=0xB2”字符的byte转换为byte数据类型
答:思路通过Integer作为转换的中间桥梁
public static int stringToByte(String in, byte[] b) throws Exception
if (b.length < in.length() / 2)
throw new Exception("byte array too small");


int j=0;
StringBuffer buf = new StringBuffer(2);
for (int i=0; i<in.length(); i++, j++)
buf.insert(0, in.charAt(i));
buf.insert(1, in.charAt(i+1));
int t = Integer.parseInt(buf.toString(),16);
System.out.println("byte hex value:" + t);
b[j] = (byte)t;
i++;
buf.delete(0,2);


return j;


问题3:整数(表示范围限定为两个字节unsigned short)通过Integer.byteValue()转换成byte[2],如果超出一个byte的表示范围将会截断高位的值。
答:思路一个byte能表示的最大整数为256(超过128为负值,超过256将被截断),所以取256的倍数为byte[0],256的余数为byte[1]。
byte[] d = new byte[l+2];
….
buff.put(new Integer(l/256).byteValue());
buff.put(new Integer(l%256).byteValue());

byte[]到short、int、long的相互转换
public final static byte[] getBytes(short s, boolean asc)
byte[] buf = new byte[2];
if (asc) for (int i = buf.length - 1; i >= 0; i--) buf[i] = (byte) (s & 0x00ff);
s >>= 8;

else
for (int i = 0; i < buf.length; i++)
buf[i] = (byte) (s & 0x00ff);
s >>= 8;

return buf;

public final static byte[] getBytes(int s, boolean asc)
byte[] buf = new byte[4];
if (asc)
for (int i = buf.length - 1; i >= 0; i--)
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;

else
for (int i = 0; i < buf.length; i++)
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;

return buf;

public final static byte[] getBytes(long s, boolean asc)
byte[] buf = new byte[8];
if (asc)
for (int i = buf.length - 1; i >= 0; i--)
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;

else
for (int i = 0; i < buf.length; i++)
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;

return buf;

public final static short getShort(byte[] buf, boolean asc)
if (buf == null)
throw new IllegalArgumentException("byte array is null!");

if (buf.length > 2)
throw new IllegalArgumentException("byte array size > 2 !");

short r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--)
r <<= 8;
r |= (buf[i] & 0x00ff);

else
for (int i = 0; i < buf.length; i++)
r <<= 8;
r |= (buf[i] & 0x00ff);

return r;

public final static int getInt(byte[] buf, boolean asc)
if (buf == null)
throw new IllegalArgumentException("byte array is null!");

if (buf.length > 4)
throw new IllegalArgumentException("byte array size > 4 !");

int r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--)
r <<= 8;
r |= (buf[i] & 0x000000ff);

else
for (int i = 0; i < buf.length; i++)
r <<= 8;
r |= (buf[i] & 0x000000ff);

return r;

public final static long getLong(byte[] buf, boolean asc)
if (buf == null)
throw new IllegalArgumentException("byte array is null!");

if (buf.length > 8)
throw new IllegalArgumentException("byte array size > 8 !");

long r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--)
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);

else
for (int i = 0; i < buf.length; i++)
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);

return r;


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/s_ongfei/archive/2007/06/14/1652740.aspx
参考技术B 将16进制转换为10进制,用integer类的parseInt()函数:Integer.parseInt("0x11",16));本回答被提问者采纳 参考技术C //写个简单的函数就行了。
public class StringNum

public static void main(String [] args)
String hex = "0x11";
System.out.println(StringNum.StrHexToInt(hex));

//这是转换函数
public static short StrHexToInt(String hex)
if(!hex.startsWith("0x") && !hex.startsWith("0X"))
return -1;
else
hex = hex.replaceFirst("0[x|X]", "");
return Short.parseShort(hex, 16);



参考技术D public static void main(String[] args)
int numStr = 0x10;
String num = numStr + "";
System.out.println(Integer.parseInt(num, 10));

java中如何让byte[]与string类型转换后,保持不变

在android中我从网络(通过图片网址)上获得几张图片后,我想进入下一个activity时,把这几张图片传过去,可是我想以列表list传过去,可是android的activity中intent中只能传arrayList<string>类型,我想将得到的byte【】转换成string后传过去,但到那边不可以用了。public class AnroidTestActivity extends Activity
private EditText imagePathText;
private static final String TAG="DataActivity";
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagePathText = (EditText) findViewById(R.id.editText1);
imageView=(ImageView)findViewById(R.id.imageView1);

//获取我的宝马车图片
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String path = imagePathText.getText().toString();
try

byte[] data=NetTool.getImage(path);
System.out.println(data.length);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bm);
String string=bytesToString(data);

List<String>list=new ArrayList<String>();
list.add(string);
System.out.println("转换后得到的数据:"+list.get(0));
System.out.println("转换后得到的数据:"+list.get(0).length());
Intent intent=new Intent();
intent.putStringArrayListExtra("list", (ArrayList<String>) list);
intent.setClass(AnroidTestActivity.this,TestIntent.class);
startActivity(intent);
catch (Exception e)
Log.i(TAG, e.toString());
Toast.makeText(AnroidTestActivity.this, "获得图片失败", 1).show();


);

// 获取网页源代码


//将byte【】类型转换成string类型
public static String bytesToString(byte[] b)
StringBuffer result = new StringBuffer("");
int length = b.length;
for (int i = 0; i < length; i++)
result.append((char)(b[i] & 0xff));

return result.toString();


public class TestIntent extends Activity
private ImageView imageView;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
imageView=(ImageView)findViewById(R.id.test_imageView1);
Intent intent=getIntent();
List<String> list=new ArrayList<String>();
list=(ArrayList<String>)intent.getStringArrayListExtra("list");
System.out.println("转换后得到的数据:"+list.get(0));
System.out.println("转换后得到的数据:"+list.get(0).length());
byte[]data= list.get(0).getBytes();
System.out.println(data.length);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bm);

/** * 将String转为byte数组 */
// public static byte[] stringToBytes(String s, int length)
// while (s.getBytes
//
// ().length < length)
// s += "";
//
// return s.getBytes();
//

String.getBytes()是取决于本地缺省编码的,两边不一样就抓瞎了。你这种情况其实是要传byte[],这样硬生生转成String总觉得太危险,一般的做法是弄成比如Base64这样的7bits编码。现成的有sun.misc.BASE64Encoder和sun.misc.BASE64Decoder。追问

怎么用啊

追答

编码就是
byte[] bs = ...;
BASE64Encoder enc = new BASE64Encoder();
String s = enc.encodeBuffer(bs);
解码就是
String s = ...;
BASE64Decoder dec = new BASE64Decoder();
byte[] newbs = dec.decodeBuffer(s);
不过android下有没有这两个包我不知道,但是Base64的编码解码器源码网上不少,也不是很长,自己加也可以。

参考技术A 建议不要转换成String,图片读取只用byte或者byte[]不会出错。
byte[]转换String时使用举例
byte[] bytes = new byte[](byte) 0x03,(byte) 0x04,;

String targetStr = new String(bytes, "UTF-8")
其中"UTF-8"是转码参数,不写的话会按系统默认转码,这就说明byte[]转换String时,
出现特殊字符,即半角英文 数字 半角符号以外的特殊字符,如汉字 特殊符号等时,
如果编码设定不对的话,二进制编码将无法保持一致。
代码中的方法public static String bytesToString(byte[] b) 应该一样存在这个问题。追问

那应该如何通过intent的来传递图片呢?如果是一张图片可以用byte【】传,可我一次传几张怎么做呀?

追答

你的问题我不是很清楚,你的意思是一次选取多个图形文件传输么?
如果是这个意思的话,有两种办法:
第一种,以单独传送图片的机制 循环发送。
第二种,传送时每个文件中间加特殊分割字段,类似于大型机数据的“数据头”。这时需要接受方有一样的解析方式,按照数据头分割文件。

参考技术B 只是一个思路,你在这个Activity里把流转换成Bitmap,再放到集合里,传过去。

以上是关于在java中 如何将得到的String转换为Short型 急。。的主要内容,如果未能解决你的问题,请参考以下文章

java中如何把一个文件转化为byte数组

java怎么将string转换成byte数组

在 Java 中,如何将 String 转换为 char 或将 char 转换为 String?

在java中,如何将byte转为string

高手请进!如何把整形数据转换为字符串(C语言)?

如何使用Gson将对象转换为String