10 Replies Latest reply: Jul 31, 2009 2:32 PM by jschellSomeoneStoleMyAlias RSS

    String to BCD

    807588
      Hi everyone,

      I'm trying to convert a String to BCD..

      I am using a stringbuilder to make a string and a byte with a variable:

           
      private void toBcd(String value, byte[] buf) 
              {
                int charpos = 0; //char where I start
                int bufpos = 0;
                if (value.length() % 2 == 1) {
                     //for odd lengths I encode just the first digit in the first byte
                     buf[0] = (byte)(value.charAt(0) - 48);
                     charpos = 1;
                     bufpos = 1;
                }
                //encode the rest of the string
                while (charpos < value.length()) {
                     buf[bufpos] = (byte)(((value.charAt(charpos) - 48) << 4)
                               | (value.charAt(charpos + 1) - 48));
                     charpos += 2;
                     bufpos++;
                }
           }
      That is the method I am using to parse the string. Everything works fine, except that when I write the resulting byte to an outputstream there's a zero ( 0 ) before the characters I pass with the string.

      For example a bad result is *0*438 0872 9200 1... and it must be 4380 8729 2001..., because that is what I am writing. What can be happend with this?

      Any Idea to take off the zero from what I am writing?

      -- I am using the tcp dump to view the result and is only this part the one that is not working properly.
        • 1. Re: String to BCD
          796440
          Add print statements so you can see what's happening at each step. And start with a smaller number (fewer digits) so that it's quicker and easier to see what's happening.
          • 2. Re: String to BCD
            800427
            Three possibilities here : value.length() is odd and value.charAt(0) == '0' or if length is even, charAt(0) and charAt(1) are both '0'
            or you are doing evil things writing out your byte array.

            How exactly are you printing out the byte array?
            • 3. Re: String to BCD
              jschellSomeoneStoleMyAlias
              Any Idea to take off the zero from what I am writing?

              -- I am using the tcp dump to view the result and is only this part the one that is not working properly.
              And what does TCP have to do with debugging this method?
              • 4. Re: String to BCD
                807588
                What I do is:
                outs.write((((length % 100) / 10) << 4) | (length % 10));  //First I write the lenght to outputstream
                byte[] buf = null;
                buf = new byte[(length/2)+1];  //I create a new byte
                toBcd(ReturnString(), buf); //Then I pass the byte and the string to the (toBCD method) in this way:

                outs.write(buf); //To end I write the

                The ReturnString method

                 public String ReturnString(String value) //The value never starts with zero (0)
                        {
                            StringBuilder sb = new StringBuilder();                     
                                     
                         for (int i=0;i<value.toCharArray().length;i++)   
                         {     
                             sb.append(value.toCharArray()); 
                }
                return sb.toString();
                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                • 5. Re: String to BCD
                  807588
                  OK. I got something but there is still a zero.


                  if I add << 4 to this
                  (value.charAt(0) - 48);
                  insted of print 0412

                  move the zero the right and prints (4012 )

                  Any Idea?
                  • 6. Re: String to BCD
                    YoungWinston
                    Kanell wrote:
                    Any Idea to take off the zero from what I am writing?
                    I tried it with printf("%x") and it seems to work.

                    And, if you're not worried about efficiency, the following conversion works just fine:
                    public static byte[] convertToBCD(String val) {
                         BigInteger v = new BigInteger(val);
                         return new BigInteger(v.abs().toString(), 16).shiftLeft(4)
                              .add(BigInteger.valueOf(v.signum() < 0 ? 13L : 12L)).toByteArray();
                    }
                    Winston

                    Edited by: YoungWinston on Jul 30, 2009 11:19 AM - changed 'private' to 'public'
                    • 7. Re: String to BCD
                      800427
                      outs.write((((length % 100) / 10) << 4) | (length % 10));  //First I write the lenght to outputstream
                      I am not sure I understand what you are doing here.
                      This will not print out the length of the array and is probably the reason you are seeing the 0.

                      In general, if you want to create strings in a file with the proper encoding, you use a PrintWriter.
                      What character encoding do you want to use for your file?
                      • 8. Re: String to BCD
                        EJP
                        I'm trying to convert a String to BCD.
                        That doesn't make sense unless the String only contains numeric digits. More probably you should be converting a BigDecimal to BCD.
                        • 9. Re: String to BCD
                          807588
                          Result, it was that the length of the message was odd.
                          • 10. Re: String to BCD
                            jschellSomeoneStoleMyAlias
                            Kanell wrote:
                            Result, it was that the length of the message was odd.
                            Of course as BCD encodes two numerics in 8 bits.
                            And when the string was odd the first byte had only one BCD value in the lower 4 bits. And the upper 4 bits was zero.