summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/Convert.java
blob: af7359ceb1b601be7b9e7d489f3aead27a75d1de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.macaroon.piztor;

import android.annotation.SuppressLint;

public class Convert {
	
	@SuppressLint("DefaultLocale")
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
				int pos = i * 2;
				d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	
	public static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
	
	public static byte[] intToBytes(int i) {   
		  byte[] d = new byte[4];   
		  d[0] = (byte)((i >> 24) & 0xFF);
		  d[1] = (byte)((i >> 16) & 0xFF);
		  d[2] = (byte)((i >> 8) & 0xFF); 
		  d[3] = (byte)(i & 0xFF);
		  return d;
	}
	
	public static byte[] doubleToBytes(double d){
		  byte[] b=new byte[8];
		  long l=Double.doubleToLongBits(d);
		  for(int i=0;i<8;i++){
			  b[i] = (byte)(l >>> 8*(7-i));
		  }
		  return b;
	}
	
	public static void write(byte[] s,byte[] w,int l) {
		
		for(int i=0;i<w.length;i++){
			s[i+l] = w[i];
		}
	}
	
	public static String byteToHexString(byte[] buffer){
		String p ="";
		for (int i = 0; i < buffer.length; i++) {
			   String hex = Integer.toHexString(buffer[i] & 0xFF);
			   if (hex.length() == 1) {
			    hex = '0' + hex;
			   }
			   p += hex;
		}
		return p;
	}
}