java中

1 short = 2 byte

1 char  = 2 byte

1 int    = 4 byte

1 long = 8 byte

C语言中

typedef unsigned char byte;

在32 位的系统上short 咔出来的内存大小是2 个byte;
int 咔出来的内存大小是4 个byte;
long 咔出来的内存大小是4 个byte;
float 咔出来的内存大小是4 个byte;
double 咔出来的内存大小是8 个byte;
char 咔出来的内存大小是1 个byte。
(注意这里指一般情况,可能不同的平台还会有所不同,具体平台可以用sizeof 关键字测试一下)

import java.nio.ByteOrder;

public class BytesTransUtils {

     private String TAG = "BytesTransUtils";
private static BytesTransUtils instance = null; private BytesTransUtils() {
// Log.i(TAG, "instance BytesTransUtils");
} public static BytesTransUtils getInstance() {
if (instance == null) {
instance = new BytesTransUtils();
} return instance;
} public boolean testCPU() {
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
// System.out.println("is big ending");
return true;
} else {
// System.out.println("is little ending");
return false;
}
} public byte[] getBytes(short s, boolean bBigEnding) {
byte[] buf = new byte[2]; if (bBigEnding){
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 byte[] getBytes(int s, boolean bBigEnding) {
byte[] buf = new byte[4]; if (bBigEnding) {
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
} else {
System.out.println("1");
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
} return buf;
} public byte[] getBytes(long s, boolean bBigEnding) {
byte[] buf = new byte[8]; if (bBigEnding) {
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 short getShort(byte[] buf, boolean bBigEnding) {
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 (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
} return r;
} public int getInt(byte[] buf, boolean bBigEnding) {
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 (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
} return r;
} public long getLong(byte[] buf, boolean bBigEnding) {
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 (bBigEnding) {
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
} else {
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
} return r;
} /*----------------------------------------------------------*/
/* 对转换进行一个简单的封装 */
/*----------------------------------------------------------*/
public byte[] getBytes(int i) {
return getBytes(i, this.testCPU());
} public byte[] getBytes(short s) {
return getBytes(s, this.testCPU());
} public byte[] getBytes(long l) {
return getBytes(l, this.testCPU());
} public int getInt(byte[] buf) {
return getInt(buf, this.testCPU());
} public short getShort(byte[] buf) {
return getShort(buf, this.testCPU());
} public long getLong(byte[] buf) {
return getLong(buf, this.testCPU());
} /****************************************/
public short[] Bytes2Shorts(byte[] buf) {
byte bLength = 2;
short[] s = new short[buf.length / bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength]; for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
} s[iLoop] = getShort(temp);
} return s;
} public byte[] Shorts2Bytes(short[] s) {
byte bLength = 2;
byte[] buf = new byte[s.length * bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]); for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
} return buf;
} /****************************************/
public int[] Bytes2Ints(byte[] buf) {
byte bLength = 4;
int[] s = new int[buf.length / bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength]; for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
} s[iLoop] = getInt(temp); System.out.println("2out->"+s[iLoop]);
} return s;
} public byte[] Ints2Bytes(int[] s) {
byte bLength = 4;
byte[] buf = new byte[s.length * bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]); System.out.println("1out->"+s[iLoop]); for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
} return buf;
} /****************************************/
public long[] Bytes2Longs(byte[] buf) {
byte bLength = 8;
long[] s = new long[buf.length / bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = new byte[bLength]; for (int jLoop = 0; jLoop < bLength; jLoop++) {
temp[jLoop] = buf[iLoop * bLength + jLoop];
} s[iLoop] = getLong(temp);
} return s;
} public byte[] Longs2Bytes(long[] s) {
byte bLength = 8;
byte[] buf = new byte[s.length * bLength]; for (int iLoop = 0; iLoop < s.length; iLoop++) {
byte[] temp = getBytes(s[iLoop]); for (int jLoop = 0; jLoop < bLength; jLoop++) {
buf[iLoop * bLength + jLoop] = temp[jLoop];
}
} return buf;
} }

  

