Skip to content

Instantly share code, notes, and snippets.

View hu2di's full-sized avatar

Huy-Hung Dinh hu2di

View GitHub Profile
@hu2di
hu2di / Singleton.java
Last active April 10, 2017 02:04
Java Design Pattern: Singleton
public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
@hu2di
hu2di / KeyHash.java
Created April 10, 2017 02:13
Generate Key Hash for Facebook SDK android
private void getKeyHash() {
try {
PackageInfo info = getPackageManager().getPackageInfo(
"your Application Id",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
@hu2di
hu2di / PathFromURI.java
Created April 10, 2017 02:24
Android: Get Real Path From URI
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
@hu2di
hu2di / MySubString.java
Created April 10, 2017 02:29
Java: SubString
public class MySubString {
public static void main(String args[]) {
String s1="HuyHung";
System.out.println(s1.substring(2,4));//return yH
System.out.println(s1.substring(2)); //return yHung
}
}
@hu2di
hu2di / Video.java
Created April 10, 2017 02:32
Android: List all Video on device
public static void printNameOfVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
vidsCount = c.getCount();
while (c.moveToNext()) {
Log.d("VIDEO", c.getString(0));
}
@hu2di
hu2di / LoadFacebook.js
Created April 10, 2017 02:34
Load Facebook SDK for JavaScript
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your facebook app id',
xfbml : true,
version: 'v2.6'
});
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s) [0];
@hu2di
hu2di / ConvertVie.js
Last active August 24, 2024 12:08
JavaScript: Chuyển tiếng Việt có dấu sang không dấu
function removeVietnameseTones(str) {
str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");
str = str.replace(/ì|í|ị|ỉ|ĩ/g,"i");
str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");
str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");
str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
str = str.replace(/đ/g,"d");
str = str.replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g, "A");
str = str.replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g, "E");
@hu2di
hu2di / ReadXML.js
Created April 10, 2017 02:36
Read XML file into Node.js as a JSON
//nstallation:
//npm install --save xml2js
//Usage:
//Read XML
var returnJSONResults = function(baseName, queryName) {
var XMLPath = "data.xml";
var rawJSON = loadXMLDoc(XMLPath);
function loadXMLDoc(filePath) {
var fs = require('fs');
var xml2js = require('xml2js');
@hu2di
hu2di / Print.cpp
Created April 10, 2017 02:40
printf - function isn't safe
#include<stdio.h>
int main()
{
char input[200];
while(1)
{
printf("\n\n");
gets(input);
printf(input);
}
@hu2di
hu2di / Convert.cs
Created April 10, 2017 02:42
C-sharp: Convert
int n;
n = int.Parse(Console.ReadLine());
double sum = 0.0;
for (int iCount = 1; iCount <= n; iCount++)
{
sum = sum + 1 / iCount;
}
Console.WriteLine("Ket qua: {0}", sum);