Skip to content

Instantly share code, notes, and snippets.

@OhMeadhbh
Created July 14, 2014 19:22
Show Gist options
  • Save OhMeadhbh/21508f6fe4210b9f0877 to your computer and use it in GitHub Desktop.
Save OhMeadhbh/21508f6fe4210b9f0877 to your computer and use it in GitHub Desktop.
javascript utility functions
// function _pad()
//
// pads the input with count numbers of char characters. Examples:
// _pad( "7" ) => "7"
// _pad( "A", 4 ) => " A"
// _pad( "7", 4, "*" ) => "***7"
function _pad( input, count, char ) {
count = count ? count : 0;
return String( Array( count ).join( ( char ? char : ' ' ) ) + input ).slice( - count );
}
// function _hex_pad()
//
// returns a hex string from a number, padded with count zeros.
// _hex_pad( 12, 4 ) => "000C"
function _hex_pad( input, count ) {
return _pad( input.toString(16), count, '0' ).toUpperCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment