convert string to byte[]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    convert string to byte[]

    I have this simple question but i dont know which method to convert it..

    the case is like this:

    i have encrypted a string in byte[] form.

    and since it is a string already, but in the end i still need to decrypted it back to original string. for example:

    Code:
    String str = "Peter is smart";
    
    // encrypt it
    byte[] data = enc.encryptString(str);
    
    String test_str = data.toString();
    System.out.println(test_str);     // this value is : [B@356f
    Code:
    String str2 = getTest_Str();  // this String field is a must and value is [B@356f
    
    // here i want to make the string into byte[]
    byte[] bb = str2.someMethod();  
    
    System.out.println(bb);    // i want this bb value to be exactly the same as test_str above, is [B@356f.
    
    // here i can do decryption which take byte[] as parameter.
    System.out.println(enc.decryptString( bb ));  // this should print "Peter is smart"
    how to make the string to byte[] without changing the value?

    any idea of what i'm talking about ?
    any idea ?

    thanks in advance.
    Nick
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    One of the constructors of the String class takes a byte[] array as an argument.

    kind regards,

    Jos

    Comment

    • nickyeng
      Contributor
      • Nov 2006
      • 252

      #3
      Originally posted by JosAH
      One of the constructors of the String class takes a byte[] array as an argument.

      kind regards,

      Jos
      i know.

      maybe the way i explain is confusing but the string is [@B43f.

      decryptString(b yte[] b) takes byte[] as argument.

      So i have to make "[@B43f" to byte[] first, and then pass to decryptString(x x) method.

      but i dont know how to make a string to byte[] without changing the value(in this case is [@B43f ) so that the string is [@B43f and the byte array is also [@B43f.

      String(byte[]) returns string, not return byte[], so it not help in this case.

      any idea?

      Comment

      • nickyeng
        Contributor
        • Nov 2006
        • 252

        #4
        somehow look like this:

        Code:
        String str = "[@B43f";
        
        byte[] b = str.clone();
        
        System.out.println("b = " + b);   // if this value is [@B43f, then means this is what i want.
        but i'm not sure whether this is the way of doing it cos i dont have Eclipse or jdk in my pc here.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by nickyeng
          somehow look like this:

          Code:
          String str = "[@B43f";
          
          byte[] b = str.clone();
          
          System.out.println("b = " + b);   // if this value is [@B43f, then means this is what i want.
          but i'm not sure whether this is the way of doing it cos i dont have Eclipse or jdk in my pc here.
          Wait until you have access to a compiler, then. Us telling you yes or no isn't going to help you learn.

          Comment

          • nickyeng
            Contributor
            • Nov 2006
            • 252

            #6
            Originally posted by BigDaddyLH
            Wait until you have access to a compiler, then. Us telling you yes or no isn't going to help you learn.
            it doesn't work in j2me application.

            i've tried.

            any method to do that?

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by nickyeng
              it doesn't work in j2me application.
              Why doesn't it work? Analyzing why it failed may help you find a solution.

              Comment

              • nickyeng
                Contributor
                • Nov 2006
                • 252

                #8
                i found code from website eventually that helps me with that.

                thanks for your time tho.

                NickyEng

                Comment

                • nickyeng
                  Contributor
                  • Nov 2006
                  • 252

                  #9
                  Originally posted by BigDaddyLH
                  Why doesn't it work? Analyzing why it failed may help you find a solution.
                  you're talking like talk pointless.

                  if everyone could analyze the question, then we don't need to come here and post question tho.

                  anyway, thanks.

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by nickyeng
                    you're talking like talk pointless.

                    if everyone could analyze the question, then we don't need to come here and post question tho.

                    anyway, thanks.
                    I was replying to your post where you said

                    Originally posted by nickyeng
                    it doesn't work in j2me application.
                    It's important to learn how to express problems in better detail. You don't just go to your doctor and only say "I am unwell." Perhaps this article should be in a sticky post at the top of the forum:

                    How to ask questions the smart way.

                    Comment

                    Working...