Code ascii

tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007 - 11 nov. 2007 à 03:05
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007 - 14 nov. 2007 à 20:07
bonjour à tous  et merci d'avoir cliquez sur moi .
Alors voila j'ai un probleme pour récuperer la valeur du code ascii dun caractere speciaux
mais le resultat n'est pas celui que j'attends quelqu un peut mexpliquer pourquoi ?
je voudrais qu'il maffiche le code ascii de Ö et il maffiche -99 ..
je crois que c un probleme de charset siouplait alaide !
voici mon programe :
MAIN.c
<hr size="2" width="100%" />
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "charset.h"

int main(int argc,char* argv[],int iNbArg,char ** capArg)
{
    // Chaîne Iso8859-1
    printf("declaration des variable\n");
    PAUSE();
    long i = 1;
    char password[]=("klbmfgh");
    long j=0;
    long k=0;
    long pwdec[]={0};
    char url[]="";
    char verify[]="";
    char *decode[17]={0};
    int caracActuel= 0;
    char copycaeascii[100]={0};
    char caSrc[]="ÖØÄÚÞÌ×îè×ÏÞÓÝèòØ";/* var unknown */
    int iStrLen = strlen(caSrc),ret;
    FILE *unknown=NULL;

    printf("Transformation du contenu de la variable unknown en ASCII\n");
    PAUSE();
    char caEAscii[iStrLen];
    ret = iso8859ToEascii(caSrc,iStrLen,caEAscii,iStrLen);
    caEAscii[ret] = 0;
    printf("Extended Ascii: %s\n",caEAscii);/* EXTENDED ASCII */
    strcpy(copycaeascii,caEAscii);    printf("var unknown %s\n",copycaeascii); /* var unknown copycaeascii */
    PAUSE();
    for (k=0;k < strlen(caSrc); k++)
    {

    }

PAUSE();

printf("la valeur de decode est : %s\n",decode);

 if (strcmp(copycaeascii,caEAscii) == 0)
 {
     printf("la copy de unknow est identique a la valeur de unknown soit :\n%c\n",copycaeascii[1]);
     PAUSE();
     printf("le code ascii de %c est %d\n",copycaeascii[1],copycaeascii[1]);
     PAUSE();
 }
 else
 {
 printf("les chaine ne sont pas identique\n");
 }
    system("pause");
    return 0;
}

CHARSET.h :

<hr size="2" width="100%" />

#ifndef UNICODE
#define UNICODE
#define wchar               unsigned short // Unicode is encoded in 16 bits

#define DEST_TOO_SMALL        0
#define UTF8_BAD_STRING      -1
#define INCOMPATIBLE_UNICODE -1
#define INCOMPATIBLE_EASCII  -1
#define INCOMPATIBLE_ISO8859 -1
#define PAUSE() system("PAUSE");\
printf("\n\n");
#define TAILLEMAX 100000

char* strcpy(char* copieDeLaChaine, const char* chaineACopier);

int strcmp(const char* chaine1, const char* chaine2);
unsigned int easciiToIso8859(const char * capSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen);
unsigned int iso8859ToUnicode(const char * capSrc,unsigned int iSrcLen,wchar * wcapDest,unsigned int iDestLen);
unsigned int unicodeToUtf8(const wchar * wcapSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen);
unsigned int utf8ToUnicode(const char * capSrc,unsigned int iSrcLen,wchar * wcapDest,unsigned int iDestLen);
unsigned int unicodeToIso8859(const wchar * wcapSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen);
unsigned int iso8859ToEascii(const char * capSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen);
#endif

CHARSET.C

<hr size="2" width="100%" />

/**
 * Functions to Convert beetween different charset
 * author Julien Folly
 * version 1.0
 */

#include "charset.h"

#define ASCII               0x7f    // 7 Bits
#define ISO_8859            0xff    // 8 Bits