java中基本数据类型和C语言中基本数据类型转换的更多相关文章

  1. java 8 Stream中操作类型和peek的使用

    目录 简介 中间操作和终止操作 peek 结论 java 8 Stream中操作类型和peek的使用 简介 java 8 stream作为流式操作有两种操作类型,中间操作和终止操作.这两种有什么区别呢 ...

  2. Python3.x中bytes类型和str类型深入分析

    Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和b ...

  3. 关于 Go 中 Map 类型和 Slice 类型的传递

    关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...

  4. Java并发工具类CountDownLatch源码中的例子

    Java并发工具类CountDownLatch源码中的例子 实例一 原文描述 /** * <p><b>Sample usage:</b> Here is a pai ...

  5. iconv简介(1、字符串|文件字符转换:iconv用于将一种已知的字符集文件转换成另一种已知的字符集文件)(2、编程语言函数功能的相似性:iconv不仅再php中有用,而且c语言中也有用,还有linux等)

    iconv简介(1.字符串|文件字符转换:iconv用于将一种已知的字符集文件转换成另一种已知的字符集文件)(2.编程语言函数功能的相似性:iconv不仅再php中有用,而且c语言中也有用,还有lin ...

  6. Java中int类型和tyte[]之间转换及byte[]合并

    JAVA基于位移的 int类型和tyte[]之间转换 [java] view plaincopy /** * 基于位移的int转化成byte[] * @param int number * @retu ...

  7. Java中日期类型和mysql中日期类型进行整合

      1. java与mysql中日期.时间类型总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mysql(版本:5.1.50)的时间日期类型如下:   da ...

  8. java中XMLGregorianCalendar类型和Date类型之间的相互转换

    import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...

  9. MySQL中Decimal类型和Float Double的区别 & BigDecimal与Double使用场景

    MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,double等非标准类型,在DB中保存的是近似值,而Decimal则以字符串的形 ...

随机推荐

  1. 我眼中的C#3.0 摘自于网络:http://www.cnblogs.com/joinger/articles/1297237.html

    每次有新技术发布时,我们总能感受到两种截然不同的情绪: 一种是恐惧和抵抗,伴随着这种情绪的还有诸如"C# 2.0用的挺好的,为什么要在C# 3.0搞到那么复杂?"或者"我 ...

  2. vi命令的常用操作

    G:移动到底部 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件, ...

  3. Jquery树控件ZTree异步加载

    异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 这里面主要设计ztree的setting变量的async属性设置: var setting = { ...

  4. mysql 货币字段类型的存储

    loat类型是可以存浮点数(即小数类型),但是float有个坏处,当你给定的数据是整数的时候,那么它就以整数给你处理.这样我们在存取货币值的时候自然遇到问题,我的default值为:0.00而实际存储 ...

  5. Swift -> Optional嵌套 探讨

    准备运动:Optional 的介绍 王巍的<Swifter>一书中,介绍了一个有用的命令:在 LLDB 中输入 fr v -R foo,可以查看foo 这个变量的内存构成.我们稍后的分析将 ...

  6. 为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序

    在Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法.简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的 ...

  7. Listview的OnScrollListener的滑动监听实现分页加载

    //---------------主布局文件---------------------------- <ListView android:layout_width="fill_pare ...

  8. 查看log的方法

    adb logcat>1.txt adb shell cat /proc/atf_log/atf_log > atf_log 会保存在adb的本地文件. 还有一种是实时读取的方式: adb ...

  9. hb_gui配置heartbeat做MariaDB的高可用

    系统平台:CentOS release 6.5 (Final) Kernel:2.6.32-431.el6.x86_64 一.启动hb_gui hb_gui & 添加资源组 添加MySQL_I ...

  10. System.Web.Optimization找不到引用怎么办?

    Install-Package Microsoft.AspNet.Web.Optimization