1

I want to implement one functionality as my ANDROID colleague did. Below is code snippet and I am having trouble converting that code to objective-c. So please guide me in right direction--- Thanks

Here is the code snippet

public String createControlParams() {
        controlParams_ = "";
        String expiry = "";
        String delayedDelivery = "";
        String restricted = "";
        String priorityIndicator = "";
        String acknowledgement = "";
      //Note - PMessage.PRIORITY_INDICATOR_WHITE is defined as Int like 1,2,3
        if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_WHITE) {
            expiryTimeText_ = "";
            restrictBackup = false;
            restrictForward_ = false;
            readAcknowledgement_ = false;
        }

        if (expiryTimeText_ != null && expiryTimeText_.length() > 0) {
            int exptime = Integer.parseInt(expiryTimeText_);
            byte h = (byte) (exptime / 60);
            byte m = (byte) (exptime % 60);
            expiry = new String(new byte[]{'E', 0, h, m});//??? how to get this thing in objective-c
        }
        if (delayDelivery) {
            long timeDifference;
            long deliveryTime;
            // Fix 
//            if (midlet_.getPlatform().equalsIgnoreCase("rim")) {
////                timeDifference = Calendar.getInstance().getTimeZone().getRawOffset();
////                deliveryTime = (dateField_.getTime() - (dateField_.getTime() % (24 * 60 * 60 * 1000))) + (24 * 60 * 60 * 1000) + deliverySetTime_ - timeDifference;//+timeDifference;//(((Integer) timeSpinner.getValue()).longValue() * 1000);
//                deliveryTime = (dateField_.getTime() + deliverySetTime_);// - timeDifference;
//            } else {
            deliveryTime = dateField_.getTime() + deliverySetTime_;// - timeDifference;
//            }
            if (deliveryTime > (new Date().getTime() + 2000)) { // Added to_ make message as instant delivery as opposed to_ delayed delivery if the delivery time is set in past (Added 2 seconds for message processing time)
                delayedDelivery = "D" + deliveryTime;
            }
        }
        if (restrictBackup && restrictForward_) {
            restricted = new String(new byte[]{'R', (byte) 3});
        } else if (restrictForward_) {
            restricted = new String(new byte[]{'R', (byte) 1});
        } else if (restrictBackup) {
            restricted = new String(new byte[]{'R', (byte) 2});
        }
        if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_RED) {
            priorityIndicator = new String(new byte[]{'P', (byte) PMessage.PRIORITY_INDICATOR_RED});
        } else if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_YELLOW) {
            priorityIndicator = new String(new byte[]{'P', (byte) PMessage.PRIORITY_INDICATOR_YELLOW});
        } else if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_GREEN) {
            priorityIndicator = new String(new byte[]{'P', (byte) PMessage.PRIORITY_INDICATOR_GREEN});
        } else if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_GRAY) {
            priorityIndicator = new String(new byte[]{'P', (byte) PMessage.PRIORITY_INDICATOR_GRAY});
        } else if (priorityIndicator_ == PMessage.PRIORITY_INDICATOR_WHITE) {
            priorityIndicator = new String(new byte[]{'P', (byte) PMessage.PRIORITY_INDICATOR_WHITE});
        }

        if (readAcknowledgement_) {
            acknowledgement = new String(new byte[]{'A', PMessage.ACK_READ});
        }

        controlParams_ = priorityIndicator;
        if (!expiry.equals("") && expiry != null) {
            controlParams_ += (byte) (-128) + expiry;
        }
        if (!delayedDelivery.equalsIgnoreCase("") && delayedDelivery != null) {
            controlParams_ += (byte) (-128) + delayedDelivery;
        }
        if (!restricted.equalsIgnoreCase("") && restricted != null) {
            controlParams_ += (byte) (-128) + restricted;
        }
        if (!acknowledgement.equalsIgnoreCase("") && acknowledgement != null) {
            controlParams_ += (byte) (-128) + acknowledgement;
        }
        return controlParams_;
