Blog

Hexa class – format correct hexa string

This class will help you to get correct hexa string to save in a file. eg, 10 in decimal is “A” in hexadecimal, but when creating an hexa string to save it in a file you need to give the byte like “0A”.

Then you have word ( 2 bytes ) and DWord ( 4 bytes ). Lets say you want to save in hexa a width of your bitmap, saying 678. 678 in hexa is 2A6 and must be saved in a DWord ( 4 bytes ). The thing is that Words and DWords are read from right to left, but the bytes still from left to right. So you should have this to be correct: A6 02 00 00.

Anyway you don’t need to know all of this. With the class to get your hexa string just do Hexa.DWord( “2A6” ) and you’ll get the “A6020000” in result. Check the function in the class.
The getString will let you choose the length of the string, eg if you need to retreive the hexa for a pixel do like:
[as]
getString( _bmp.getPixel(0,0).toString( 16 ), 6 );
[/as]

[as]
/*
* Class written by Xavier MARTIN (xxlm or zeflasher)
* http://dev.webbymx.net
* http://www.webbymx.net
* If you are using this class, I will be glad to receive a picture of you front of your city hall ( or wherever outside )
* To do so please visit http://dev.webbymx.net and go in the about page to get my details…
* Licence: http://creativecommons.org/licenses/by-nc-sa/2.0/fr/deed.en
*/

class net.webbymx.files.Hexa {
/* ****************************************************************************
* CONSTRUCTOR
**************************************************************************** */
private function Hexa() {}

/* ****************************************************************************
* PUBLIC STATIC FUNCTIONS
**************************************************************************** */
private static function fillZeroBytes( s : String ) : String {
var l : Number;

if ( arguments[ 1 ] != undefined && typeof( arguments[ 1 ] ) == “number” ) l = arguments[ 1 ];
if ( l == undefined || ( l % 2 != 0 ) ) l = s.length + ( ( s.length % 2 ) )

for ( var i : Number = s.length; i < l; ++i ) s = s + "0"; return s; } /** * return the Hexa for a byte from * @param s : the hexa string passed to be encoded to be write in binary files * @return */ public static function getByte ( s : String ) : String { return getString( s, 2 ); } /** * return the Hexa for a Word ( 2 bytes ) from * @param s : the hexa string passed to be encoded to be write in binary files * @return the hexa string encoded */ public static function getWord ( s : String ) : String { return getString( s, 4 ); } /** * return the Hexa for a DWord ( 4 bytes ) from * @param s : the hexa string passed to be encoded to be write in binary files * @return the hexa string encoded */ public static function getDWord ( s : String ) : String { return getString( s, 8 ); } /** * Get the Hexa from * @param s : the hexa string passed to be encoded to be write in binary files * @param l : the length of the string to be returned * @return the hexa string encoded */ public static function getString( s : String, l : Number ) : String { var r : String = ""; // if string lenght > 0 then check for bytes and create the string to read from right to left
while ( s.length > 0 ) {
if ( s.length >= 2 ) {
r += s.substring( s.length-2 )
s = s.slice( 0, s.length-2 )
} else {
r += “0” + s;
s = “”;
}
}

// fille the rest of the string with zero
r = fillZeroBytes(r, l );

return r
}

}
[/as]

[Download id not defined]