CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2000
    Posts
    1

    Conversion from byte array to String and back

    Hi, I'd be very grateful if somebody help.
    I need to convert an array of bytes to a String, sending this string as a parameter and getting again a byte array.
    The first step is acomplished through new String(byte[] myarray). Then I'm using getbytes() method.
    This scheme works ok if bytes containing 0x8d, 0x8e, 0x8f, 0x90, 0x9d, 0x9e, are not involved. If it's not the case in my returned bytearray I obtain 0x3f.

    Here is a demonstrating piece of code. Thanks.

    javacode/
    public static void main(String[] args) {

    byte [] byteArray = new byte[256];
    for (int i = 0; i < 256; i++) {
    byteArray[i] = (byte)i;
    }

    String kk = new String(byteArray);
    byte [] byteArray2 = kk.getBytes();

    for (int j = 0; j < 256; j++) {
    System.out.println(byteArray[j] + " = " + byteArray2[j]);
    }
    /javacode




  2. #2
    Join Date
    Sep 2000
    Posts
    27

    Re: Conversion from byte array to String and back

    Afraid I can't help you, but I've just hit exactly the same problem (also occurs with 0x81).

    did you find a reason and/or a solution ?


  3. #3
    Join Date
    Feb 2001
    Posts
    1

    Re: Conversion from byte array to String and back

    Yes I've found a workaround to this problem: use the codepage ISO8859_1.
    With the default codepage some values like 0x81 has no representation so you obtain the error. With the korean codepage every number you can obtain when obtaining a byte array from a string has representation so you can smoothly perform your string-byte-string tasks.


  4. #4
    Join Date
    Nov 2000
    Posts
    70

    Re: Conversion from byte array to String and back

    How do I specify the codepage? I am using Visual Cafe 4.1.
    Please advise. Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured