hillpig的个人博客分享 http://blog.sciencenet.cn/u/hillpig 畅想ing,思考ing,前行ing Email:bluevaley@gmail.com

博文

如何获得Java无符号byte的值

已有 6088 次阅读 2014-9-16 17:58 |个人分类:杂七杂七加一|系统分类:科研笔记|关键词:学者

如何获得Java无符号byte的值?

   /**

    * byte被看作无符号值转换成short

    * @param x

    * @return

    */

   public static short getUnsignedByteValue(final byte x) {    

   ByteBuffer tmpBuffer = ByteBuffer.allocate(2);

   tmpBuffer.put(new byte[]{0x00,x});

   tmpBuffer.flip();

   tmpBuffer.order(ByteOrder.BIG_ENDIAN);            

   return tmpBuffer.getShort();

   }

例如:

byte x = (byte) 0xff;

short unsignedX = getUnsignedByteValue(x);


加我私人微信,交流技术。





https://m.sciencenet.cn/blog-419883-828246.html

上一篇:刷新你的思维-记10个打动了我的公司和产品
下一篇:错误解决: Failed to find: play-service-wearable+

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-17 20:18

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部