<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7539392226778505626</id><updated>2024-09-14T11:24:57.910-07:00</updated><category term="ASN.1 code"/><category term="ASN.1 example code"/><category term="ASN.1 java code"/><category term="java"/><category term="StringTokenizer in j2me"/><category term="StringTokenizer j2me code"/><category term="StringTokenizer javame"/><category term="StringTokenizer javame code"/><category term="array"/><category term="bits from byte"/><category term="byte"/><category term="code"/><category term="equalIgnoreCase in j2me"/><category term="equalIgnoreCase in javame"/><category term="exception"/><category term="float in j2me"/><category term="float in javame"/><category term="hex"/><category term="image scaling in  javame"/><category term="image scaling in j2me"/><category term="iteration of bits in byte"/><category term="j2me custom font code"/><category term="java.util.zip example"/><category term="java.util.zip example code"/><category term="java.util.zip tutorial"/><category term="javame custom font code"/><category term="printStackTrace"/><category term="stacktrace"/><title type='text'>Example source code</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-8337679081788938858</id><published>2010-10-25T05:18:00.000-07:00</published><updated>2010-10-26T05:50:58.106-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="j2me custom font code"/><category scheme="http://www.blogger.com/atom/ns#" term="javame custom font code"/><title type='text'>Custom Font in J2ME</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;I have seen many posts where people asking help for creating custom font in j2me so i though to share an example for the people. The idea is to use self created custom font as an image and when required drawing of a string draw particular part of the image through clipping. Of course that particular part of the image will match the string we wish to draw. This way you can control the height and width of the image which j2me doesn&#39;t provide you. I have created an image in photo shop to be used as font.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhS3BCpcCtn0A_KoiU8fAwhnkhSn-riM34bReun-RZRenja0HmeeIBYDIuDvzSxeQmR0Xq3vosSLWBifyKD1Al4v4R6bc2p3D8217znPIfvQ1LfKfs8Hve-9-zxsnW0d08Q5pFI-MBgRk/s400/custom+font.png&quot; style=&quot;cursor:pointer; cursor:hand;width: 400px; height: 9px;&quot; border=&quot;0&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5531958481150269186&quot; /&gt;&lt;/div&gt;&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;import javax.microedition.lcdui.*;&lt;br /&gt;&lt;br /&gt;public class CustomFont {&lt;br /&gt;&lt;br /&gt;    private Image image;&lt;br /&gt;    private int[] widthMap;&lt;br /&gt;    private int[] positionMap;&lt;br /&gt;    private String characterMap;&lt;br /&gt;    public int height;&lt;br /&gt;    private int length;&lt;br /&gt;&lt;br /&gt;    public CustomFont() {&lt;br /&gt;        if (initialize()) {&lt;br /&gt;            this.height = 12;&lt;br /&gt;            length = characterMap.length();&lt;br /&gt;            this.positionMap = new int[length];&lt;br /&gt;            short xPos = 0;&lt;br /&gt;            int width = 0;&lt;br /&gt;            for (int i = 0; i &lt; length; i++) {&lt;br /&gt;                width = this.widthMap[i];&lt;br /&gt;                this.positionMap[i] = xPos;&lt;br /&gt;                xPos += width;&lt;br /&gt;            }&lt;br /&gt;        } else {&lt;br /&gt;            System.out.println(&quot;Could not initialize Custom Font&quot;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public boolean initialize() {&lt;br /&gt;&lt;br /&gt;        try {&lt;br /&gt;            this.image = Image.createImage(&quot;/custom font.png&quot;);&lt;br /&gt;            this.characterMap = &quot;@©®?$&amp;_+-&#39;\&quot;(),./\\:;!&lt;&gt;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%*|[]= &quot;;&lt;br /&gt;            this.widthMap = new int[]{12, 10, 10, 6, 6, 8, 8, 6, 4, 2, 4, 4,&lt;br /&gt;                        4, 2, 2, 4, 4, 2, 2, 2, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 8, 7, 8, 8, 7, 6, 8, 8, 2, 6, 8, 7, 8, 8, 8, 7, 8, 8, 7, 8,&lt;br /&gt;                        8, 8, 12, 8, 8, 8, 6, 6, 5, 6, 6, 5, 6, 6, 2, 3, 6, 2, 10, 6, 6, 6, 6, 4, 6, 4, 6, 6, 10, 6, 6, 6, 10, 6, 2, 3, 3, 7, 3&lt;br /&gt;                    };&lt;br /&gt;            return true;&lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void drawString(String str, int x, int y, Graphics graphics) {&lt;br /&gt;&lt;br /&gt;        int[] previousWidthMap;&lt;br /&gt;        int[] previousPositionsMap;&lt;br /&gt;        int[] indeces = null;&lt;br /&gt;&lt;br /&gt;        length = str.length();&lt;br /&gt;        indeces = new int[length];&lt;br /&gt;        for (int i = length - 1; i &gt;= 0; i--) {&lt;br /&gt;            char inputCharacter = str.charAt(i);&lt;br /&gt;            indeces[i] = indexOf(inputCharacter);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        previousPositionsMap = new int[indeces.length];&lt;br /&gt;        previousWidthMap = new int[indeces.length];&lt;br /&gt;&lt;br /&gt;        for (int i = 0; i &lt; indeces.length; i++) {&lt;br /&gt;            int index = indeces[i];&lt;br /&gt;            if (index == -1) {&lt;br /&gt;                // ignore, this is a character which is not available in the&lt;br /&gt;                // character-map&lt;br /&gt;            } else {&lt;br /&gt;&lt;br /&gt;                previousPositionsMap[i] = positionMap[index];&lt;br /&gt;                previousWidthMap[i] = widthMap[index];&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        int clipX = graphics.getClipX();&lt;br /&gt;        int clipY = graphics.getClipY();&lt;br /&gt;        int clipWidth = graphics.getClipWidth();&lt;br /&gt;        int clipHeight = graphics.getClipHeight();&lt;br /&gt;        for (int i = 0; i &lt; previousWidthMap.length; i++) {&lt;br /&gt;            int characterWidth = previousWidthMap[i];&lt;br /&gt;            graphics.clipRect(x, y, characterWidth, this.height);&lt;br /&gt;            int imageX = x - previousPositionsMap[i];&lt;br /&gt;            int imageY = y;&lt;br /&gt;            graphics.drawImage(this.image, imageX, imageY, Graphics.TOP | Graphics.LEFT);&lt;br /&gt;            x += characterWidth;&lt;br /&gt;&lt;br /&gt;            graphics.setClip(clipX, clipY, clipWidth, clipHeight);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public int stringWidth(String str) {&lt;br /&gt;        length = str.length();&lt;br /&gt;        int width = 0;&lt;br /&gt;        for (int i = length - 1; i&lt;br /&gt;                &gt;= 0; i--) {&lt;br /&gt;            char inputCharacter = str.charAt(i);&lt;br /&gt;            int charactorMapIndex = this.characterMap.indexOf(inputCharacter);&lt;br /&gt;            if (charactorMapIndex == -1) {&lt;br /&gt;                charactorMapIndex = this.characterMap.indexOf(&quot;?&quot;);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            width = width + this.widthMap[charactorMapIndex];&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return width;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public int getHeight() {&lt;br /&gt;        return height;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private int indexOf(char inputCharacter) {&lt;br /&gt;        return characterMap.indexOf(inputCharacter);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/blockqoute&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/8337679081788938858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/custom-font-in-j2me.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/8337679081788938858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/8337679081788938858'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/custom-font-in-j2me.html' title='Custom Font in J2ME'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhS3BCpcCtn0A_KoiU8fAwhnkhSn-riM34bReun-RZRenja0HmeeIBYDIuDvzSxeQmR0Xq3vosSLWBifyKD1Al4v4R6bc2p3D8217znPIfvQ1LfKfs8Hve-9-zxsnW0d08Q5pFI-MBgRk/s72-c/custom+font.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-6205057059407212078</id><published>2010-10-25T03:03:00.000-07:00</published><updated>2010-10-26T05:52:20.065-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java.util.zip example"/><category scheme="http://www.blogger.com/atom/ns#" term="java.util.zip example code"/><category scheme="http://www.blogger.com/atom/ns#" term="java.util.zip tutorial"/><title type='text'>java.util.zip example</title><content type='html'>So many of you not aware how to use java.util.zip package to compress and uncompress the data. So here are some utility methods for your understanding&lt;br /&gt;&lt;br /&gt;public static byte [] zip (byte[] uncompressedData, int off, int len)&lt;br /&gt;  {&lt;br /&gt;      Deflater compressor = new Deflater();&lt;br /&gt;      compressor.setLevel(Deflater.BEST_COMPRESSION);&lt;br /&gt;      compressor.setInput(uncompressedData, off, len);&lt;br /&gt;      compressor.finish();&lt;br /&gt;&lt;br /&gt;      ByteArrayOutputStream bos = new ByteArrayOutputStream(len);&lt;br /&gt;&lt;br /&gt;      byte[] buf = new byte[1024];&lt;br /&gt;      while (!compressor.finished())&lt;br /&gt;      {&lt;br /&gt;          int count = compressor.deflate(buf);&lt;br /&gt;          bos.write(buf, 0, count);&lt;br /&gt;      }&lt;br /&gt;      try&lt;br /&gt;      {&lt;br /&gt;          bos.close();&lt;br /&gt;      }&lt;br /&gt;      catch (IOException e)&lt;br /&gt;      {&lt;br /&gt;      }&lt;br /&gt;      return bos.toByteArray();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static byte [] unzip (byte [] compressedData, int off, int len)&lt;br /&gt;  {&lt;br /&gt;      Inflater decompressor = new Inflater();&lt;br /&gt;      decompressor.setInput(compressedData, off, len);&lt;br /&gt;&lt;br /&gt;      ByteArrayOutputStream bos = null;&lt;br /&gt;      bos = new ByteArrayOutputStream(len);&lt;br /&gt;&lt;br /&gt;      // Decompress the data&lt;br /&gt;      byte[] buf = new byte[1024];&lt;br /&gt;      while (!decompressor.finished())&lt;br /&gt;      {&lt;br /&gt;          try&lt;br /&gt;          {&lt;br /&gt;              int count = decompressor.inflate(buf);&lt;br /&gt;              bos.write(buf, 0, count);&lt;br /&gt;          }&lt;br /&gt;          catch (DataFormatException e)&lt;br /&gt;          {&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;      try&lt;br /&gt;      {&lt;br /&gt;          bos.close();&lt;br /&gt;      }&lt;br /&gt;      catch (IOException e)&lt;br /&gt;      {&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      return bos.toByteArray();&lt;br /&gt;  }</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/6205057059407212078/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/javautilzip-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/6205057059407212078'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/6205057059407212078'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/javautilzip-example.html' title='java.util.zip example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-1067156947023136812</id><published>2010-10-25T02:59:00.000-07:00</published><updated>2010-10-25T03:01:49.064-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="exception"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="printStackTrace"/><category scheme="http://www.blogger.com/atom/ns#" term="stacktrace"/><title type='text'>capture stack trace in string</title><content type='html'>So may of you want to see the complete stack trace in logs. Not happy with Exception.getMessage(). So here is a way to convert the stack trace to string so now you can enjoy the printStackTrace functionality in your logs&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;     * Capture Stack Trace into a String&lt;br /&gt;     */&lt;br /&gt;    public static String captureStackTrace(Throwable exception)&lt;br /&gt;    {&lt;br /&gt;        StringBuffer stackTrace = new StringBuffer();&lt;br /&gt;        stackTrace.append(&quot;Exception in thread \&quot;&quot;);&lt;br /&gt;        stackTrace.append(Thread.currentThread());&lt;br /&gt;        stackTrace.append(&quot;\&quot; &quot;);&lt;br /&gt;        stackTrace.append(exception);&lt;br /&gt;        stackTrace.append(&quot;\n&quot;);&lt;br /&gt;&lt;br /&gt;        StackTraceElement elements[] = exception.getStackTrace();&lt;br /&gt;        for (int x = 0; x &lt; elements.length; x++)&lt;br /&gt;        {&lt;br /&gt;            stackTrace.append(elements[x].toString());&lt;br /&gt;            stackTrace.append(&quot;\n&quot;);&lt;br /&gt;        }&lt;br /&gt;        return stackTrace.toString();&lt;br /&gt;    }</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/1067156947023136812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/capture-stack-trace-in-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1067156947023136812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1067156947023136812'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/capture-stack-trace-in-string.html' title='capture stack trace in string'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-7642451025006027323</id><published>2010-10-25T02:53:00.000-07:00</published><updated>2010-10-26T05:53:08.044-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="image scaling in  javame"/><category scheme="http://www.blogger.com/atom/ns#" term="image scaling in j2me"/><title type='text'>Scaling Image in J2ME</title><content type='html'>/**&lt;br /&gt; * Creates a new, scaled version of the given image.&lt;br /&gt; *&lt;br /&gt; * @param src: The source image&lt;br /&gt; * @param dstW: The destination (scaled) image width&lt;br /&gt; * @param dstH: The destination (scaled) image height&lt;br /&gt; * @return Image: A new Image object with the given width and height.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;        public static Image scaleImage (Image src, int dstW, int dstH) {&lt;br /&gt;                int srcW = src.getWidth();&lt;br /&gt;                int srcH = src.getHeight();&lt;br /&gt;&lt;br /&gt;                Image tmp = Image.createImage(dstW, srcH);&lt;br /&gt;                Graphics g = tmp.getGraphics();&lt;br /&gt;&lt;br /&gt;                int delta = (srcW &lt;&lt; 16) / dstW;&lt;br /&gt;                int pos = delta/2;&lt;br /&gt;&lt;br /&gt;                for (int x = 0; x &lt; dstW; x++) {&lt;br /&gt;                        g.setClip(x, 0, 1, srcH);&lt;br /&gt;                        g.drawImage(src, x - (pos &gt;&gt; 16), 0, Graphics.LEFT | Graphics.TOP);&lt;br /&gt;                        pos += delta;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                Image dst = Image.createImage(dstW, dstH);&lt;br /&gt;                g = dst.getGraphics();&lt;br /&gt;&lt;br /&gt;                delta = (srcH &lt;&lt; 16) / dstH;&lt;br /&gt;                pos = delta/2;&lt;br /&gt;&lt;br /&gt;                for (int y = 0; y &lt; dstH; y++) {&lt;br /&gt;                        g.setClip(0, y, dstW, 1);&lt;br /&gt;                        g.drawImage(tmp, 0, y - (pos &gt;&gt; 16), Graphics.LEFT | Graphics.TOP);&lt;br /&gt;                        pos += delta;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                return dst;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/7642451025006027323/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/scaling-image-in-j2me.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/7642451025006027323'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/7642451025006027323'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/scaling-image-in-j2me.html' title='Scaling Image in J2ME'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-5159080571769731974</id><published>2010-10-25T02:47:00.000-07:00</published><updated>2010-10-26T05:54:06.196-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="StringTokenizer in j2me"/><category scheme="http://www.blogger.com/atom/ns#" term="StringTokenizer j2me code"/><category scheme="http://www.blogger.com/atom/ns#" term="StringTokenizer javame"/><category scheme="http://www.blogger.com/atom/ns#" term="StringTokenizer javame code"/><title type='text'>StringTokenizer in J2me</title><content type='html'>So in javame/ j2me StringTokenizer is also not available. I have written some utility methods for this purpose&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;/**&lt;br /&gt;* stringTokenizer&lt;br /&gt;* Tokenize the string on the basis of parmeter token&lt;br /&gt;* remember empty spaces will not be return as tokens&lt;br /&gt;*&lt;br /&gt;* @param str String&lt;br /&gt;* @param token char&lt;br /&gt;* @return Vector&lt;br /&gt;*/&lt;br /&gt;public static Vector stringTokenizer(String str, char token)&lt;br /&gt;{&lt;br /&gt;/* TOKENIZE THE STRING ON THE BASIS OF THAT CHARACTER*/&lt;br /&gt;Vector temp = new Vector();&lt;br /&gt;str = str.trim();&lt;br /&gt;&lt;br /&gt;char[] stringCharacters = str.toCharArray();&lt;br /&gt;int j = 0;&lt;br /&gt;for (int i = 0; i &lt;&gt;&lt;br /&gt;{&lt;br /&gt;if (stringCharacters[i] == token)&lt;br /&gt;{&lt;br /&gt;if (new String(stringCharacters, j, i - j).trim().length() &gt; 0)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;temp.addElement(new String(stringCharacters, j, i - j));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;j = i + 1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;if (j &lt;&gt;&lt;br /&gt;{&lt;br /&gt;temp.addElement(new String(stringCharacters, j,&lt;br /&gt;stringCharacters.length - j));&lt;br /&gt;}&lt;br /&gt;return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/***************************************************************************/&lt;br /&gt;/**&lt;br /&gt;* stringTokenizer&lt;br /&gt;* This is overloaded function of stringTokenizer it&lt;br /&gt;* can tokenize on the basis of String also&lt;br /&gt;* Tokenize the string on the basis of parmeter token&lt;br /&gt;* remember empty spaces will not be return as tokens&lt;br /&gt;*&lt;br /&gt;* @param str String&lt;br /&gt;* @param token char&lt;br /&gt;* @return Vector&lt;br /&gt;*/&lt;br /&gt;public static Vector stringTokenizer(String str, String token)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Vector temp = new Vector();&lt;br /&gt;int index;&lt;br /&gt;str = str.trim();&lt;br /&gt;index = str.indexOf(token);&lt;br /&gt;&lt;br /&gt;while (index != -1)&lt;br /&gt;{&lt;br /&gt;if (str.substring(0, index).trim().length() &gt; 0)&lt;br /&gt;{&lt;br /&gt;temp.addElement(str.substring(0, index));&lt;br /&gt;}&lt;br /&gt;str = str.substring(index + token.length(), str.length());&lt;br /&gt;index = str.indexOf(token);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;if (index == -1 &amp;&amp; str.trim().length() &gt; 0)&lt;br /&gt;{&lt;br /&gt;temp.addElement(str);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/***************************************************************************/&lt;br /&gt;/**&lt;br /&gt;* stringTokenizerWithSpaces&lt;br /&gt;* Tokenize the string on the basis of parmeter token&lt;br /&gt;* here string is trimed from right side so symbol is&lt;br /&gt;* included without spaces&lt;br /&gt;* but empty spaces&lt;br /&gt;* inside string are included as tokens&lt;br /&gt;* This method specially written for the cases&lt;br /&gt;* like &quot;PSO,,&quot; Here this method return 3 tokens while&lt;br /&gt;* other &#39;stringTokenizer(String,char)&#39; will return only one&lt;br /&gt;* @param str String&lt;br /&gt;* @param token char&lt;br /&gt;* @return Vector&lt;br /&gt;*/&lt;br /&gt;public static Vector stringTokenizerWithSpaces(String str, char token)&lt;br /&gt;{&lt;br /&gt;/* TOKENIZE THE STRING ON THE BASIS OF THAT CHARACTER*/&lt;br /&gt;Vector temp = new Vector();&lt;br /&gt;str = str.trim();&lt;br /&gt;&lt;br /&gt;char[] stringCharacters = str.toCharArray();&lt;br /&gt;int j = 0;&lt;br /&gt;for (int i = 0; i &lt;&gt;&lt;br /&gt;{&lt;br /&gt;if (stringCharacters[i] == token)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (i &gt; 0)&lt;br /&gt;{&lt;br /&gt;temp.addElement(new String(stringCharacters, j, i - j));&lt;br /&gt;}&lt;br /&gt;j = i + 1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* FOR INCLUDING LAST TOKEN&lt;br /&gt;* */&lt;br /&gt;if (j &lt;&gt;&lt;br /&gt;{&lt;br /&gt;temp.addElement(new String(stringCharacters, j,&lt;br /&gt;stringCharacters.length - j));&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* IF LAST CHARACTER IS TOKEN ITSELF THEN WE LL ADD EMPTY STRING FOR IT&lt;br /&gt;* */&lt;br /&gt;if (stringCharacters[stringCharacters.length - 1] == token)&lt;br /&gt;{&lt;br /&gt;temp.addElement(&quot;&quot;);&lt;br /&gt;}&lt;br /&gt;return temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/blockqoute&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/5159080571769731974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/stringtokenizer-in-j2me.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/5159080571769731974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/5159080571769731974'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/stringtokenizer-in-j2me.html' title='StringTokenizer in J2me'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-765139542074733506</id><published>2010-10-25T02:27:00.000-07:00</published><updated>2010-10-26T05:57:27.303-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="equalIgnoreCase in j2me"/><category scheme="http://www.blogger.com/atom/ns#" term="equalIgnoreCase in javame"/><category scheme="http://www.blogger.com/atom/ns#" term="float in j2me"/><category scheme="http://www.blogger.com/atom/ns#" term="float in javame"/><title type='text'>J2ME tips</title><content type='html'>Here are some tips/ tricks for the very simple problems&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Float in CLDC1.0 :&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;This is the method i wrote years ago when needed to work in CLDC 1.0 with Float. As you might be aware that Float is not supported in CLDC1.0 so it compares the 2 float values given it as String. I have not revised the code so there would be a lot of room to optimize it&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;public static boolean compareFloats(String str1, String str2)&lt;/div&gt;&lt;div&gt;    {&lt;/div&gt;&lt;div&gt;        final char DECIMAL = &#39;.&#39;;&lt;/div&gt;&lt;div&gt;        int decimal = (int) DECIMAL;&lt;/div&gt;&lt;div&gt;        str1 = str1.trim();&lt;/div&gt;&lt;div&gt;        str2 = str2.trim();&lt;/div&gt;&lt;div&gt;        int firstDecimalPosition = str1.indexOf(decimal);&lt;/div&gt;&lt;div&gt;        int secondDecimalPosition = str2.indexOf(decimal);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        if (firstDecimalPosition == -1)&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;            str1 = str1.concat(&quot;.0&quot;);&lt;/div&gt;&lt;div&gt;            firstDecimalPosition = str1.indexOf(decimal);&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;        if (secondDecimalPosition == -1)&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;            str2 = str2.concat(&quot;.0&quot;);&lt;/div&gt;&lt;div&gt;            secondDecimalPosition = str2.indexOf(decimal);&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        if (str1.substring(0, firstDecimalPosition).equals(&quot;0&quot;) &amp;amp;&amp;amp;&lt;/div&gt;&lt;div&gt;            str2.substring(0, secondDecimalPosition).equals(&quot;-0&quot;))&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            return true;&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        if (str1.substring(0, firstDecimalPosition).equals(&quot;-0&quot;) &amp;amp;&amp;amp;&lt;/div&gt;&lt;div&gt;            str2.substring(0, secondDecimalPosition).equals(&quot;0&quot;))&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            return false;&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        int firstValue = Integer.parseInt(str1.substring(0,&lt;/div&gt;&lt;div&gt;                firstDecimalPosition));&lt;/div&gt;&lt;div&gt;        int secondValue = Integer.parseInt(str2.substring(0,&lt;/div&gt;&lt;div&gt;                secondDecimalPosition));&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        if (firstValue &gt; secondValue)&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            return true;&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;        else if (firstValue &lt;&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;            return false;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;        else&lt;/div&gt;&lt;div&gt;        {&lt;/div&gt;&lt;div&gt;            String firstValAfterDec = str1.substring(firstDecimalPosition + 1,&lt;/div&gt;&lt;div&gt;                    str1.length()).trim();&lt;/div&gt;&lt;div&gt;            String secondValAfterDec = str2.substring(secondDecimalPosition + 1,&lt;/div&gt;&lt;div&gt;                    str2.length()).trim();&lt;/div&gt;&lt;div&gt;            int length1 = firstValAfterDec.length();&lt;/div&gt;&lt;div&gt;            int length2 = secondValAfterDec.length();&lt;/div&gt;&lt;div&gt;            int difference;&lt;/div&gt;&lt;div&gt;            if (firstValAfterDec.length() &lt;&gt;&lt;div&gt;            {&lt;/div&gt;&lt;div&gt;                difference = length2 - length1;&lt;/div&gt;&lt;div&gt;                for (int i = 0; i &lt;&gt;&lt;div&gt;                {&lt;/div&gt;&lt;div&gt;                    firstValAfterDec = firstValAfterDec + &quot;0&quot;;&lt;/div&gt;&lt;div&gt;                }&lt;/div&gt;&lt;div&gt;            }&lt;/div&gt;&lt;div&gt;            if (secondValAfterDec.length() &lt;&gt;&lt;div&gt;            {&lt;/div&gt;&lt;div&gt;                difference = length1 - length2;&lt;/div&gt;&lt;div&gt;                for (int i = 0; i &lt;&gt;&lt;div&gt;                {&lt;/div&gt;&lt;div&gt;                    secondValAfterDec = secondValAfterDec + &quot;0&quot;;&lt;/div&gt;&lt;div&gt;                }&lt;/div&gt;&lt;div&gt;            }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            firstValue = Integer.parseInt(firstValAfterDec);&lt;/div&gt;&lt;div&gt;            secondValue = Integer.parseInt(secondValAfterDec);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            if (firstValue &gt; secondValue)&lt;/div&gt;&lt;div&gt;            {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;                return true;&lt;/div&gt;&lt;div&gt;            }&lt;/div&gt;&lt;div&gt;            else if (firstValue &lt;&gt;&lt;div&gt;            {&lt;/div&gt;&lt;div&gt;                return false;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            }&lt;/div&gt;&lt;div&gt;            else&lt;/div&gt;&lt;div&gt;            /** if both value are equal return false*/&lt;/div&gt;&lt;div&gt;            {&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;                return false;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;            }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;        }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;    }&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;equalIqnoreCase in CLDC 1.0:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Again fairly simple &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;  public static boolean equalStrings(String first, String second)&lt;/div&gt;&lt;div&gt;    {&lt;/div&gt;&lt;div&gt;        first = first.trim().toLowerCase();&lt;/div&gt;&lt;div&gt;        second = second.trim().toLowerCase();&lt;/div&gt;&lt;div&gt;        return first.equals(second);&lt;/div&gt;&lt;div&gt;    }&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/765139542074733506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/j2me-tips.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/765139542074733506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/765139542074733506'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/j2me-tips.html' title='J2ME tips'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-858219829320971076</id><published>2010-10-25T02:06:00.000-07:00</published><updated>2010-10-26T05:55:38.303-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 example code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 java code"/><title type='text'>ASN.1 Continued 2</title><content type='html'>&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;ASN.1 protocol&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;So once you have read the &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/asn1.html&quot;&gt;tag &lt;/a&gt;and the &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/continuing-on-previous-post-reading.html&quot;&gt;length&lt;/a&gt;; the rest is simple. Read the bytes from the stream according to the &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/continuing-on-previous-post-reading.html&quot;&gt;length&lt;/a&gt;. This will be the value of that &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/asn1.html&quot;&gt;tag&lt;/a&gt;. Now you can interpret that accordingly. For example if you an element version in your stream &lt;div&gt;&lt;ol&gt;&lt;li&gt;First you will read that tag&lt;/li&gt;&lt;li&gt;Then you will read the length ( Length is actually the number of the bytes for the in which value of version is stored)&lt;/li&gt;&lt;li&gt;Then you will read the number of bytes according to the length and this will be value of the your &#39;version tag&#39;&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Thats it from my side on ASN.1 protocol using BER ( basic encoding rule). If you have any questions, queries please do write me in the comments section&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/858219829320971076/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/asn1-continued-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/858219829320971076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/858219829320971076'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/asn1-continued-2.html' title='ASN.1 Continued 2'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-7707798281606824175</id><published>2010-10-22T08:13:00.000-07:00</published><updated>2010-10-26T05:55:21.308-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 example code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 java code"/><title type='text'>ASN.1 Continued</title><content type='html'>&lt;div&gt;Continuing on the &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/asn1.html&quot;&gt;previous post&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Reading Length in ASN.1&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;private int readLength(DataInputStream inputStream)&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;byte[] bytes = readBytes(inputStream, 1);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;BitIterator iterator = new BitIterator(bytes);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;String mostSignificant = iterator.getBits(1);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;String value = iterator.getBits(7);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;int length = Integer.parseInt(value, 2);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;if (!mostSignificant.equals(&quot;0&quot;))&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;{&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;   &lt;/span&gt;bytes = readBytes(inputStream, length);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;   &lt;/span&gt;value = byteArraytoHex(bytes);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;   &lt;/span&gt;length = Integer.parseInt(value, 16);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Length of the record  &quot; + length);&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;return length;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/7707798281606824175/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/continuing-on-previous-post-reading.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/7707798281606824175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/7707798281606824175'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/continuing-on-previous-post-reading.html' title='ASN.1 Continued'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-1359471024724058330</id><published>2010-10-22T05:20:00.000-07:00</published><updated>2010-10-26T05:54:53.554-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 example code"/><category scheme="http://www.blogger.com/atom/ns#" term="ASN.1 java code"/><title type='text'>ASN.1</title><content type='html'>A few days earlier I have to work on a system using ASN.1 protocol. I did some research to find a solution written in java for BER so that i can test the files encoded through ASN.1; to my surprise the libraries I found further confuse me. I didn&#39;t want to blame any one but people have written more complicated things on the subject thus making more confusing for a beginner to understand what the protocol is and how he should write some code to start. So I decided to write a simple piece of code myself.&lt;div&gt;I am not writing here about the ASN.1 protocol itself; I am writing a piece of code to start decoding a file in ASN.1 protocol using BER. The structure of ASN.1 is like TLV ( which is tag, length, value). You can read more about &lt;a href=&quot;http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One&quot;&gt;ASN.1&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Basic_Encoding_Rules&quot;&gt;BER&lt;/a&gt; at wiki. &lt;/div&gt;&lt;div&gt;&lt;b&gt;Reading tag of ASN.1&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;private String readTag(DataInputStream inputStream)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte[] bytes = readBytes(inputStream, 1);&lt;br /&gt;&lt;/blockqoute&gt;&lt;br /&gt;Of course we can easily read bytes to a byte array, then covert this byte array to hex like&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;String hexValue = byteArraytoHex(bytes).trim();&lt;br /&gt;&lt;/blockqoute&gt;&lt;br /&gt;&lt;br /&gt;If you need help converting byte array to hex please take a look at previous post &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/byte-array-to-hex.html&quot;&gt;here&lt;/a&gt;&lt;br /&gt;Rest is simple&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;int tagClass = (bytes[0] &amp;amp; 0xC0) &gt;&gt; 6;&lt;br /&gt;&lt;br /&gt; int tagType = (bytes[0] &amp;amp; 0x20) &gt;&gt; 5;&lt;br /&gt; int tag = bytes[0] &amp;amp; 0x1F;&lt;br /&gt; if (tag == 31)&lt;br /&gt; {&lt;br /&gt;  StringBuffer buffer = new                    StringBuffer(byteArraytoHex(bytes).trim());&lt;br /&gt;  bytes = readBytes(inputStream, 1);&lt;br /&gt; &lt;br /&gt;  buffer.append(byteArraytoHex(bytes).trim());&lt;br /&gt;  BitIterator iterator = new BitIterator(bytes);&lt;br /&gt;  String mostSignificant = iterator.getBits(1);&lt;br /&gt;  while (!mostSignificant.equals(&quot;0&quot;))&lt;br /&gt;  {&lt;br /&gt;   bytes = readBytes(inputStream, 1);&lt;br /&gt;  &lt;br /&gt;   buffer.append(byteArraytoHex(bytes).trim());&lt;br /&gt;   iterator = new BitIterator(bytes);&lt;br /&gt;   mostSignificant = iterator.getBits(1);&lt;br /&gt;  }&lt;br /&gt;  hexValue = buffer.toString();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;//  System.out.print(&quot;Tag Class = &quot;);&lt;br /&gt;//  switch (tagClass)&lt;br /&gt;//  {&lt;br /&gt;//  case 0:&lt;br /&gt;//   System.out.print(&quot;Universal&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  case 1:&lt;br /&gt;//   System.out.print(&quot;Application&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  case 2:&lt;br /&gt;//   System.out.print(&quot;Context-Specific&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  case 3:&lt;br /&gt;//   System.out.print(&quot;Private&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  }&lt;br /&gt;//&lt;br /&gt;//  System.out.print(&quot; ------ Type = &quot;);&lt;br /&gt;//&lt;br /&gt;//  switch (tagType)&lt;br /&gt;//  {&lt;br /&gt;//  case 0:&lt;br /&gt;//   System.out.print(&quot;Primitive&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  case 1:&lt;br /&gt;//   System.out.print(&quot;Constructed&quot;);&lt;br /&gt;//   break;&lt;br /&gt;//  }&lt;br /&gt;//&lt;br /&gt;//  System.out.println(&quot;  ------- tage Value = &quot; + tag);&lt;br /&gt;&lt;br /&gt; return hexValue;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;&lt;br /&gt;And you might need BitIterator class too; can be found &lt;a href=&quot;http://codeexample.blogspot.com/2010/10/specific-bits-from-byte.html&quot;&gt;here&lt;/a&gt;&lt;/blockqoute&gt;&lt;/blockquote&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/1359471024724058330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/asn1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1359471024724058330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1359471024724058330'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/asn1.html' title='ASN.1'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-1046291063030536137</id><published>2010-10-21T06:11:00.000-07:00</published><updated>2010-10-26T05:56:17.131-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bits from byte"/><category scheme="http://www.blogger.com/atom/ns#" term="iteration of bits in byte"/><title type='text'>Specific bits from byte</title><content type='html'>&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Bit Iteration:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;Following code shows how to iterate between bits of a byte array. The original class was taken from somewhere else i have just customized it to get number of desired bits. Please do note that bits are iterated from Most significant bit (MSB)&lt;br /&gt;&lt;blockqoute&gt;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;&lt;br /&gt;public final class BitIterator implements Iterator&lt;Integer&gt;&lt;br /&gt;{&lt;br /&gt; private final byte[] array;&lt;br /&gt; &lt;br /&gt; private int bitIndex = 0;&lt;br /&gt; private int arrayIndex = 0;&lt;br /&gt;&lt;br /&gt; public BitIterator(byte[] array)&lt;br /&gt; {&lt;br /&gt;  this.array = array;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; public boolean hasNext()&lt;br /&gt; {&lt;br /&gt;  return (arrayIndex &lt; array.length) &amp;&amp; (bitIndex &lt; 8);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public Integer next()&lt;br /&gt; {&lt;br /&gt;  Integer val = (array[arrayIndex] &gt;&gt; (7 - bitIndex) &amp; 1) == 1 ? 1 : 0;&lt;br /&gt;  bitIndex++;&lt;br /&gt;  if (bitIndex == 8)&lt;br /&gt;  {&lt;br /&gt;   bitIndex = 0;&lt;br /&gt;   arrayIndex++;&lt;br /&gt;  }&lt;br /&gt;  return val;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getBits(int count)&lt;br /&gt; {&lt;br /&gt;  StringBuffer buffer = new StringBuffer();&lt;br /&gt;  for (int i = 0; i &lt; count; i++)&lt;br /&gt;  {&lt;br /&gt;   if (hasNext())&lt;br /&gt;   {&lt;br /&gt;    buffer.append(next());&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return buffer.toString().trim();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void remove()&lt;br /&gt; {&lt;br /&gt;  throw new UnsupportedOperationException();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;&lt;/blockqoute&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/1046291063030536137/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/specific-bits-from-byte.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1046291063030536137'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/1046291063030536137'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/specific-bits-from-byte.html' title='Specific bits from byte'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7539392226778505626.post-5631568005577098991</id><published>2010-10-21T05:06:00.000-07:00</published><updated>2010-10-21T06:05:12.927-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="byte"/><category scheme="http://www.blogger.com/atom/ns#" term="hex"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Byte Array to Hex</title><content type='html'>&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Byte Array to Hex:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;public static String byteArraytoHex(byte[] bytes)&lt;br /&gt; {&lt;br /&gt;   StringBuffer buffer= new StringBuffer();&lt;br /&gt;   byte b;&lt;br /&gt;   for (int y = 0, x = 0; y &lt; bytes.length; ++y, ++x) &lt;br /&gt;   {&lt;br /&gt; b = ((byte) (bytes[y] &gt;&gt; 4));&lt;br /&gt; b = (byte)( b&lt;0?(16+b):b);&lt;br /&gt; buffer.append(Integer.toString((b), 16).toUpperCase());&lt;br /&gt; b = ((byte) (bytes[y] &amp; 0xF));&lt;br /&gt; b=(byte)( b&lt;0?(16+b):b);&lt;br /&gt; buffer.append(Integer.toString((b), 16).toUpperCase());&lt;br /&gt; buffer.append(&quot; &quot;);&lt;br /&gt;   }&lt;br /&gt;   return  buffer.toString();&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;</content><link rel='replies' type='application/atom+xml' href='http://codeexample.blogspot.com/feeds/5631568005577098991/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://codeexample.blogspot.com/2010/10/byte-array-to-hex.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/5631568005577098991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7539392226778505626/posts/default/5631568005577098991'/><link rel='alternate' type='text/html' href='http://codeexample.blogspot.com/2010/10/byte-array-to-hex.html' title='Byte Array to Hex'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>