public class DataTypeChangeHelper
{
/**
* 将一个单字节的byte转换成32位的int
*
* @param b byte
* @return convert result
*/
public static int unsignedByteToInt(byte b)
{
return (int) b & 0xFF;
}
/**
* 将一个单字节的Byte转换成十六进制的数
*
* @param b byte
* @return convert result
*/
public static String byteToHex(byte b)
{
int i = b & 0xFF;
return Integer.toHexString(i);
}
/**
* 将一个4byte的数组转换成32位的int
*
* @param buf bytes buffer
* @param pos byte[]中开始转换的位置
* @return convert result
*/
public static long unsigned4BytesToInt(byte[] buf, int pos)
{
int firstByte = 0;
int secondByte = 0;
int thirdByte = 0;
int fourthByte = 0;
int index = pos;
firstByte = (0x000000FF & ((int) buf[index]));
secondByte = (0x000000FF & ((int) buf[index + 1]));
thirdByte = (0x000000FF & ((int) buf[index + 2]));
fourthByte = (0x000000FF & ((int) buf[index + 3]));
index = index + 4;
return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
}
/**
* 将16位的short转换成byte数组
*
* @param s short
* @return byte[] 长度为2
*/
public static byte[] shortToByteArray(short s)
{
byte[] targets = new byte[2];
for (int i = 0; i < 2; i++)
{
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* 将32位整数转换成长度为4的byte数组
*
* @param s int
* @return byte[]
*/
public static byte[] intToByteArray(int s)
{
byte[] targets = new byte[2];
for (int i = 0; i < 4; i++)
{
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* long to byte[]
*
* @param s long
* @return byte[]
*/
public static byte[] longToByteArray(long s)
{
byte[] targets = new byte[2];
for (int i = 0; i < 8; i++)
{
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* 32位int转byte[]
*/
public static byte[] int2byte(int res)
{
byte[] targets = new byte[4];
targets[0] = (byte) (res & 0xff);// 最低位
targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
return targets;
}
/**
* 将长度为2的byte数组转换为16位int
*
* @param res byte[]
* @return int
*/
public static int byte2int(byte[] res)
{
// res = InversionByte(res);
// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
return targets;
}
/**
* byte数组转short数组 小端储存
*
* @param src
* @return
*/
public static short[] byteArrayToShortArray(byte[] src)
{
int count = src.length >> 1;
short[] dest = new short[count];
for (int i = 0; i < count; i++)
{
dest[i] = (short) ((src[2 * i + 1] << 8) | (src[2 * i] & 0xff));
}
return dest;
}
/**
* short数组转byte数组 小端储存
*
* @param src
* @return
*/
public static byte[] shortArrayToByteArray(short[] src)
{
int count = src.length;
byte[] dest = new byte[count*2];
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte) (src[i] & 0xff);
dest[i * 2 + 1] = (byte) (src[i] >> 8 & 0xff);
}
return dest;
}
/**
* short数组转byte数组 小端储存
*
* @param src
* @return
*/
public static int shortArrayToByteArray(short[] src, byte[] outarry)
{
byte[] dest = outarry;
int count = outarry.length / 2 <= src.length ? outarry.length / 2 : src.length;
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte) (src[i] & 0xff);
dest[i * 2 + 1] = (byte) (src[i] >> 8 & 0xff);
}
return count * 2;
}
/**
* convert double signals to 16-bit pcm
*
* @param d double signals
* @param s 16-bit pcm
* @param numsamples no. of samples to be converted
*/
public static void double2Short(double[] d, short[] s, int numsamples)
{
for (int i = 0; i < numsamples; i++) s[i] = double2Short(d[i]);
}
/**
* convert a double signal sample to 16 bits pcm sample
*
* @param d a double sample
* @return
*/
public static short double2Short(double d)
{
if (d > 1.0)
{
d = 1.0;
}
else if (d < -1.0)
{
d = -1.0;
}
return (short) (d * Short.MAX_VALUE);
}
/**
* convert a yuan-byte short sample to double sample
*
* @param s 16 bits pcm sample
* @return
*/
public static double short2Double(short s)
{
return (double) s / Short.MAX_VALUE;
}
/**
* convert yuan-byte short samples to double samples
*
* @param s 16-bit samples
* @param d double samples
* @param numsamples no. of samples to be converted
*/
public static void short2Double(short[] s, double[] d, int numsamples)
{
for (int i = 0; i < numsamples; i++) d[i] = short2Double(s[i]);
}
/**
* convert a yuan-byte short sample to float sample
*
* @param s 16 bits pcm sample
* @return
*/
public static float short2Float(short s)
{
return (float) s / Short.MAX_VALUE;
}
/**
* convert 16-bit pcm to float
*
* @param s 16-bit pcm data
* @param f float data
* @param numsamples no. of samples to be converted
*/
public static void short2Float(short[] s, float[] f, int numsamples)
{
for (int i = 0; i < numsamples; i++) f[i] = short2Float(s[i]);
}
/**
* convert a float signal sample to 16 bits pcm sample
*
* @param f a float sample
* @return
*/
public static short float2Short(float f)
{
if (f > 1.0)
{
f = 1.0f;
}
else if (f < -1.0)
{
f = -1.0f;
}
return (short) (f * Short.MAX_VALUE);
}
/**
* convert float signal to 16-bit pcm
*
* @param f float signal
* @param s 16-bit pcm
* @param numsamples no. of samples to be converted
*/
public static void float2Short(float[] f, short[] s, int numsamples)
{
for (int i = 0; i < numsamples; i++) s[i] = float2Short(f[i]);
}
/**
* convert 24-bit pcm signal to souble samples
*
* @param orgBuffer
* 24-bit pcm samples
* @param outBuffer
* double samples
*/
private static boolean isPerGain = false;
public static void byteArray24_2_doubleArray(byte[] orgBuffer, double[] outBuffer)
{
int len = orgBuffer.length / 3 < outBuffer.length ? orgBuffer.length / 3 : outBuffer.length;
int i0, i1, i2;
double f;
int a;
for (int i = 0; i < len; i++)
{
i0 = (int) orgBuffer[3 * i + 0] & 0x000000ff;
i1 = (int) orgBuffer[3 * i + 1] & 0x000000ff;
i2 = (int) orgBuffer[3 * i + 2] & 0x000000ff;
//negative value, pad with 0xff
if ((i2 & 0x00000080) != 0)
{
i2 |= 0xffffff00;
}
i0 |= i2 << 16 | i1 << 8;
/**
* test
*/
if (isPerGain)
{
a = i % 8;
if (a >= 4)
{
i0 = i0 << 6;
}
// else
// i0 = 0;
}
f = (float) i0 / 16777216;
if (f < -1.0)
{
f = -1.0;
}
else if (f > 1.0)
{
f = 1.0;
}
outBuffer[i] = f;
}
}
public static void byteArray24_2_doubleArray_withPerGain(byte[] orgBuffer, double[] outBuffer)
{
isPerGain = true;
byteArray24_2_doubleArray(orgBuffer, outBuffer);
isPerGain = false;
}
/**
* convert 32-bit pcm signal to double samples
*
* @param orgBuffer 32-bit pcm samples
* @param outBuffer double samples
*/
public static void byteArray32_2_doubleArray(byte[] orgBuffer, double[] outBuffer)
{
int len = orgBuffer.length / 4 > outBuffer.length ? outBuffer.length : orgBuffer.length / 4;
int i0, i1, i2, i3;
double f;
for (int i = 0; i < len; i++)
{
/*
* merge to int
*/
i0 = (int) orgBuffer[i * 4 + 0] & 0x000000ff;
i1 = (int) orgBuffer[i * 4 + 1] & 0x000000ff;
i2 = (int) orgBuffer[i * 4 + 2] & 0x000000ff;
i3 = (int) orgBuffer[i * 4 + 3] & 0x000000ff;
//negative value, pad with 0xff
if ((i3 & 0x00000080) != 0)
{
i3 |= 0xffffff00;
}
i0 |= i3 << 24 | i2 << 16 | i1 << 8;
/*
* convert to double
*/
f = (float) i0 / Integer.MAX_VALUE;
if (f < -1.0)
{
f = -1.0;
}
else if (f > 1.0)
{
f = 1.0;
}
outBuffer[i] = f;
}
}
}
android 数据类型转化工具
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、基本类型的运算 1、字符串之间的运算 2、数字类型之间,数字类型与字符串类型之间的运算 二、复杂类型的运算 三...
- 1:变量使用的注意事项 ①、变量定义后可以不赋值,使用时再赋值。不赋值不能使用。 ②、变量使用时有作用域的限制。 ...