public class DataTypeChangeHelper
{
/**
* 將一個(gè)單字節(jié)的byte轉(zhuǎn)換成32位的int
*
* @param b byte
* @return convert result
*/
public static int unsignedByteToInt(byte b)
{
return (int) b & 0xFF;
}
/**
* 將一個(gè)單字節(jié)的Byte轉(zhuǎn)換成十六進(jìn)制的數(shù)
*
* @param b byte
* @return convert result
*/
public static String byteToHex(byte b)
{
int i = b & 0xFF;
return Integer.toHexString(i);
}
/**
* 將一個(gè)4byte的數(shù)組轉(zhuǎn)換成32位的int
*
* @param buf bytes buffer
* @param pos byte[]中開始轉(zhuǎn)換的位置
* @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轉(zhuǎn)換成byte數(shù)組
*
* @param s short
* @return byte[] 長(zhǎng)度為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位整數(shù)轉(zhuǎn)換成長(zhǎng)度為4的byte數(shù)組
*
* @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轉(zhuǎn)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);// 最高位,無符號(hào)右移。
return targets;
}
/**
* 將長(zhǎng)度為2的byte數(shù)組轉(zhuǎn)換為16位int
*
* @param res byte[]
* @return int
*/
public static int byte2int(byte[] res)
{
// res = InversionByte(res);
// 一個(gè)byte數(shù)據(jù)左移24位變成0x??000000,再右移8位變成0x00??0000
int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
return targets;
}
/**
* byte數(shù)組轉(zhuǎn)short數(shù)組 小端儲(chǔ)存
*
* @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數(shù)組轉(zhuǎn)byte數(shù)組 小端儲(chǔ)存
*
* @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數(shù)組轉(zhuǎn)byte數(shù)組 小端儲(chǔ)存
*
* @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 數(shù)據(jù)類型轉(zhuǎn)化工具
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 一、基本類型的運(yùn)算 1、字符串之間的運(yùn)算 2、數(shù)字類型之間,數(shù)字類型與字符串類型之間的運(yùn)算 二、復(fù)雜類型的運(yùn)算 三...
- 1:變量使用的注意事項(xiàng) ①、變量定義后可以不賦值,使用時(shí)再賦值。不賦值不能使用。 ②、變量使用時(shí)有作用域的限制。 ...
- 《雪落下聲音》 原創(chuàng)圖文:項(xiàng)宇紅 雪穿越地球 停泊中國(guó) 邂逅仙居泥鰍山公園 透過光的折射 漫天雪花空靈黑夜飄舞高...