public class DataTypeChangeHelper
{
/**
* 將一個單字節(jié)的byte轉換成32位的int
*
* @param b byte
* @return convert result
*/
public static int unsignedByteToInt(byte b)
{
return (int) b & 0xFF;
}
/**
* 將一個單字節(jié)的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 數據類型轉化工具
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門职祷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來氏涩,“玉大人,你說我怎么就攤上這事有梆∈羌猓” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵泥耀,是天一觀的道長饺汹。 經常有香客問我,道長痰催,這世上最難降的妖魔是什么兜辞? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮夸溶,結果婚禮上逸吵,老公的妹妹穿的比我還像新娘。我一直安慰自己蜘醋,他們只是感情好胁塞,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著压语,像睡著了一般啸罢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胎食,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼递雀!你這毒婦竟也來了柄延?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布窍仰,位于F島的核電站,受9級特大地震影響礼殊,放射性物質發(fā)生泄漏驹吮。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一晶伦、第九天 我趴在偏房一處隱蔽的房頂上張望碟狞。 院中可真熱鬧,春花似錦婚陪、人聲如沸族沃。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽脆淹。三九已至,卻和暖如春沽一,著一層夾襖步出監(jiān)牢的瞬間盖溺,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 一鲫竞、基本類型的運算 1辐怕、字符串之間的運算 2、數字類型之間从绘,數字類型與字符串類型之間的運算 二寄疏、復雜類型的運算 三...
- 1:變量使用的注意事項 ①陕截、變量定義后可以不賦值,使用時再賦值批什。不賦值不能使用农曲。 ②、變量使用時有作用域的限制驻债。 ...