//        System.out.println(controlParams_);
    }

     private void getDraftControlParms(String controlParams_) {
        if (!controlParams_.equals("") && controlParams_ != null) {
            String[] sysParams = Helpers.splitUsingStringDelim(controlParams_, String.valueOf((byte) (-128)));
            try {
                for (int i = 0; i < sysParams.length; i++) {
                    if (!sysParams[i].equals("") && sysParams[i] != null) {

                        if (sysParams[i].substring(0, 1).equalsIgnoreCase("P")) {
                            priorityIndicator_ = (int) sysParams[i].getBytes()[1];
                        }

                        if (sysParams[i].substring(0, 1).equalsIgnoreCase("E")) {
                            int expiryFirstByte_ =  sysParams[i].getBytes()[2];
                            int expirySecondByte_ =sysParams[i].getBytes()[3];
                            int expiryTime=(expiryFirstByte_ * 60)  + (expirySecondByte_);
                            expiryMinutesField_.setText(expiryTime+"");
                            expiryTimeText_ = expiryMinutesField_.getText();
                        }

                        if (sysParams[i].substring(0, 1).equalsIgnoreCase("R")) {
                            int restrictedByte_ = (byte) sysParams[i].getBytes()[1];
                            if(restrictedByte_==1){
                            restrictForward_ = true;
                            restrictBackup = false;
                            }else if(restrictedByte_==2){
                            restrictForward_ = false;
                            restrictBackup = true;
                            }else{
                            restrictForward_ = true;
                            restrictBackup = true;
                            }
                        }
                        if (sysParams[i].substring(0, 1).equalsIgnoreCase("A")) {
                            readAcknowledgement_ = true;
                        }
                    }

                     switch (priorityIndicator_) {
                        case PMessage.PRIORITY_INDICATOR_RED: {
                            counter.getStyle().setBgColor(0xfe0002);
                            counter.getStyle().setBgTransparency(255);
                            break;
                        }
                        case PMessage.PRIORITY_INDICATOR_YELLOW: {
                            counter.getStyle().setBgColor(0xffff00);
                            counter.getStyle().setBgTransparency(255);
                            break;
                        }
                        case PMessage.PRIORITY_INDICATOR_GREEN: {
                            counter.getStyle().setBgColor(0x80ff00);
                            counter.getStyle().setBgTransparency(255);
                            break;
                        }
                        case PMessage.PRIORITY_INDICATOR_GRAY: {
                            counter.getStyle().setBgColor(0x919594);
                            counter.getStyle().setBgTransparency(255);
                            break;
                        }
                        case PMessage.PRIORITY_INDICATOR_WHITE: {
                            counter.getStyle().setBgColor(0xffffff);
                            counter.getStyle().setBgTransparency(255);
                            break;
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
5
  • 1
    Snippet is good lol. What do you need to convert concretely? Where are you stuck? Commented Nov 9, 2011 at 11:55
  • I m stuck at this if (expiryTimeText_ != null && expiryTimeText_.length() > 0) { int exptime = Integer.parseInt(expiryTimeText_); byte h = (byte) (exptime / 60); byte m = (byte) (exptime % 60); expiry = new String(new byte[]{'E', 0, h, m});//??? how to get this thing in objective-c } Commented Nov 9, 2011 at 15:31
  • I am stuck at how do I create byte array and all the things into that byte array along with (-128) byte thing also how do I seperate this in objective-c String[] sysParams = Helpers.splitUsingStringDelim(controlParams_, String.valueOf((byte) (-128))); Commented Nov 9, 2011 at 15:34
  • @RahulVyas: Why do you need to use a String/NSString to store binary data? Just use char arrays everywhere and don't convert them to/from strings all the time. (Also, unless your requirements are interoperating with an existing system, using a hand-coded binary protocol is probably a bad idea to begin with.) Commented Nov 9, 2011 at 21:51
  • I am developing same application as the java developers developed so that's why I want to keep the same format here. That is the requirement. Commented Nov 10, 2011 at 3:03

2 Answers 2

1

I'm not an Objective C guy, but have you tried using char? In C/C++ a char is one byte and should do what you need it to.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried using unsigned char[] but still stuck at this expiry = new String(new byte[]{'E', 0, h, m});//??? how to get this thing in objective-c
I am stuck at how do I create byte array and all the things into that byte array along with (-128) byte thing also how do I seperate this in objective-c String[] sysParams = Helpers.splitUsingStringDelim(controlParams_, String.valueOf((byte) (-128)));
in C++ new std::string(char[]{ch1, ch2}). Can't help with objective C.
sure, but can't help you with that either.
1

You don't need to use bytes. You can use an NSArray of NSStrings.

Bytes, or unsigned char's in C, are more useful when you are concerned about the memory size of some data in a constrained environment.

1 Comment

the requirement is like this. The same code I need for cross platform compatibility. Can we use objective-c++ ? if yes can you please tell me how to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.