HEX in CString 1:1



  • Hallo,

    folgendes ist gegeben in einem char Array SOURCE[] liegen verschiedene
    HEX Werte als Bytes vor also 00, 0F, AA ect.

    nun möchte ich diese in einen CString kopieren aber es soll 1:1 sein.
    Also der CString soll 000FAA beinhalten.
    Dazu habe ich eine brutale Funktion geschrieben.

    void HEX2String(char SOURCE[1024] , int anzahl)
    {
    	anzahl=anzahl;
    	unsigned char temp1;
    	unsigned char temp2;
    	CString temp3;
    	CString temp4;
    	CString daten="";
    	for (int i=0;i<anzahl;i++)
    				{
    					temp3="";
    					temp4="";
    					temp1=SOURCE[i];
    					temp1=temp1>>4;
    					temp2=SOURCE[i];
    					temp2=temp2<<4;
    					temp2=temp2>>4;
    
    					if (temp1==0x0) temp3="0";
    					if (temp1==0x1) temp3="1";
    					if (temp1==0x2) temp3="2";
    					if (temp1==0x3) temp3="3";
    					if (temp1==0x4) temp3="4";
    					if (temp1==0x5) temp3="5";
    					if (temp1==0x6) temp3="6";
    					if (temp1==0x7) temp3="7";
    					if (temp1==0x8) temp3="8";
    					if (temp1==0x9) temp3="9";
    					if (temp1==0xa) temp3="A";
    					if (temp1==0xb) temp3="B";
    					if (temp1==0xc) temp3="C";
    					if (temp1==0xd) temp3="D";
    					if (temp1==0xe) temp3="E";
    					if (temp1==0xf) temp3="F";
    
    					if (temp2==0x0) temp4="0";
    					if (temp2==0x1) temp4="1";
    					if (temp2==0x2) temp4="2";
    					if (temp2==0x3) temp4="3";
    					if (temp2==0x4) temp4="4";
    					if (temp2==0x5) temp4="5";
    					if (temp2==0x6) temp4="6";
    					if (temp2==0x7) temp4="7";
    					if (temp2==0x8) temp4="8";
    					if (temp2==0x9) temp4="9";
    					if (temp2==0xa) temp4="A";
    					if (temp2==0xb) temp4="B";
    					if (temp2==0xc) temp4="C";
    					if (temp2==0xd) temp4="D";
    					if (temp2==0xe) temp4="E";
    					if (temp2==0xf) temp4="F";
    					daten=daten + temp3+temp4;
    				}
    	HEX_STRING=daten;
    }
    

    Kann man das nicht einfacher, schneller machen?
    Irgendwie klappen meine anderen Ansätze einfach net.

    Danke Holger



  • CString bietet doch die Funktionen Format und AppendFormat mit printf-Syntax. Damit kannst du deine Hex-Werte recht bequem hinzufügen. Beispiel:

    CString cs;
    int i1=0xa,i2=0xf;
    cs.AppendFormat("%X",i1);  //"%x" für Kleinschreibung
    cs.AppendFormat("%X",i2);
    cs.AppendFormat("%02X",i1);  //mit Nullen auf 2 Stellen auffüllen (0xa => "0a")
    

  • Mod

    static inline char ByteToHexA(int c)
    {
        return static_cast<char>((c<10) ? (c)+'0' : (c)-10+'A');		
    }
    
    void BinToString(PCVOID pData, PSTR pszOut, int iLen)
    {
        int	iPos=0;
        const BYTE *pszStr = (const BYTE *)pData;
        while (iLen--) 
        {
            BYTE byte = *pszStr++;
            BYTE uByte = (byte & 0xF0) >> 4,
                 lByte = byte & 0xF;
            pszOut[iPos++] = ByteToHexA(uByte);
            pszOut[iPos++] = ByteToHexA(lByte);
        }
        pszOut[iPos] = '\0';
    }
    

Anmelden zum Antworten