Skip to content

Instantly share code, notes, and snippets.

View chrishuan9's full-sized avatar
🎯
Focusing

Chris Huang chrishuan9

🎯
Focusing
  • Basel, Switzerland
View GitHub Profile
@chrishuan9
chrishuan9 / Hex2StringMain.java
Created April 17, 2012 20:47
Convert String to Hex and Hex to String
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author EtaYuy88
*/
public class Main {
@chrishuan9
chrishuan9 / String2Ascii.java
Created April 23, 2012 11:43
String 2 ASCII Converter
/*
Many network protocols and files store their characters with a byte-oriented character set such as ISO-8859-1 (ISO-Latin-1). However, Java's native character encoding is Unicode.
This example demonstrates how to convert ISO-8859-1 encoded bytes in a ByteBuffer to a string in a CharBuffer and visa versa.
In the example above, the encoding and decoding methods created new ByteBuffers into which to encode or decoding the data. Moreover, the newly allocated ByteBuffers are non-direct (Determining If a ByteBuffer Is Direct). The encoder and decoder provide methods that use a supplied ByteBuffer rather than create one. Here's an example that uses these methods:
*/
// Create the encoder and decoder for ISO-8859-1
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
public static byte[] getByteArray(String hexString) {
return BigInteger(hexString,16).toByteArray();
@chrishuan9
chrishuan9 / Hex2Byte.java
Created April 24, 2012 17:20
Oneline Hex 2 Byte Converter
import javax.xml.bind.DatatypeConverter;
public static String toHexString(byte[] array) {
return DatatypeConverter.printHexBinary(array);
}
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
@chrishuan9
chrishuan9 / Hex2Byte.java
Created April 24, 2012 17:32
Fast Hex 2 byte[]
//Source:http://www.rgagnon.com/javadetails/java-0596.html
//the simple way
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
@chrishuan9
chrishuan9 / Stringsearch.java
Created April 24, 2012 17:42
Search specific string in a text file
//Using a Scanner makes it easy to search a text file for a string in Java. You can find the code HERE
import java.io.*;
import java.util.Scanner;
public class FileSearch {
public static void main(String[] args) {
// Testing only
File f = new File(args[0]);
String search = args[1];
@chrishuan9
chrishuan9 / Alertdialog.java
Created April 24, 2012 17:43
Android Alert Dialog
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Hello World");
ad.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
@chrishuan9
chrishuan9 / gist:2516867
Created April 28, 2012 07:32
draw on surfaceview onDraw
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.restoreToCount(1);
canvas.clipRect(new Rect(0, 0, getWidth(), getHeight()));
canvas.drawCircle(x, y, 10, paint);
canvas.save();
}
float x,y;
@Override
public class LudoActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
FrameLayout Game = new FrameLayout(this);
GameView Gameview = new GameView (this);
LinearLayout GameWidgets = new LinearLayout (this);
@chrishuan9
chrishuan9 / gist:2850191
Created June 1, 2012 08:11
AB Shakerdemo
package ch.chrisii;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;