#define UTF8_MAX_IN_1       0x7f     //  7 bits (2^7-1)
#define UTF8_MAX_IN_2       0x7ff    // 11 bits (2^11-1)
#define UTF8_MAX_IN_3       0xffff   // 16 bits (2^16-1)

#define UTF8_1ST_OF_1       0     // 0xxx xxxx
#define UTF8_1ST_OF_2       0xc0  // 110x xxxx
#define UTF8_1ST_OF_3       0xe0  // 1110 xxxx
#define UTF8_TRAIL          0x80  // 10xx xxxx

/**
 * Convert from Extended ascii to ISO 8859-1
 * Return iSrcLen, DEST_TOO_SMALL if the destination is too small, or
 * INCOMPATIBLE_EASCII if it's impossible to convert from eascii to ios8835
 * The iDestLen should be equal to iSrcLen
 *
 * capSrc    ->    Pointer to the string to convert
 * iSrcLen   ->    Number of character in capSrc
 * capDest   ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int easciiToIso8859(const char * capSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen){
    if(iDestLen < iSrcLen) return DEST_TOO_SMALL;
    unsigned int i,ret = iSrcLen;
    for(i 0 ; i ASCII) ret INCOMPATIBLE_EASCII;
                capDest[i] = capSrc[i];
        }
    }
    return ret;
}

/**
 * Convert from ISO 8859-1 to Unicode
 * Return iSrcLen or DEST_TOO_SMALL if the destination is too small
 * The iDestLen should be equal to iSrcLen
 *
 * capSrc    ->    Pointer to the string to convert
 * iSrcLen   ->    Number of character in capSrc
 * wcapDest  ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int iso8859ToUnicode(const char * capSrc,unsigned int iSrcLen,wchar * wcapDest,unsigned int iDestLen){
    if(iDestLen < iSrcLen) return DEST_TOO_SMALL;
    unsigned int i;

    for(i = 0 ; i    Pointer to the string to convert
 * iSrcLen   ->    Number of character in capSrc
 * capDest   ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int unicodeToUtf8(const wchar * wcapSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen){
    if(iDestLen == 0) return DEST_TOO_SMALL;
    unsigned int iNbUTF8 = 0;
    unsigned int i;

    for(i = 0 ; i iDestLen) return DEST_TOO_SMALL;
        }else

        // Found 2 bits sequence
        if(wcapSrc[i] <= UTF8_MAX_IN_2){
            capDest[iNbUTF8++] = UTF8_1ST_OF_2 | (wcapSrc[i] >> 6  );
            if(iNbUTF8 > iDestLen) return DEST_TOO_SMALL;

            capDest[iNbUTF8++] = UTF8_TRAIL      | (wcapSrc[i] & 0x3F);
            if(iNbUTF8 > iDestLen) return DEST_TOO_SMALL;
        }else

        // Found 3 bits sequence
        /* if(wcapSrc[i] <= UTF8_MAX_IN_3) */{
            capDest[iNbUTF8++] = UTF8_1ST_OF_3 | (wcapSrc[i] >> 12);
            if(iNbUTF8 > iDestLen) return DEST_TOO_SMALL;

            capDest[iNbUTF8++] = UTF8_TRAIL        | ((wcapSrc[i] >> 6) & 0x3F);
            if(iNbUTF8 > iDestLen) return DEST_TOO_SMALL;

            capDest[iNbUTF8++] = UTF8_TRAIL        | (wcapSrc[i] & 0x3F);
            if(iNbUTF8 > iDestLen) return DEST_TOO_SMALL;
        }
    }
    return iNbUTF8;
}

