Results 1 to 18 of 18
  1. #1
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Unhappy Problem about MD5

    :confused: Dear FRIENDS! I have a such as problem: I'm realized algorithm MD5, program working correct with Latin alphabet, but when I give text with Cyrillic symbols, result is incorrect, I checked it with on-line version of MD5 and standard method (message digest) of java :confused: . Results are different if there are Cyrillic symbols. How to change default character table of java, can you advise me, please :( Thanks a lot for each answer!!! :)

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    So presumably you have your text in a String? In that case, when you call getBytes() on that string to get the bytes to pass into MessageDigest, pass in the name of a character set, e.g. "UTF-8".

  3. #3
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Please, answer

    How to switch to UTF8? I do not know... can you give some advise, please... :confused:

  4. #4
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default

    I think
    String unicode = "Unicode: \u30e6\u30eb\u30b3\u30fc\u30c9";
    byte[] bytes = unicode.getBytes("UTF-8");

    working incorrect

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Well, look:
    - getBytes() really does correctly return the bytes for the string in a given character set
    - MessageDigest, if correctly used, really does correctly calculate the MD5 checksum
    So possibilities include:
    - the online checker is messing up the character encoding in some way (I'm guessing you don't have source code, so how will we ever know...?)
    - you're not using MessageDigest correctly (you haven't posted any code)

  6. #6
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Here it is

    Sure, I'll not ask check all code, but anyway, can you show me, what's my problem? It works with Latin alphabet correct, different results if I check standard MD5 method and mine. If I say true, I used open source of MD5... but, I learned it, how does it work... first of all I realized MD5 looking pseudo code from wikipedia, and I thought that it works only in binary view... then I find open source of MD5, and learned it, but... that problem is realy killing me... :( HELP PLEASE!!!

    import java.io.*;


    class MD5 {
    private static long S11 = 7L;
    private static long S12 = 12L;
    private static long S13 = 17L;
    private static long S14 = 22L;
    private static long S21 = 5L;
    private static long S22 = 9L;
    private static long S23 = 14L;
    private static long S24 = 20L;
    private static long S31 = 4L;
    private static long S32 = 11L;
    private static long S33 = 16L;
    private static long S34 = 23L;
    private static long S41 = 6L;
    private static long S42 = 10L;
    private static long S43 = 15L;
    private static long S44 = 21L;

    private static char pad[] = {128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0};
    private char bytBuffer[] = new char[64];
    private long lngState[] = new long[4];
    private long lngByteCount = 0;

    MD5() {
    this.init();
    }

    private static long[] decode(char bytBlock[]) {
    long lngBlock[] = new long[16];
    int j = 0;
    for(int i = 0; i < bytBlock.length; i += 4) {
    lngBlock[j] = bytBlock[i] + bytBlock[i+1] * 256L + bytBlock[i+2] * 65536L + bytBlock[i+3] * 16777216L;
    System.out.println("lngBlock["+j+"]="+lngBlock[j]);
    j++;
    }
    return(lngBlock);
    }


    private static void transform(long lngState[], char bytBlock[]) {
    long lngA = lngState[0];
    long lngB = lngState[1];
    long lngC = lngState[2];
    long lngD = lngState[3];
    long x[] = new long[16];

    /*for (int j=0; j<bytBlock.length; j++) {
    System.out.println((int) bytBlock[j]);
    }*/

    x = decode (bytBlock);

    /* Round 1 */
    lngA = ff (lngA, lngB, lngC, lngD, x[ 0], S11, 3614090360L); /* 1 */
    lngD = ff (lngD, lngA, lngB, lngC, x[ 1], S12, 3905402710L); /* 2 */
    lngC = ff (lngC, lngD, lngA, lngB, x[ 2], S13, 606105819L); /* 3 */
    lngB = ff (lngB, lngC, lngD, lngA, x[ 3], S14, 3250441966L); /* 4 */
    lngA = ff (lngA, lngB, lngC, lngD, x[ 4], S11, 4118548399L); /* 5 */
    lngD = ff (lngD, lngA, lngB, lngC, x[ 5], S12, 1200080426L); /* 6 */
    lngC = ff (lngC, lngD, lngA, lngB, x[ 6], S13, 2821735955L); /* 7 */
    lngB = ff (lngB, lngC, lngD, lngA, x[ 7], S14, 4249261313L); /* 8 */
    lngA = ff (lngA, lngB, lngC, lngD, x[ 8], S11, 1770035416L); /* 9 */
    lngD = ff (lngD, lngA, lngB, lngC, x[ 9], S12, 2336552879L); /* 10 */
    lngC = ff (lngC, lngD, lngA, lngB, x[10], S13, 4294925233L); /* 11 */
    lngB = ff (lngB, lngC, lngD, lngA, x[11], S14, 2304563134L); /* 12 */
    lngA = ff (lngA, lngB, lngC, lngD, x[12], S11, 1804603682L); /* 13 */
    lngD = ff (lngD, lngA, lngB, lngC, x[13], S12, 4254626195L); /* 14 */
    lngC = ff (lngC, lngD, lngA, lngB, x[14], S13, 2792965006L); /* 15 */
    lngB = ff (lngB, lngC, lngD, lngA, x[15], S14, 1236535329L); /* 16 */

    /* Round 2 */
    lngA = gg (lngA, lngB, lngC, lngD, x[ 1], S21, 0xf61e2562L); /* 17 */
    lngD = gg (lngD, lngA, lngB, lngC, x[ 6], S22, 0xc040b340L); /* 18 */
    lngC = gg (lngC, lngD, lngA, lngB, x[11], S23, 0x265e5a51L); /* 19 */
    lngB = gg (lngB, lngC, lngD, lngA, x[ 0], S24, 0xe9b6c7aaL); /* 20 */
    lngA = gg (lngA, lngB, lngC, lngD, x[ 5], S21, 0xd62f105dL); /* 21 */
    lngD = gg (lngD, lngA, lngB, lngC, x[10], S22, 0x2441453L); /* 22 */
    lngC = gg (lngC, lngD, lngA, lngB, x[15], S23, 0xd8a1e681L); /* 23 */
    lngB = gg (lngB, lngC, lngD, lngA, x[ 4], S24, 0xe7d3fbc8L); /* 24 */
    lngA = gg (lngA, lngB, lngC, lngD, x[ 9], S21, 0x21e1cde6L); /* 25 */
    lngD = gg (lngD, lngA, lngB, lngC, x[14], S22, 0xc33707d6L); /* 26 */
    lngC = gg (lngC, lngD, lngA, lngB, x[ 3], S23, 0xf4d50d87L); /* 27 */
    lngB = gg (lngB, lngC, lngD, lngA, x[ 8], S24, 0x455a14edL); /* 28 */
    lngA = gg (lngA, lngB, lngC, lngD, x[13], S21, 0xa9e3e905L); /* 29 */
    lngD = gg (lngD, lngA, lngB, lngC, x[ 2], S22, 0xfcefa3f8L); /* 30 */
    lngC = gg (lngC, lngD, lngA, lngB, x[ 7], S23, 0x676f02d9L); /* 31 */
    lngB = gg (lngB, lngC, lngD, lngA, x[12], S24, 0x8d2a4c8aL); /* 32 */

    /* Round 3 */
    lngA = hh (lngA, lngB, lngC, lngD, x[ 5], S31, 0xfffa3942L); /* 33 */
    lngD = hh (lngD, lngA, lngB, lngC, x[ 8], S32, 0x8771f681L); /* 34 */
    lngC = hh (lngC, lngD, lngA, lngB, x[11], S33, 0x6d9d6122L); /* 35 */
    lngB = hh (lngB, lngC, lngD, lngA, x[14], S34, 0xfde5380cL); /* 36 */
    lngA = hh (lngA, lngB, lngC, lngD, x[ 1], S31, 0xa4beea44L); /* 37 */
    lngD = hh (lngD, lngA, lngB, lngC, x[ 4], S32, 0x4bdecfa9L); /* 38 */
    lngC = hh (lngC, lngD, lngA, lngB, x[ 7], S33, 0xf6bb4b60L); /* 39 */
    lngB = hh (lngB, lngC, lngD, lngA, x[10], S34, 0xbebfbc70L); /* 40 */
    lngA = hh (lngA, lngB, lngC, lngD, x[13], S31, 0x289b7ec6L); /* 41 */
    lngD = hh (lngD, lngA, lngB, lngC, x[ 0], S32, 0xeaa127faL); /* 42 */
    lngC = hh (lngC, lngD, lngA, lngB, x[ 3], S33, 0xd4ef3085L); /* 43 */
    lngB = hh (lngB, lngC, lngD, lngA, x[ 6], S34, 0x4881d05L); /* 44 */
    lngA = hh (lngA, lngB, lngC, lngD, x[ 9], S31, 0xd9d4d039L); /* 45 */
    lngD = hh (lngD, lngA, lngB, lngC, x[12], S32, 0xe6db99e5L); /* 46 */
    lngC = hh (lngC, lngD, lngA, lngB, x[15], S33, 0x1fa27cf8L); /* 47 */
    lngB = hh (lngB, lngC, lngD, lngA, x[ 2], S34, 0xc4ac5665L); /* 48 */

    /* Round 4 */
    lngA = ii (lngA, lngB, lngC, lngD, x[ 0], S41, 0xf4292244L); /* 49 */
    lngD = ii (lngD, lngA, lngB, lngC, x[ 7], S42, 0x432aff97L); /* 50 */
    lngC = ii (lngC, lngD, lngA, lngB, x[14], S43, 0xab9423a7L); /* 51 */
    lngB = ii (lngB, lngC, lngD, lngA, x[ 5], S44, 0xfc93a039L); /* 52 */
    lngA = ii (lngA, lngB, lngC, lngD, x[12], S41, 0x655b59c3L); /* 53 */
    lngD = ii (lngD, lngA, lngB, lngC, x[ 3], S42, 0x8f0ccc92L); /* 54 */
    lngC = ii (lngC, lngD, lngA, lngB, x[10], S43, 0xffeff47dL); /* 55 */
    lngB = ii (lngB, lngC, lngD, lngA, x[ 1], S44, 0x85845dd1L); /* 56 */
    lngA = ii (lngA, lngB, lngC, lngD, x[ 8], S41, 0x6fa87e4fL); /* 57 */
    lngD = ii (lngD, lngA, lngB, lngC, x[15], S42, 0xfe2ce6e0L); /* 58 */
    lngC = ii (lngC, lngD, lngA, lngB, x[ 6], S43, 0xa3014314L); /* 59 */
    lngB = ii (lngB, lngC, lngD, lngA, x[13], S44, 0x4e0811a1L); /* 60 */
    lngA = ii (lngA, lngB, lngC, lngD, x[ 4], S41, 0xf7537e82L); /* 61 */
    lngD = ii (lngD, lngA, lngB, lngC, x[11], S42, 0xbd3af235L); /* 62 */
    lngC = ii (lngC, lngD, lngA, lngB, x[ 2], S43, 0x2ad7d2bbL); /* 63 */
    lngB = ii (lngB, lngC, lngD, lngA, x[ 9], S44, 0xeb86d391L); /* 64 */

    lngState[0] = (lngState[0] + lngA) & 0xFFFFFFFFL;
    lngState[1] = (lngState[1] + lngB) & 0xFFFFFFFFL;
    lngState[2] = (lngState[2] + lngC) & 0xFFFFFFFFL;
    lngState[3] = (lngState[3] + lngD) & 0xFFFFFFFFL;

    /*System.out.println("lngState[0]="+lngState[0]);
    System.out.println("lngState[1]="+lngState[1]);
    System.out.println("lngState[2]="+lngState[2]);
    System.out.println("lngState[3]="+lngState[3]);*/
    x = decode (pad);
    }

    private static long ff(long lngA, long lngB, long lngC, long lngD, long lngX, long lngS, long lngAC) {
    lngA = (lngA + (lngB & lngC | (~lngB) & lngD) + lngX + lngAC) & 0xFFFFFFFFL;
    lngA = ((lngA << lngS) | (lngA >>> (32L-lngS))) & 0xFFFFFFFFL;
    lngA = (lngA + lngB) & 0xFFFFFFFFL;
    return(lngA);
    }

    private static long gg(long lngA, long lngB, long lngC, long lngD, long lngX, long lngS, long lngAC) {
    lngA = (lngA + (lngB & lngD | lngC & ~lngD) + lngX + lngAC) & 0xFFFFFFFFL;
    lngA = ((lngA << lngS) | (lngA >>> (32L-lngS))) & 0xFFFFFFFFL;
    lngA = (lngA + lngB) & 0xFFFFFFFFL;
    return(lngA);
    }

    private static long hh(long lngA, long lngB, long lngC, long lngD, long lngX, long lngS, long lngAC) {
    lngA = (lngA + (lngB ^ lngC ^ lngD) + lngX + lngAC) & 0xFFFFFFFFL;
    lngA = ((lngA << lngS) | (lngA >>> (32L-lngS))) & 0xFFFFFFFFL;
    lngA = (lngA + lngB) & 0xFFFFFFFFL;
    return(lngA);
    }

    private static long ii(long lngA, long lngB, long lngC, long lngD, long lngX, long lngS, long lngAC) {
    lngA = (lngA + (lngC ^ (lngB | ~lngD)) + lngX + lngAC) & 0xFFFFFFFFL;
    lngA = ((lngA << lngS) | (lngA >>> (32L-lngS))) & 0xFFFFFFFFL;
    lngA = (lngA + lngB) & 0xFFFFFFFFL;
    return(lngA);
    }

    private void update(char bytInput[], long lngLen) {
    int index = (int)( this.lngByteCount % 64);
    int i = 0;
    this.lngByteCount += lngLen;
    int partLen = 64 - index;
    if (lngLen >= partLen) {
    for (int j = 0; j < partLen; j++) {
    this.bytBuffer[j + index] = bytInput[j];
    //System.out.println("bytBuffer["+(j + index)+"]="+ (int) bytBuffer[j + index]);
    }
    transform (this.lngState, this.bytBuffer);

    for (i = partLen; i + 63 < lngLen; i += 64) {
    for (int j = 0; j<64; ++j) {
    this.bytBuffer[j] = bytInput[j+i];
    }
    transform (this.lngState, this.bytBuffer);
    }
    index = 0;
    }
    else {
    i = 0;
    }
    for (int j = 0; j < lngLen - i; j++) {
    this.bytBuffer[index + j] = bytInput[i + j];
    System.out.println("bytBuffer["+(index + j)+"]="+ (int) bytBuffer[index + j]);
    }
    //System.out.println("____________________________") ;

    }

    public void md5final() {
    char bytBits[] = new char[8];
    int index, padLen;
    long bits = this.lngByteCount * 8;

    bytBits[0] = (char) (bits & 0xffL);
    bytBits[1] = (char) ((bits >>> 8) & 0xffL);
    bytBits[2] = (char) ((bits >>> 16) & 0xffL);
    bytBits[3] = (char)((bits >>> 24) & 0xffL);
    bytBits[4] = (char)((bits >>> 32) & 0xffL);
    bytBits[5] = (char)((bits >>> 40) & 0xffL);
    bytBits[6] = (char)((bits >>> 48) & 0xffL);
    bytBits[7] = (char)((bits >>> 56) & 0xffL);
    //System.out.println("__________________________");
    /*System.out.println("bytBits[0]="+ (int) bytBits[0]);
    System.out.println("bytBits[1]="+ (int) bytBits[1]);
    System.out.println("bytBits[2]="+ (int) bytBits[2]);
    System.out.println("bytBits[3]="+ (int) bytBits[3]);
    System.out.println("bytBits[4]="+ (int) bytBits[4]);
    System.out.println("bytBits[5]="+ (int) bytBits[5]);
    System.out.println("bytBits[6]="+ (int) bytBits[6]);
    System.out.println("bytBits[7]="+ (int) bytBits[7]);
    System.out.println("__________________________");*/

    index = (int) this.lngByteCount%64;
    if (index < 56 ) {
    padLen = 56 - index;
    }
    else {
    padLen = 120 - index;
    }
    update(pad, padLen);
    update(bytBits, 8);
    }

    private StringBuffer toHexString() {
    long myByte = 0;
    StringBuffer mystring = new StringBuffer();
    for (int j = 0; j < 4; ++j) {
    for (int i = 0; i < 32; i += 8) {
    myByte = (this.lngState[j] >>> i) & 0xFFL;
    if (myByte < 16) {
    mystring.append("0" + Long.toHexString(myByte));
    }
    else {
    mystring.append(Long.toHexString(myByte));
    }
    }
    }
    return(mystring);
    }

    public void init() {
    this.lngByteCount = 0;
    this.lngState[0] = 1732584193L;
    this.lngState[1] = 4023233417L;
    this.lngState[2] = 2562383102L;
    this.lngState[3] = 271733878L;
    }

    public static void main (String args []) throws IOException {

    String strTestData;
    char chrTestData[] = new char[64];
    MD5 md5Test = new MD5();

    md5Test.init();
    strTestData = new String("Олим");
    chrTestData = strTestData.toCharArray();
    md5Test.update(chrTestData,chrTestData.length);
    md5Test.md5final();
    System.out.println("MD5 (" + strTestData +") = " + md5Test.toHexString() );
    }
    }

  7. #7
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default

    neilcoffey, I'm sorry for poor English, the code working incorrect with Cyrillic symbols

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    So first, I'm assuming your MD5 code is working (any reason not to just use the Java MessageDigest class?-- that would allow you to replace all of what you posted with about 3 lines of code!).

    Then beyond that, your code is still treating each character as though it were a byte. So at the very least, you'd need to do:

    Java Code:
    byte[] b = "My string".getBytes("UTF-8");
    char[] chrs = new char[b.length];
    for (int i = 0; i < b.length; i++) {
      chrs[i] = b[i] & 0xff;
    }
    ... then pass chars into your code as before ...
    But I'd really consider just using the MessageDigest class if you possibly can.

  9. #9
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default

    neilcoffey, I can use MessageDigest class, and I already did it. First of all, I want to answer: My scientific leader wants that, I understood MD5 algorithm. That's why I want to realize MD5 algorithm, but, anyway, it is very interesting algorithm. Thank you very much for advises, but if I'll have any problems, can I ask you?

  10. #10
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    I'll do my best.

    Even if you have to implement MD5 yourself as an exercise, I'd recommend you use the MessageDigest class to confirm the digest of the string as Java converts them (with either UTF-8 or UTF-16-- the essential point is you need an encoding that doesn't "chop off" the top byte of characters that are more than one byte). Checking against the MessageDigest class eliminates the possibility that your on-line tool is simply converting the characters wrongly.

  11. #11
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Problem again

    look:
    public class Salom1 {
    public static void main(String[] args) {
    try {
    byte[] b = "Привет".getBytes("UTF-8");
    char[] chrs = new char[b.length];
    for (int i = 0; i < b.length; i++) {
    chrs[i] = b[i] & 0xff;
    System.out.println(chrs[i]);
    }
    }
    catch (Exception ex) {
    System.out.println(ex);
    }
    }
    }
    There are "possible loss of precision" error on 7th line!
    But this code is correct:
    public class Salom1 {
    public static void main(String[] args) {
    try {
    byte[] b = "Привет".getBytes("UTF-8");
    char[] chrs = new char[b.length];
    for (int i = 0; i < b.length; i++) {
    System.out.println(b[i] & 0xff);
    }
    }
    catch (Exception ex) {
    System.out.println(ex);
    }
    }
    }

    Sorry, for taking your precious time, but if you do not help me, I will have nobody that advise me...

  12. #12
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default On more thing

    And it is giving two results, if I give one Russian symbol. For example,
    byte[] b = "А".getBytes("UTF-8"); //А - Cyrillic
    OUTPUT:
    b.length = 2
    208
    144

  13. #13
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Yes, generally, one Cyrillic character = two bytes. Exactly which two bytes depends on the character encoding, but UTF-16 is a common encoding.

  14. #14
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Exclamation Problem again

    Sorry, It's me again... am I so dumb :( ? Neil Coffey, in Pascal index of Cyrillic symbols begins 128, and I thought than Pascal works with ASCII, but when I saw ASCII table in internet, I am thinking, what encoding Pascal works with??? Can Java translit Cyrillic symbols? Or is it translit:
    Java Code:
    public class Salom1 {
    public static void main(String[] args) {
      try {
        byte[] b = "А".getBytes("KOI8-R");
        char[] chrs = new char[b.length];
        for (int i = 0; i < b.length; i++) {
          chrs[i] = (char) (b[i] & 0xff);
          System.out.println( (int) chrs[i]+"  - "+chrs[i]);
        }
      }
      catch (Exception ex) {
        System.out.println(ex);
      }
    }
    }
    I want if index of 'А' (Cyrillic) is 1041, then convert (or translit) the number to 128
    Б - 1041 - 129
    В - 1042 - 130
    Г - 1043 - 131
    Д - 1044 - 132
    ...
    I think MD5 class uses such a converting...

  15. #15
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Pascal probably does work with ASCII. The problem is that there's no standard "ASCII" way of representing Cyrillic-- or indeed any character at all beyond very basic Latin characters and a few punctuation symbols. ASCII allows fewer than 256 characters, and it's simply impossible to accommodate all the common alphabets used around the world within that number of characters! So what used to happen is that different countries/systems just used the top half of the ASCII range (characters 128-255) in some arbitrary way to accommodate the characters they needed, and various "standards" have grown up over the years. The one you mention-- KOI8-R-- is quite common for representing Cyrillic in Russia, I believe. But, for example, in Bulgaria, I understand that another encoding is more common. I'm not aware of a standard encoding that encodes Cyrillic characters starting at position 128, but evidently it sounds like some systems use such a scheme.

    I imagine that this encoding isn't tied to the Pascal language per se-- just that Pascal knows nothing about character encoding, so it's just using whatever the computer it is running on happens to choose. I imagine.

    If you use Unicode, now pretty much an international standard, then you get round much of this confusion-- each character of each common alphabet is just given a standard code (though that still doesn't answer questions about how you use those characters, of course-- characters such as apostrophe vs quotes or full stops vs ellipses continue to be confused). But to accommodate the tens of thousands of possible characters used around the world, characters have to be represented by more than one byte.

    MD5 doesn't care or know anything about character encoding. It just works on a series of bytes. Which character encoding you use to turn your characters into bytes is a completely arbitrary decision that you (and anybody else writing a similar program) must make. If for some reason you want to use the same coding that Pascal used on some computer (though I can't really think why you'd want to do this!), then you're at liberty to copy that-- though in this case, you may have to do a bit of the coding, as I don't think it's a standard character set.

    If you have no particular reason to use anything else, I would strongly recommend Unicode encoded with UTF-8 (i.e. the character set you pass in to getBytes() is "UTF-8"). It's a very common international standard.

  16. #16
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Hi

    Hi, Neil Coffey!
    Look:
    Java Code:
    import java.io.*;
    
    class OUTPUT {
    	OutputStreamWriter  rusOut=null;
    	public OUTPUT() {
    		try {
    			rusOut = new OutputStreamWriter(System.out,"UNICODE");
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    	
    	public void print(String text) {
    		try {
    			rusOut.write(text);
    			rusOut.flush();
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    	
    }
    
    class SubStringCons {
    	public static void main(String args[]) {
    		char ascii[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
    		String s1 = new String(ascii);
    		OUTPUT OT = new OUTPUT();
    		for (int i=0; i<=255; i++) {
    			System.out.print(i+"  -  ");
    			OT.print(s1.charAt(i)+"  ");
    		}
    		
    	}
    }
    The code is giving same ASCII table, which Pascal gives. You are absolutely right, ASCII is different in different platforms.
    Now, I've a question, how can I get index of 'П' in s1? I wrote code but, it's working incorrect, because index of 'П' is 1055 in Unicode.

  17. #17
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Let me explain my problem

    So, let me explain my provlem:
    class SubStringCons {
    public static void main(String args[]) {
    char ascii[] = {0,...,255};
    String s1 = new String(ascii);
    int index = s1.indexOf('П');
    System.out.println(index);
    }
    }

    Result is -1!!! Why? :confused:

  18. #18
    Join Date
    Mar 2009
    Posts
    16
    Rep Power
    0

    Default Here a result

    Here a result, which cod is:
    Java Code:
    import java.io.*;
    
    class OUTPUT {
    	OutputStreamWriter  rusOut=null;
    	public OUTPUT() {
    		try {
    			rusOut = new OutputStreamWriter(System.out,"ISO8859_1");
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    	
    	public void print(String text) {
    		try {
    			rusOut.write(text);
    			rusOut.flush();
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    	
    }
    
    class SubStringCons {
    	public static void main(String args[]) {
    		char ascii[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
    		String s1 = new String(ascii);
    		OUTPUT OT = new OUTPUT();
    		OT.print("Symbol in 143 place is - "+s1.charAt(143));
    		try {
    			int index = s1.indexOf('П');
    			System.out.println();
    			System.out.println("It's index in s1 string is: "+index);
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    		for (int i=0; i<=255; i++) {
    			if (i%7==0) System.out.println();
    			System.out.print(i+"  -  ");
    			OT.print(s1.charAt(i)+"  ");
    		}
    		System.out.println();
    	}
    }
    Java Code:
    Symbol in 143 place is - П
    It's index in s1 string is: -1
    
    0  -     1  -  ☺  2  -  ☻  3  -  ♥  4  -  ♦  5  -  ♣  6  -  ♠
    7  -    8  -   9  -       10  -
      11  -  ♂  12  -  ♀  13  -
    14  -  ♫  15  -  ☼  16  -  ►  17  -  ◄  18  -  ↕  19  -  ‼  20  -  
    21  -    22  -  ▬  23  -  ↨  24  -  ↑  25  -  ↓  26  -  →  27  -  ←
    28  -  ∟  29  -  ↔  30  -  ▲  31  -  ▼  32  -     33  -  !  34  -  "
    35  -  #  36  -  $  37  -  %  38  -  &  39  -  '  40  -  (  41  -  )
    42  -  *  43  -  +  44  -  ,  45  -  -  46  -  .  47  -  /  48  -  0
    49  -  1  50  -  2  51  -  3  52  -  4  53  -  5  54  -  6  55  -  7
    56  -  8  57  -  9  58  -  :  59  -  ;  60  -  <  61  -  =  62  -  >
    63  -  ?  64  -  @  65  -  A  66  -  B  67  -  C  68  -  D  69  -  E
    70  -  F  71  -  G  72  -  H  73  -  I  74  -  J  75  -  K  76  -  L
    77  -  M  78  -  N  79  -  O  80  -  P  81  -  Q  82  -  R  83  -  S
    84  -  T  85  -  U  86  -  V  87  -  W  88  -  X  89  -  Y  90  -  Z
    91  -  [  92  -  \  93  -  ]  94  -  ^  95  -  _  96  -  `  97  -  a
    98  -  b  99  -  c  100  -  d  101  -  e  102  -  f  103  -  g  104  -  h
    105  -  i  106  -  j  107  -  k  108  -  l  109  -  m  110  -  n  111  -  o
    112  -  p  113  -  q  114  -  r  115  -  s  116  -  t  117  -  u  118  -  v
    119  -  w  120  -  x  121  -  y  122  -  z  123  -  {  124  -  |  125  -  }
    126  -  ~  127  -  ⌂  128  -  А  129  -  Б  130  -  В  131  -  Г  132  -  Д
    133  -  Е  134  -  Ж  135  -  З  136  -  И  137  -  Й  138  -  К  139  -  Л
    140  -  М  141  -  Н  142  -  О  143  -  П  144  -  Р  145  -  С  146  -  Т
    147  -  У  148  -  Ф  149  -  Х  150  -  Ц  151  -  Ч  152  -  Ш  153  -  Щ
    154  -  Ъ  155  -  Ы  156  -  Ь  157  -  Э  158  -  Ю  159  -  Я  160  -  а
    161  -  б  162  -  в  163  -  г  164  -  д  165  -  е  166  -  ж  167  -  з
    168  -  и  169  -  й  170  -  к  171  -  л  172  -  м  173  -  н  174  -  о
    175  -  п  176  -  ░  177  -  ▒  178  -  ▓  179  -  │  180  -  ┤  181  -  ╡
    182  -  ╢  183  -  ╖  184  -  ╕  185  -  ╣  186  -  ║  187  -  ╗  188  -  ╝
    189  -  ╜  190  -  ╛  191  -  ┐  192  -  └  193  -  ┴  194  -  ┬  195  -  ├
    196  -  ─  197  -  ┼  198  -  ╞  199  -  ╟  200  -  ╚  201  -  ╔  202  -  ╩
    203  -  ╦  204  -  ╠  205  -  ═  206  -  ╬  207  -  ╧  208  -  ╨  209  -  ╤
    210  -  ╥  211  -  ╙  212  -  ╘  213  -  ╒  214  -  ╓  215  -  ╫  216  -  ╪
    217  -  ┘  218  -  ┌  219  -  █  220  -  ▄  221  -  ▌  222  -  ▐  223  -  ▀
    224  -  р  225  -  с  226  -  т  227  -  у  228  -  ф  229  -  х  230  -  ц
    231  -  ч  232  -  ш  233  -  щ  234  -  ъ  235  -  ы  236  -  ь  237  -  э
    238  -  ю  239  -  я  240  -  Ё  241  -  ё  242  -  Є  243  -  є  244  -  Ї
    245  -  ї  246  -  Ў  247  -  ў  248  -    249  -  ∙  250  -    251  -  √
    252  -  №  253  -    254  -  ■  255  -  *

Posting Permissions

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