短字节和字节到Android的短期转换 [英] short to byte and byte to short conversion in Android

查看:94
本文介绍了短字节和字节到Android的短期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在Android中软件。在软件的特定部分,我需要转换为短字节,并重新转换为它的短。我试图跌破code,但值不转换后相同。

I am developing a software in Android. In a particular portion of software, I need to convert short to byte and re-convert to it to short. I tried below code but values are not same after conversion.

  short n, n1;
  byte b1, b2;
  n = 1200;
  // short to bytes conversion
  b1 = (byte)(n & 0x00ff);
  b2 = (byte)((n >> 8) & 0x00ff);

  // bytes to short conversion
  short n1 = (short)((short)(b1) | (short)(b2 << 8));

执行n和N1的code值后不一样的。为什么呢?

after executing the code values of n and n1 are not same. Why?

推荐答案

我没有得到解决格雷厄姆的工作。这一点,但是做的工作:

I did not get Grahams solution to work. This, however do work:

n1 = (short)((b1 & 0xFF) | b2<<8);

这篇关于短字节和字节到Android的短期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