/**
 * Convert from UTF8 to unicode
 * Return the number of character, DEST_TOO_SMALL if the destination is too small,
 * or UTF8_BAD_STRING if the UTF8 String is invalid
 * The iDestLen should be equal to iSrcLen
 *
 * capSrc    ->    Pointer to the string to convert
 * iSrcLen   ->    Number of byte in capSrc
 * wcapDest  ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int utf8ToUnicode(const char * capSrc,unsigned int iSrcLen,wchar * wcapDest,unsigned int iDestLen){
    if(iDestLen == 0) return DEST_TOO_SMALL;
    unsigned int iNbUnicode = 0;
    unsigned int i;

    for(i = 0 ; i= UTF8_1ST_OF_3){
            if(i+2 >= iSrcLen) return UTF8_BAD_STRING;

            // First 8 bits
            wcapDest[iNbUnicode  ]  = ((capSrc[  i] << 4)               ) <<8;
            wcapDest[iNbUnicode  ] |= ((capSrc[++i] >> 2) & 0xf         ) <<8;

            // Last 8 bits
            wcapDest[iNbUnicode  ] |= (capSrc[  i] & 0x3) << 6;
            wcapDest[iNbUnicode++] |=  capSrc[++i] & 0x3F;

            if(iNbUnicode > iDestLen) return DEST_TOO_SMALL;
        }else

        // Found 2 bits sequence
        if((unsigned char)capSrc[i] >= UTF8_1ST_OF_2){
            if(i+1 >= iSrcLen) return UTF8_BAD_STRING;
            // First 8 bits
            wcapDest[iNbUnicode  ]   =  ((capSrc[  i] >> 2 ) & 0x7      ) <<8;

            // Last 8 bits
            wcapDest[iNbUnicode  ]  |= (capSrc[  i] & 0x3) << 6;
            wcapDest[iNbUnicode++]  |=  capSrc[++i] & 0x3F;

            if(iNbUnicode > iDestLen) return DEST_TOO_SMALL;
        }else

        // Found 1 bits sequence (ASCII)
        /* if((unsigned char)capSrc[i] >= UTF8_1ST_OF_1) */{
            wcapDest[iNbUnicode++] = capSrc[i];
            if(iNbUnicode > iDestLen) return DEST_TOO_SMALL;
        }
    }
    return iNbUnicode;
}

/**
 * Convert from Unicode to ISO 8859-1
 * Return iSrcLen, DEST_TOO_SMALL if the destination is too small, or
 * INCOMPATIBLE_UNICODE if it's impossible to convert from unicode to ios8835
 * The iDestLen should be equal to iSrcLen
 *
 * wcapSrc   ->    Pointer to the string to convert
 * iSrcLen   ->    Number of character in capSrc
 * capDest   ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int unicodeToIso8859(const wchar * wcapSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen){
    if(iDestLen < iSrcLen) return DEST_TOO_SMALL;
    unsigned int i,ret = iSrcLen;
    for(i 0 ; i> 8 > 0) ret INCOMPATIBLE_UNICODE;
        capDest[i] = wcapSrc[i];
    }
    return ret;
}

/**
 * Convert from  ISO 8859-1 to Extended ascii
 * Return iSrcLen, DEST_TOO_SMALL if the destination is too small, or
 * INCOMPATIBLE_ISO8859 if it's impossible to convert from ios8835 to eascii
 * The iDestLen should be equal to iSrcLen
 *
 * capSrc    ->    Pointer to the string to convert
 * iSrcLen   ->    Number of character in capSrc
 * capDest   ->    Pointer to an allocated memory area for the converted string
 * iDestLen  ->    Size of the allocated memory area
 */
unsigned int iso8859ToEascii(const char * capSrc,unsigned int iSrcLen,char * capDest,unsigned int iDestLen){
    if(iDestLen < iSrcLen) return DEST_TOO_SMALL;
    unsigned int i,ret = iSrcLen;
    for(i 0 ; i ASCII) ret INCOMPATIBLE_ISO8859;
                capDest[i] = capSrc[i];
        }
    }
    return ret;
}

8 réponses

tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
11 nov. 2007 à 03:37
Jai simplifié le main c pour plus de lisibilité .
( non parckeu depuis que je me prends la tete avec ce probleme c devenu le  foutoir )
main.c

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "charset.h"

