String pack and unpack

Hi, i�m having trouble trying to write a methode to unpack a string packed with the following methode.
can some one tell me how to write the unpack methode ?
thanks.
public String pack(String s) {
byte[] a = new byte[16];
//If the string is an odd length then pack with an F
if ( s.length() % 2 == 1 ) {  
s = (char) 0xFF + s ;
//Now pack each pair of bytes into the array
for ( int i = 0, j = 0 ; i < s.length() ; i += 2, j++ ) {
a[j] = (byte) ( ( (s.charAt(i) & 0x0F) << 4 ) +
(s.charAt(i+1) & 0x0F) ) ;
return new String(A);
}

You need know the assumptions about the content of the original string. A packing algorithm like the one you describe might be used to pack a numeric string into an array of bytes. In that case, you assume that the original string consisted of nothing but ASCII digits, and you can recover the string with the following method:
public String unpack(String a) {
  byte [] b = a.getBytes();
  StringBuffer buff = new StringBuffer(b.length * 2);
  for (int i = 0; i < b.length; i++) {
    buff.append((char)(0x30 | ((b[i] & 0xf0) >> 4)));
    buff.append((char)(0x30 | (b[i] & 0x0f)));
  // If the first char was a pad char, then remove it
  if (buff.charAt(0) == (char)0x3f)
    return buff.substring(1);
  return buff.toString();
}

Similar Messages

  • Pack and unpack a JFrame

    Hi,
    I'm using a JFrame and I 'd like to put a JButton to pack and unpack the window. Now, the pack is simple (I use the pack() method), but to enlarge the window? I've tried to store the original size before pack, and then resize the window but, since I use a BorderLayout layout manager the center panel doen't return visible.
    can anyone help me?

    You likely are not calling validate() on the layout after the resize
    Eximport javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test extends JFrame implements ActionListener
       private JPanel p;
       private Rectangle r;
       public Test()
          super("Flip");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setSize(770,580);
          p = new JPanel(new BorderLayout());
          JPanel p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("North"));
          p.add(p2, BorderLayout.NORTH);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("West"));
          p.add(p2, BorderLayout.WEST);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("East"));
          p.add(p2, BorderLayout.EAST);
          p2 = new JPanel(new FlowLayout());
          p2.add(new JLabel("Center"));
          p.add(p2, BorderLayout.CENTER);
          p2 = new JPanel(new FlowLayout());
          JButton pack = new JButton("Pack");
          JButton unpack = new JButton("Unpack");
          pack.addActionListener(this);
          unpack.addActionListener(this);
          p2.add(pack);
          p2.add(unpack);
          p.add(p2, BorderLayout.SOUTH);
          setContentPane(p);
       public void actionPerformed(ActionEvent ae)
          String s = ae.getActionCommand();
          if(s.equals("Pack"))
             r= getBounds();
             pack();
          else
             setBounds(r);
             validate();
       public static void main(String[] args)
          new Test().setVisible(true);
    }

  • Pack and unpack in ABAP objects

    can anyone explain me how to pack or unpack a field inside a method which is inside a class.

    Hi
    See the doc of PACK and UNPACK
    PACK source TO destination.
    This statement, <b>which is forbidden in classes</b>, converts the content of the data object source to the data type p of length 16 without decimal places. In contrast to the conversion rules for elementary data types, a decimal separator in source is ignored. This assigns the converted content to the data object destination.
    The data type of source must be character-type, flat, and its content must be interpretable as a numeric value. The data type of destination must be flat. If destination has the data type p, the interim result is assigned to it from left to right. Surplus characters are cut off on the left, and the decimal places are determined by the data type of destination. If destination does not have the data type p, the interim result is converted to the data type of destination according to the rules in the conversion table for source field type p.
    UNPACK source TO destination.
    This statement converts the content of the data object source according to a specific rule and assigns the converted content to data object destination. For source, the data type p of length 16 without decimal places is expected. The data type of destination must be character-type and flat.
    The conversion is performed according to the following rules:
    If the data type of source is not of the type p with length 16 and without decimal places, then the content of source is converted to this data type. Contrary to the rules described in conversion rules for elementary data types, any decimal separator in source is completely ignored.
    The digits of the interim result are assigned to data object destination right-aligned and without +/- sign. Any additional places in destination are filled with leading zeros. If the length of destination is not sufficient, the assigned variable is truncated from the left.
    Reward points for useful Answers
    Regards
    Anji

  • Packing and Unpacking BITS from a SHORT

    Hello everyone,
    I am have six pieces of data packed into a 16bit short value and I am wondering if my code to pack and unpack the short looks right to everyone else. The six smaller values that I am pulling out of the short are of the length (in order) 2+2+3+3+3+3 bits . My approach to unpacking the short is applying a bitmask to isolate just the sub-value I want, then using the bitwise-left shift ( << operator) to move that value to the 0-position in the short, then casting the value to a byte for use in my program. The code to do that looks something like this:
    public final short maskValue1 = Short.parseShort("0000000000000011", 2);
    public final short maskValue2 = Short.parseShort("0000000000001100", 2);
    public final short maskValue3 = Short.parseShort("0000000001110000", 2);
    public final short maskValue4 = Short.parseShort("0000001110000000", 2);
    public final short maskValue5 = Short.parseShort("0001110000000000", 2);
    public final short maskValue6 = Short.parseShort("111000000000000", 2);
         public void unpackEncodedMap(short map, byte[] outMap) {
              outMap[0] = (byte)((map & maskValue1) << 14);
              outMap[1] = (byte)((map & maskValue2) << 12);
              outMap[2] = (byte)((map & maskValue3) << 9);
              outMap[3] = (byte)((map & maskValue4) << 6);
              outMap[4] = (byte)((map & maskValue5) << 3);
              outMap[5] = (byte)(map & maskValue6);
    The approach I use to encode the short value involves taking my input byte array and casting each into a short. Then I use the bitwise-right shift operator ( >> ) to move the bit value into the appropriate position. To apply the next byte-value I bitwise-OR the previous value onto the new mask. The values in the byte-array are assured not to be greater then their sub-value bit lengths ... so the downcast is all right. Here is my code to do the packing:
         public short encodeMap(byte[] mymap) {
              short out = (short)(((short)mymap[0]) >> 2);
              out = (short)((out | ((short)mymap[1])) >> 2);
              out = (short)((out | ((short)mymap[2])) >> 3);
              out = (short)((out | ((short)mymap[3])) >> 3);
              out = (short)((out | ((short)mymap[4])) >> 3);
              out = (short)(out | ((short)mymap[5]));
              return out;
    Thanks for looking at my functions and point out any inconsistencies. I feel like there is something I am not seeing in my code. Thanks!

    Well...thanks for the one-line answer :) yes, in the sense that it compiles and runs, but I am not sure if there is a problem with my logic or not.My program is acting strange and I am trying to isolate my problem, I think the problem might be in my pack/unpack methods.... possibly in my bit-math.

  • WebLogic Pack and Unpack

    When you pack and unpack the domain from one server to another server, config.xml is missing while we unpack the same,
    here we would like to know, do we need to metion the same path(Same folder structure) while pack and unpack?

    Hi
    Yes, one of the basic requirements is all the Machines should have Absolutely same version of Weblogic Software and in the same folder structure. The reason is when we run Pack command on the main master domain, it does store the full folder structure of some folders that has all the modules. So when you run UnPack on this other machine, basically it will unpack. But runtime, if folder structure is not matching, the most common error you may get is like Java home not found, middleware home not found and ofcourse ends up with all NoClassDefFound errors.
    Hence the folder structure should be same. And one more thing. After running unpack command, immediately you will NOT see any managed server folders under your domain/servers. That folder will be still Empty only. Only when you start the managed server like startManagedWeblogic.cmd myMS1 adminurl, thats when under domain/servers you will see a new folder named "myMS1" which is the name of managed server that you gave when you created the main master domain and packed it. Also, note it will prompt for username and password. ALSO, everytime you start managed servers, it will go to Admin Server and get some important files like config.xml file, jdbc files and few other config files and mainly all the LDAP files. Basically everytime it Syncs from AdminServer. So do NOT make any changes to any files on ManagedServer. Because when you restart they will get overwritten by admin files.
    Thanks
    Ravi Jegga

  • Pack and Unpack in HU

    Hi MM Gurus,
    I have a requirement of packing one HU of say 5EA Qty and Unpack in two HUs say 4EA & 1EA.Is this scenario is  possible in Handling Unit Management. If possible please explain.
    Regards,
    Manikyappa

    hi,
    if you have handling unit management activated (TVSHP-EXIDV_UNIQUE), you can create HUs within hu02. Here you can pack the f.e. 5 pieces into one HU or also unpack it into two HUs with 4+1 pieces (add material, total quantity, plant and storage location and the packaging material -> select these to lines and push "pack" button in the middle of the screen for packing it; to unpack HUs use the "empty" or "delete HU" button on the left side of the screen, after this you can pack again like you want... you have several possibilities to create HUs within this transaction). You can also use transaction humo for changing HUs (and for a lot of useful things used with HUs).
    (If you do not have handling unit management activated, you can only create shipping units within a delivery (you have the same screen there as in hu02).)
    I hope I could help you.
    Regards, Ely

  • Hanlding Unit Pack and Unpack Function module

    Hi All,
        Can any one know the functiona modules for PACK Handling Unit and UNPACK Handling Unit? If so please reply me.
    Thanks,
      Pranav

    Hi,
    Please check this FM.
    PACK_HANDLING_UNIT
    UNPACK_HANDLING_UNIT
    Hope this will help.
    Regards,
    Ferry Lianto

  • Pack and unpack Radius VSA attributes

    Hi
    As far as I know there are some methods to pack radius VSA attributes. Here are:
    As the part of Cisco-AVPair
    26 - VSA
    Length
    9  - Vendor ID
    1  - Vendor Type (Cisco-AVPair Attribute ID)
    Attribute Name=Value
    In the Vendor Specific attribute ("throught attribute ID")
    26  - VSALength
    9 - Vendor ID
    2 - Vendor Type (Attribute ID)
    Vendor Length
    Attribute Name=Value
    In the Vendor Specific attribute ("throught attribute ID") 
    26 - VSA
    Length
    9 - Vendor ID
    2 - Vendor Type (Attribute ID)
    Value
    i.e. with attribute name and witout.
    How to understand which attribute needs attribute name in value string?
    For example:
    26|Length|9|2|Vendor Length|1|h323-incoming-conf-id=82b5fc8cd6f411dfa3c6080027716a9a
    26|Length|9|2|Vendor Length|35|h323-incoming-conf-id=82b5fc8cd6f411dfa3c6080027716a9a
    26|Length|9|2|Vendor Length|35|82b5fc8cd6f411dfa3c6080027716a9a
    which of the methods is right?

    Hi,
    For the specific VSA you used in the example (h323-incoming-conf-id), (1) is the correct encoding, since Cisco VSA vendor type 1 (also more commonly referred to as  cisco AV Pair) is always encoded in strings with the format of "attribute=value". This applies to other cisco VSAs that use string encoding as well. For VSA's that don't use string encoding, eg., fax-pages (vendor type 5, encoding integer), it typically doesn't include the value. You should be able to check that against the vendor dictionary to confirm. Please also see:
    http://www.cisco.com/en/US/docs/ios/voice/cdr/developer/guide/cdrdefs.html
    Thanks,
    Wen

  • Changing WL port using pack and unpack Commands

    Hi,
    I am an experienced WL8 user, just starting with WL9.
    I created a first domain relatively easily using "config.sh"; and after finishing this proof of concept thought that it might be a good idea to create a template from it for the rest of the guys here.
    For that I used the ?pack? command, which works fine.
    The completing ?unpack? command also seems to works fine.
    The only thing is that I fail to locate any option to determine the server?s listen port (always sets to be 7001 like my original domain), and this is problem because we all work on the same linux machine.
    The only option I found so far is to start the server on it original port (7001) and than change it using the admin console. This is not good enough since we want to use an automatic install wizard?
    Did I miss something?
    Is there any command line to help?
    Or any other tips?
    Your help is appreciated,
    zilbi

    Hi Again,
    solved it using the WLST offline tool.
    the script (customCmpDomain.py):
    #=======================================================================================
    # Open the domain template.
    #=======================================================================================
    readTemplate("cmpDomainTemplate.jar")
    #=======================================================================================
    # Change server's port
    #=======================================================================================
    cd('Servers/AdminServer')
    set('ListenPort', 7080)
    #=======================================================================================
    # Open (unpack) the domain.
    #=======================================================================================
    writeDomain("/home/rami/domains/cmpDomain")
    #=======================================================================================
    # Close & Exit
    #=======================================================================================
    closeTemplate()
    exit()
    and just run it using 'weblogic.WLST'
    thanks anyway!
    zilbi

  • Pack and unpack

    Hi
    Please do help me.
      Data: dmshb(11),             "amount in SEK or EUR
              amount_1    LIKE dfkkop-betrw.  " of length 13 and decimal 2 with +/-sign
      amount_1 = '   91111111.00-'.
      dmshb = amount.
    the result of this is   dmshb = *111111.00-
    the expected is  dmshb = 09111111100
    Please do tell me the logic to do this
    promise to reward points
    bye
    Mac

    Hi Madan,
    This is your code.
    REPORT ZTEST_TEST01 .
    Data: dmshb(11), "amount in SEK or EUR
    amount_1 type netpr. " of length 13 and decimal 2 with +/-sign
    amount_1 = ' 91111111.00-'.
    unpack amount_1 to dmshb.
    *dmshb = amount_1.
    write: / 'amount', amount_1 ,
             'dmshhb', dmshb.
    Regards,
    Raghav

  • HT201441 dear sir . Ihave bought a dell ipad under serial number DMPKQFF4F186 on 25th of dec.2013,it was packed and seems it was .new,after unpacking i found out it is lock. I just need to know when this serial number has registered in icloud. I will grat

    dear sir . I have bought a dell ipad under serial number DMPKQFF4F186 / UDID : 976ba8f5ba16f665bdf3b1b5ec53a172c9d65381. on 25th of dec.2013,it was packed and seems it was new after unpacking i found out it is locked.Ijust neednto know when this serial number has registered in icloud. I will grateful if you could let me this information. locking forward to your kind response.  With Regards

    Greetings,
    I've never seen this issue, and I handle many iPads, of all versions. WiFi issues are generally local to the WiFi router - they are not all of the same quality, range, immunity to interference, etc. You have distance, building construction, and the biggie - interference.
    At home, I use Apple routers, and have no issues with any of my WiFi enabled devices, computers, mobile devices, etc - even the lowly PeeCees. I have locations where I have Juniper Networks, as well as Aruba, and a few Netgears - all of them work as they should.
    The cheaper routers, Linksys, D-Link, Seimens home units, and many other no name devices have caused issues of various kinds, and even connectivity.
    I have no idea what Starbucks uses, but I always have a good connection, and I go there nearly every morning and get some work done, as well as play.
    You could try changing channels, 2.4 to 5 Gigs, changing locations of the router. I have had to do all of these at one time or another over the many years that I have been a Network Engineer.
    Good Luck - Cheers,
    M.

  • Jam Packs and "Garageband Instruments" when reinstalling Logic?

    What's the proper procedure when it comes to Jam Packs and 3rd party Apple Loops libraries when reinstalling Logic and OS X?
    I'm keeping my Apple Loops on a separate drive and know that I can just drag them to the Loop Browser after reinstalling Logic. However, by doing so, I don't think I'll be getting the "Garageband Instruments" that are installed with the Jam Packs and some 3rd party Apple Loops libraries. Is that correct? If so, what's the best way to deal with this? Always installing all Apple Loops from the disks or perhaps saving a folder (which one?) with the Garageband Instruments before re-installing OS X?
    When I last installed Logic, and told the installer to install the Jam Packs on an external drive, they were still installed on the system drive! Does anyone know why? Now what's the best way to move the Apple Loops? What I've done in the past is installing them to their default location, and then creating an alias in Home:Library:Audio:Apple Loops folder that points to the new location on the external drive. Is that the way to go or is another way better?
    Any input or help would be greatly appreciated.

    Jam packs consist of two types of content. Loop content will go to where you choose to install it (an external drive if you wish). Instrument content has to go to the GB library, for a number of reasons, and the GB library will be on your system disk under Application Support/Garageband.
    So backup your GB library in addition to your Logic related stuff.
    It is possible to move much (but not all) of the heavy Instrument content to an external drive too, but there are gotchas with this.

  • I purchased a monthly pack and havent been able to use it. I have been getting charged for 3 months now and I still get an error message saying "unable to load all your plans". How do I re-install or dowload and get my last 3 payments reimbursed?

    I purchased a monthly pack and havent been able to use it. I have been getting charged for 3 months now and I still get an error message saying "unable to load all your plans". How do I re-install or dowload and get my last 3 payments reimbursed?

    Link for Download & Install & Setup & Activation problems may help
    -Online Chat http://www.adobe.com/support/download-install/supportinfo/
    or
    Adobe contact information - http://helpx.adobe.com/contact.html

  • I paid for PDF Pack and keep getting an error 404 - file not found when I try to do anything

    Just paid for PDF Pack and cannot get it to start working.  Have a file open, went to account, create, got to website, and it wont let me do anything,

    Hi EJ,
    I wanted to check in to make sure you have everything working at this point?
    Let me know if you have further questions.
    Regards, Stacy

  • In 6/2009 I bought iWork Family pack and have used it on two computers. Now I want to download iWork to my iMac but there is no way via the App store. How do I install the iWork bundle I paid for.

    In 6/2009 I bought iWork Family pack and have used it on two computers. Now I want to download iWork to my iMac but there is no way via the App store. How do I install the iWork bundle I paid for.i

    As of a minute ago you can still download the trial at this link. Note: clicking the link will automatically start the download.

Maybe you are looking for