瀏覽代碼

added hex

Nicolas Cannasse 19 年之前
父節點
當前提交
15434185b9
共有 1 個文件被更改,包括 16 次插入0 次删除
  1. 16 0
      std/StringTools.hx

+ 16 - 0
std/StringTools.hx

@@ -260,6 +260,22 @@ class StringTools {
 		return out.toString();
 		return out.toString();
 		#end
 		#end
 	}
 	}
+	
+	/**
+		Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
+	**/
+	public static function hex( n : Int, ?digits : Int ) {
+		var s = "";
+		var hexChars = "0123456789ABCDEF";
+		do {
+			s = hexChars.charAt(n%16) + s;
+			n = Std.int(n/16);
+		} while( n > 0 );
+		if( digits != null )
+			while( s.length < digits )
+				s = "0"+s;
+		return s;
+	}
 
 
 	#if neko
 	#if neko
 	private static var _urlEncode = neko.Lib.load("std","url_encode",1);
 	private static var _urlEncode = neko.Lib.load("std","url_encode",1);