[英]Byte array to Date in Java
I have a byte array with 4 bytes taken from a header of a drv and these 4 bytes represent the timestamp of a videoframe. 我有一个字节数组,其中4个字节是从drv的标头中提取的,而这4个字节表示视频帧的时间戳。 So I need to convert these 4 bytes in a Date or a TimeStamp.
所以我需要在Date或TimeStamp中转换这4个字节。 Here is the array and the values that I got for the first frame:
byte[] array= {192, 242, 217, 84};
这是我在第一帧中得到的数组和值:
byte[] array= {192, 242, 217, 84};
I'll really appreciate any help. 我将非常感谢您的帮助。
byte[] array= {192, 242, 217, 84};
int timestamp = ByteBuffer.wrap(array).getInt();
Thanks everybody I already did it. 谢谢大家,我已经做到了。 Here you have the solution:
这里您有解决方案:
String binary=toBin(head[11])+toBin(head[10])+toBin(head[9])+toBin(head[8]);
long unixSeconds=stringToInt(binary);
Date date = new Date(unixSeconds*1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getDefault());
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
public static String toBin(byte numDec){
int num= UnsignedBytes.toInt(numDec);
if(num==0)
return "00000000";
String result=Integer.toBinaryString(num);
int cont= 8-result.length()%8;
if(cont!=8){
for(int i=0; i<cont;i++){
result="0"+result;
}
}
return result; }
public static long stringToInt(String bin){
long result=0;
bin= bin.trim();
for(int i=bin.length()-1;i>=0;i--){
String aux= String.valueOf(bin.charAt(i));
result+= Integer.parseInt(aux)* Math.pow(2, (bin.length()-1-i));
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.