Page 1 sur 1

Hex to ASCII text converter

Posté : mer. 20 mars 2019, 12:00:47
par fbillard
Bonjour,

Je suis a la recherche d'une solution (RPG, SQL, ...) pour convertir ma chaîne hexadécimal en ASCII.
Exemple : 3930303031373638 (Hex) --> 90001768 (ASCII)

Au même titre que cette page WEB
https://www.rapidtables.com/convert/num ... ascii.html

Merci.

(sans sujet)

Posté : mer. 20 mars 2019, 12:05:10
par florian67
Salut,

Je ne sais pas si c'est ce que tu cherches à faire mais j'ai trouvé ça :
https://itknowledgeexchange.techtarget. ... decimal-2/

Florian

(sans sujet)

Posté : jeu. 21 mars 2019, 16:15:39
par monthi
Bonjour

voici l'équivalent en 5250 (je pense)

DSPF

Code : Tout sélectionner

     A                                      DSPSIZ(27 132 *DS4                 -
     A                                             24 80 *DS3)
     A                                      CA03
     A                                      CA10
     A          R F01
    A                                  1 53'Hex to ASCII text converter'
     A                                  3  2'Hex input'
     A            HEXA        1200A  B  3 12DSPATR(HI)
     A                                      DSPATR(UL)
     A  60                                  ERRMSG('La chaîne hexa n''est pas u-
     A                                      n multiple de 2' 60)
     A                                 14  2'ASCII Text output'
     A            ASCII       1200A  O 15 12DSPATR(UL)
     A                                 26 12'Enter=Convert'
     A                                 26 32'F3=Exit'
     A                                 26 44'F10=Swap'
     A          R F01B
    A                                  1 53'ASCII to Hex text converter'
     A                                  3  2'Ascii input'
     A            ASCII2       600A  B  3 14DSPATR(HI)
     A                                      DSPATR(UL)
     A                                      CHECK(LC)
     A                                 10  2'Hex output'
     A            HEXA2       1800   O 10 14
     A                                 26 12'Enter=Convert'
     A                                 26 32'F3=Exit'
     A                                 26 44'F10=Swap'         

(sans sujet)

Posté : jeu. 21 mars 2019, 16:16:02
par monthi
Le programme

Code : Tout sélectionner

       ctl-opt actgrp(*caller);

       dcl-f AFFASCIIF workstn;

       dcl-s Hexval char(16) INZ('0123456789ABCDEF'); // car hexa valide
       dcl-s sens char(1) inz('H');

       dcl-pr Cvthc  extproc('cvthc');
         *n pointer value;
         *n pointer value;
         *n int(10) value;
       End-Pr;


       dow 1=1;
         if sens='H';
           exfmt F01;
         else;
           exfmt F01B;
         endif;
         if *inkc;
           leave;
         elseif *inkJ; // swap;
           if Sens='H';
             sens='A';
           else;
             sens='H';
           EndIf;
           iter;
         EndIf;

         if sens='H';
           convHex2ascii();
         else;
           convAscii2hex();
         endif;
       EndDo;

       *inlr=*on;
       //***************************************************************
       // Conversion Hex vers AScii

       dcl-proc convHex2ascii;

         dcl-s tmp varchar(600);
         dcl-s hex char(2) based(ptr);
         dcl-s car char(1) based(ptr);
         dcl-s tmphex varchar(1200);
         dcl-s ptr_end pointer;

         // suppression des espaces éventuels entre les caractère hex
         tmphex=%scanrpl(' ':'':hexa);

         *in60= %rem&#40;%len&#40;tmphex&#41;&#58;2&#41;<>0;
         if *in60;
           //pas un multiple de 2
           return;
         EndIf;

         // code hex en maj
         tmphex=%xlate&#40;'abcdef'&#58;'ABCDEF'&#58;tmphex&#41;;
         ptr=%addr&#40;tmphex&#58;*data&#41;;
         ptr_end=ptr+%len&#40;tmphex&#41;;

         dow ptr<ptr_end;
           tmp+=cvthexa&#40;hex&#41;;
           ptr+=2;
         EndDo;

         ascii=cvtascii&#40;'A2E'&#58;tmp&#41;;
         //pour affichage ecran 5250 on replace les caratère < X'40' &#40;espace&#41;
         // par X'40'

         ptr=%addr&#40;ascii&#41;;
         ptr_end=ptr+%len&#40;tmp&#41;;
         dow ptr<ptr_end;
           if car<X'40';
             car=X'40';
           EndIf;
           ptr+=1;
         EndDo;


       End-Proc;
       //***************************************************************
       // Conversion AScii vers hex

       dcl-proc convAscii2hex;

         dcl-s tmp varchar&#40;600&#41;;
         dcl-s car char&#40;1&#41; based&#40;ptr&#41;;
         dcl-s car2 char&#40;2&#41;;
         dcl-s tmphex varchar&#40;1200&#41;;
         dcl-s ptr_end pointer;

         // suppression des espaces éventuels entre les caractère hex
         tmp=cvtascii&#40;'E2A'&#58;%trim&#40;ascii2&#41;&#41;;


         ptr=%addr&#40;tmp&#58;*data&#41;;
         ptr_end=ptr+%len&#40;tmp&#41;;

         dow ptr<ptr_end;
           cvthc&#40;%addr&#40;car2&#41;&#58;ptr&#58;2&#41;;
           if tmphex<>'';
             tmphex+=' ';
           EndIf;
           tmphex+=car2;
           ptr+=1;
         EndDo;

         hexa2=tmphex;
       End-Proc;

       //*****************************************************************************
       //**valeur Hexa&#130;
       dcl-Proc cvthexa;

         Dcl-pi *n char&#40;1&#41;;
           VALHEXX char&#40;2&#41; const;
         end-pi;

         Dcl-ds bin$;
           BIN1 uns&#40;3&#41;;
         end-ds;


         BIN1 = &#40;&#40;%scan&#40;%subst&#40;valhexx&#58;1&#58;1&#41;&#58;Hexval&#41;-1&#41;*16&#41; +
         &#40;%scan&#40;%subst&#40;valhexx&#58;2&#58;1&#41;&#58;Hexval&#41;-1&#41;;
         RETURN BIN$;
       //
       end-proc;
       //*************************************************************************************
       dcl-Proc cvtAscii;

         dcl-pi *n varchar&#40;600&#41;;
           Sens char&#40;3&#41; const;
           Ebcdic varchar&#40;1200&#41; value;
         end-pi;

         dcl-s ptrin  pointer;
         Dcl-s p_Bufout  pointer;
         Dcl-s BufInsize uns&#40;10&#41;;
         Dcl-s BufOutsize uns&#40;10&#41;;
         Dcl-s rc uns&#40;10&#41;;
         Dcl-s bufout  char&#40;600&#41;;
         dcl-s car char&#40;1&#41; based&#40;ptr_car&#41;;

         Dcl-ds ToAscii;
           ICORV_A  bindec&#40;9&#41;;
           ICOC_A   bindec&#40;9&#41; DIM&#40;00012&#41;;
         end-ds;
         //
         Dcl-ds dsFROM;
           from_ccsid int&#40;10&#41; Inz&#40;819&#41;;
           from_ca int&#40;10&#41; Inz&#40;0&#41;;
           from_Sa int&#40;10&#41; Inz&#40;0&#41;;
           from_ss int&#40;10&#41; Inz&#40;1&#41;;
           from_il int&#40;10&#41; Inz&#40;0&#41;;
           from_E0 int&#40;10&#41; Inz&#40;1&#41;;
           from_r  char&#40;8&#41; INZ&#40;*allx'00'&#41;;
         end-ds;

         dcl-ds dsTO;
           To_ccsid int&#40;10&#41; inz&#40;0&#41;;
           To_ca int&#40;10&#41; Inz&#40;0&#41;;
           To_Sa int&#40;10&#41; Inz&#40;0&#41;;
           To_ss int&#40;10&#41; Inz&#40;1&#41;;
           To_il int&#40;10&#41; Inz&#40;0&#41;;
           To_E0 int&#40;10&#41; Inz&#40;1&#41;;
           To_r  char&#40;8&#41; INZ&#40;*allx'00'&#41;;
         end-ds;

         Dcl-pr IConvOpen   char&#40;52&#41;   ExtProc&#40;'QtqIconvOpen'&#41;;
           *n pointer  Value options&#40;*string&#41;; //            To Code
           *n pointer Value options&#40;*string&#41;; //Fr Code
         end-pr;

         Dcl-pr IConv int&#40;10&#41; ExtProc&#40;'iconv'&#41;;
           *n Char&#40;52&#41; Value; // Code Discripter
           *n pointer  value; // ptr to InBuffer Ptr
           *n uns&#40;10&#41;; // inSize ptr
           *n pointer value; // ptr to OutBuffer Pt
           *n uns&#40;10&#41;; // OutSize ptr
         end-pr;

         Dcl-pr IConvClose  int&#40;10&#41;  ExtProc&#40;'iconv_close'&#41;;
           *n char&#40;52&#41; Value;
         end-pr;

         if sens='A2E';
           from_ccsid=819;
           To_ccsid=0;
         else;
           from_ccsid=0;
           To_ccsid=819;
         EndIf;

         ToAscii=IConvOpen&#40;%addr&#40;dsTo&#41;&#58;%addr&#40;dsFROM&#41;&#41;;
         ptrin=%addr&#40;Ebcdic&#58;*data&#41;;
         bufInsize=%len&#40;Ebcdic&#41;;
         bufout='';
         BufOutsize=%size&#40;bufout&#41;;
         p_bufout=%addr&#40;bufout&#41;;
         rc=iconv&#40;ToAscii&#58;%addr&#40;ptrin&#41;&#58;bufinsize&#58;%addr&#40;p_bufout&#41;&#58;bufOutsize&#41;;
         iconvclose&#40;ToAscii&#41;;

         return %subst&#40;bufout&#58;1&#58;%size&#40;bufout&#41;-bufOutsize&#41;;
       end-proc;

 
Cdlt