int main(int argc,char* argv[],int iNbArg,char ** capArg)
{
    // Chaîne Iso8859-1
    printf("declaration des variable\n");
    PAUSE();
    long i = 1;
    char password[]=("klbmfgh");
    long j=0;
    long k=0;
    long pwdec[]={0};
    char url[]="";
    char verify[]="";
    char *decode[17]={0};
    char copycaeascii[100]={0};
    char caSrc[]="ÖØÄÚÞÌ×îè×ÏÞÓÝèòØ";/* var unknown */
    int iStrLen = strlen(caSrc),ret;
    FILE *unknown=NULL;

    printf("Transformation du contenu de la variable unknown en ASCII\n");
    PAUSE();
    char caEAscii[iStrLen];
    ret = iso8859ToEascii(caSrc,iStrLen,caEAscii,iStrLen);
    caEAscii[ret] = 0;
    printf("Extended Ascii: %s\n",caEAscii);/* EXTENDED ASCII */
    strcpy(copycaeascii,caEAscii);    printf("var unknown %s\n",copycaeascii); /* var unknown copycaeascii */
    PAUSE();
    for (k=0;k < strlen(caSrc); k++)
    {
        printf("le code ascii de %c est %d\n",copycaeascii[k],copycaeascii[k]);

        }

    system("pause");
    return 0;
}
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
11 nov. 2007 à 09:31
Faire affichage en tant que NON signé.

ciao...
BruNews, MVP VC++
0
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
12 nov. 2007 à 03:36
J'ai déja essayé toute les forme de printf() %d %u %o %x etc .. rien ne marche je ne comprends pas .
0
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
12 nov. 2007 à 03:56
ah ouai t'avais raison ca marche quand je declare la copy entend que char non signé.
merci bcp
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
12 nov. 2007 à 05:21
oui mais ya vraiment un probleme avec le charse on dirait bien
je cherche donc le code ascii des caractere de la chaine ÖØÄÚÞÌ×îè×ÏÞÓÝèòØ
seulement je cherche a ckil me donne le code ascii du jeu de caractere en windows-1252.
donc pour Ö j'aimerais qu'il me renvois 214 et pas 153 comme il le fait maintenant.

j'ai changer ca dans main.c :
<hr size="2" width="100%" />

unsigned char copycaeascii[100]={0};

et

printf("le code ascii de %c est %u\n",copycaeascii[k],copycaeascii[k]);
0
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
13 nov. 2007 à 22:53
Siouplait ya personne pour m'aider ?
0
Alcantornet Messages postés 89 Date d'inscription mardi 8 février 2005 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 16:50
printf("le code ascii de %c est %u\n",copycaeascii[k],caSrc[k]);

et si tu met ça donne quoi ?
0
tasken2 Messages postés 7 Date d'inscription dimanche 5 septembre 2004 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 20:07
Ca me donne 4294967254 je crois quec ladresse du pointeur chu pas sur.
mais ca va pour ca j'ai reussis a passer outre mon manque de compétence

 char caSrc[]="ÖØÄÚÞÌ×îè×ÏÞÓÝèòØ";
    long decode[17] = {214,216,196,218,222,204,215,238,232,215,207,222,211,221,232,242,216};

j'ai regarder sur google l'equivalent du code ÖØÄÚÞÌ×îè×ÏÞÓÝèòØ en windows-1252
et j'ai vu que ca faisait ca :
214,216,196,218,222,204,215,238,232,215,207,222,211,221,232,242,216
mais j'ai une variable qui va devoir contenir des caractere speciaux dont je n'ai pas prédéfini a l'avance leur code decimale  windows-1252.
et comme la console comprends rien a ce jeu de caractere j'ai  cru compris
qu'elle utiliser l'iso 8859 chu pas sur non plus bref ..
Quelque pense que je devrais creer une fonction pour simuler le jeu de caractere windows-1252  comme Julien Folly et sa library que j'utilise ?
comment devrais je procédé
0
Rejoignez-nous