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:

Actionscript:
  1. getString( _bmp.getPixel(0,0).toString( 16 ), 6 );

Actionscript:
  1. /*
  2. * Class written by Xavier MARTIN (xxlm or zeflasher)
  3. * http://dev.webbymx.net
  4. * http://www.webbymx.net
  5. * If you are using this class, I will be glad to receive a picture of you front of your city hall ( or wherever outside )
  6. * To do so please visit http://dev.webbymx.net and go in the about page to get my details...
  7. * Licence: http://creativecommons.org/licenses/by-nc-sa/2.0/fr/deed.en
  8. */
  9.  
  10. class net.webbymx.files.Hexa {
  11. /* ****************************************************************************
  12. * CONSTRUCTOR
  13. **************************************************************************** */ 
  14.     private function Hexa() {}
  15.    
  16. /* ****************************************************************************
  17. * PUBLIC STATIC FUNCTIONS
  18. **************************************************************************** */
  19.     private static function fillZeroBytes( s : String ) : String {
  20.         var l : Number;
  21.        
  22.         if ( arguments[ 1 ] != undefined && typeof( arguments[ 1 ] ) == "number" ) l = arguments[ 1 ];
  23.         if ( l == undefined || ( l % 2 != 0 ) ) l = s.length + ( ( s.length % 2 ) )
  24.        
  25.         for ( var i : Number = s.length; i <l; ++i ) s = s + "0";
  26.        
  27.         return s;      
  28.     }
  29.    
  30. /**
  31. * return the Hexa for a byte from
  32. * @param    s   : the hexa string passed to be encoded to be write in binary files
  33. * @return
  34. */
  35.     public static function getByte ( s : String ) : String {
  36.         return getString( s, 2 );
  37.     }
  38.    
  39.    
  40. /**
  41. * return the Hexa for a Word ( 2 bytes ) from
  42. * @param    s   : the hexa string passed to be encoded to be write in binary files
  43. * @return   the hexa string encoded
  44. */ 
  45.     public static function getWord ( s : String ) : String {
  46.         return getString( s, 4 );
  47.     }
  48.  
  49.    
  50. /**
  51. * return the Hexa for a DWord ( 4 bytes ) from
  52. * @param    s   : the hexa string passed to be encoded to be write in binary files
  53. * @return   the hexa string encoded
  54. */
  55.     public static function getDWord ( s : String ) : String {
  56.         return getString( s, 8 );
  57.     }
  58.    
  59.    
  60. /**
  61. * Get the Hexa from
  62. * @param    s   : the hexa string passed to be encoded to be write in binary files
  63. * @param    l   : the length of the string to be returned
  64. * @return   the hexa string encoded
  65. */
  66.    
  67.     public static function getString( s : String, l : Number ) : String {
  68.         var r : String = "";
  69.        
  70.     //  if string lenght> 0 then check for bytes and create the string to read from right to left
  71.         while ( s.length> 0 ) {
  72.             if ( s.length>= 2 ) {
  73.                 r += s.substring( s.length-2 )
  74.                 s = s.slice( 0, s.length-2 )
  75.             } else {
  76.                 r += "0" + s;
  77.                 s = "";
  78.             }
  79.         }
  80.        
  81.     //  fille the rest of the string with zero
  82.         r = fillZeroBytes(r, l );
  83.        
  84.         return r
  85.     }
  86.    
  87. }

Hexa Class Download the class Unknown

Related posts:

  1. AS2 – Vector Class
  2. Exporting BitmapData in hexa for file saving using swf2exe soft ( like mdm Zinc )
  3. AS2 – Listening the event of a CLASS
  4. MDM Zinc – Class DateManager for ftp and filesystem
  5. MDM Zinc – Class FileSystemManager

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Tags: , ,

  • http://dev.webbymx.net/?p=51 Dev by MX» Blog Archive » Exporting BitmapData in hexa for file saving using swf2exe soft ( like mdm Zinc )

    [...] This class will help you to have a string in hexadecimal format to save it in a file. It creates the header of the file, the header of the bitmap and the data. This class require the Hexa class. Be sure you have it when compiling. [...]

  • http://dev.webbymx.net/2006/10/27/exporting-bitmapdata-in-hexa-for-file-saving-using-swf2exe-soft-like-mdm-zinc/ Exporting BitmapData in hexa for file saving using swf2exe soft ( like mdm Zinc ) | Dev by MX

    [...] file. It creates the header of the file, the header of the bitmap and the data. This class require the Hexa class. Be sure you have it when [...]

blog comments powered by Disqus