CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Apr 2008
    Posts
    6

    Problem hex string to char * ..

    I want to transform hex string I type in Edit Box (MFC) to a char *

    like...

    aa bb cc dd ee ff 03 f8 d3 fe

    typed that in Edit Box and when I press convert button

    It should make that into char array well yah char * same thing.

    This is what I got so far but for some reason yes it compiles great but when I press convert it crashes

    char strData[255] = {0};//held from Edit Box.
    char * intercept = NULL;



    LRESULT res;

    res = SendMessage(GetDlgItem(hDlg,IDC_EDIT),WM_GETTEXTLENGTH,0,0);
    if(((int)res == 0 || (int)res > 999))// we want text but not bigger than limit.
    {
    return 0;
    }
    SendMessage(GetDlgItem(hDlg,IDC_EDIT),WM_GETTEXT,sizeof(strData),(LPARAM)strData);

    char* temp=strdup(strData);

    //Convert a hexyString to a hex character and put in array
    for(int i = 0; i < res; i++)
    intercept[i] = strtoul(temp, &temp, 0x10);
    delete[] temp;
    temp = 0;




    yah.. thats about it

    well I know I ain't that smart to figure this out so if anyone could help I'd really like that
    Last edited by Fuji22; April 15th, 2008 at 11:10 PM.

  2. #2
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: Problem hex string to char * ..

    Will this FAQ Integer to string and vice-verca for any base be of any help?
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problem hex string to char * ..

    'intercept[i]' looks like you are dereferencing a NULL pointer.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Problem hex string to char * ..

    You are using MFC, so why are you messing around with SendMessage, GetDlgItem and char pointers ?

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem hex string to char * ..

    Good point Skizmo! You can always easily use DDE by creating your dialogs with the classwizard. Or alternatively you can "cheat" by:
    Code:
             char ctxt[8]="Hello\0";
             CString svtxt;
             GetDlgItem(IDC_EDIT1)->SetWindowText(ctxt);
    	 GetDlgItem(IDC_EDIT1)->GetWindowText(svtxt);
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Apr 2008
    Posts
    6

    Re: Problem hex string to char * ..

    nope that Integer to string and vice-verca for any base had no help what so ever..

    anyways yah its not really MFC im just using Resource editor from Visual C++ 6.0 but its really Win32 dialogs but thats not the problem

  7. #7
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem hex string to char * ..

    I want to transform hex string I type in Edit Box (MFC) to a char *
    Oh thanks for letting us know in advance!
    ahoodin
    To keep the plot moving, that's why.

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem hex string to char * ..

    Can I ask what products you are using to accomplish your goals?
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Apr 2008
    Posts
    6

    Re: Problem hex string to char * ..

    I also got this done by myself a little problem with arrays now.. If I'd make a array like 5000 (guess size) then at the end It must be the same size as the hex value of ascii string. But okay here is my current code.
    Code:
    					res = SendMessage(GetDlgItem(hDlg,IDC_EDIT),WM_GETTEXTLENGTH,0,0);
    					if(((int)res == 0 || (int)res > 9999))// we want text but not bigger than limit.
    					{
    						return 0;
    					}
    				//	SendMessage(GetDlgItem(hDlg,IDC_EDIT),WM_GETTEXT,sizeof(strData),(LPARAM)strData);
    					SendMessage(GetDlgItem(hDlg,IDC_EDIT),WM_GETTEXT,res,(LPARAM)strData);
    
    					for(int ac = 0; ac <= sizeof(strData); ac++)
    						printf("strData [%d] = %d\n", ac, strData[ac]);
    
    					printf("\n");
    
    
    
    					char temp[(res/2)+sizeof(char)] = {0}; //allocate temp array <- the hell this giving me a array for error C2466: cannot allocate an array of constant size 0
    
    					char *p = strtok(strData, " ");  //split hex characters by space
    					int curPos = 0;
    
    					while(p != NULL)  //if pointer is not NULL
    					{ 
    					  temp[curPos]=(char)strtoul(p, NULL, 0x10); 
    					  p = strtok(NULL, " "); 
    					  curPos++;
    					} 
    
    
    //					char* temp=strdup(strData);
    
    
    					for(int ab = 0; ab <= (res / 2)-1; ab++)
    						printf("temp [%d] = %d\n", ab, temp[ab]);
    
    					printf("\n");
    Last edited by Fuji22; April 16th, 2008 at 01:43 PM.

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem hex string to char * ..

    Please use code tags when posting. Edit your post. Right above the words LastPost in your editor there is a white button marked code tags. Select all the text in your post that is code and click "Code" please.

    Lack of Code Tags leads to practically un-readable and ugly postings.

    Thank you.

    Also Which OS/Windows version? Compiler? etc? Thanks.
    Last edited by ahoodin; April 16th, 2008 at 12:19 PM.
    ahoodin
    To keep the plot moving, that's why.

  11. #11
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problem hex string to char * ..

    USE CODE TAGS!

    Quote Originally Posted by Fuji22
    Code:
    	char temp[(res/2)+sizeof(char)] = {0}; //allocate temp array <- the hell this giving me a array for error C2466: cannot allocate an array of constant size 0
    You cannot declare an array that way. The array size must be constant at compile time. res is a variable. If the size varies, you'll need to dynamically allocate the array at runtime.

  12. #12
    Join Date
    Apr 2008
    Posts
    6

    Re: Problem hex string to char * ..

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0

    for

    const int size = (res/2)+1;
    char temp[size] = {0}; //allocate temp array

    I don't believe the compiler is this stupid.

  13. #13
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem hex string to char * ..

    Ou Contraire:

    Code:
    char temp[size] = {0}; //allocate temp array
    Should be:

    Code:
    char* ptemp = new char[size]; //allocate temp array
    
    //after you use it
    delete [] ptemp;
    now to access an element dereference by:

    Code:
    *(ptemp+ addr) = 'a';
    or

    Code:
    ptemp[addr] = 'a';
    You have alot of learning to do my young Padwan. And apparently the compiler is all over it. Whichever compiler you are using.
    Last edited by ahoodin; April 16th, 2008 at 02:47 PM.
    ahoodin
    To keep the plot moving, that's why.

  14. #14
    Join Date
    Apr 2008
    Posts
    6

    Re: Problem hex string to char * ..

    but why must I use pointers for such similar operations. I already mastered Java but I can't find the power in java aka dll injection so thats why I switched to C.

    if char blahlbah[30] works I dont get it.. aint pointers for functions and such so it will be less overhead on functions

    so Dynamic Allocation of arrays can't be done without pointers? wow.. why doesn't someone just make a new langauge thats just as powerful but without the hassle lol.
    Last edited by Fuji22; April 16th, 2008 at 04:26 PM.

  15. #15
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Problem hex string to char * ..

    Quote Originally Posted by Fuji22
    but why must I use pointers for such similar operations. I already mastered Java but I can't find the power in java aka dll injection so thats why I switched to C.

    if char blahlbah[30] works I dont get it.. aint pointers for functions and such so it will be less overhead on functions
    The compiler allocates space for that array at compile time. The compiler knows what 30 means. It means allocate 30 units for this thing; save it in the executable. If you have a variable, the compiler has no idea what it will be when the program runs later after it is compiled.

    so Dynamic Allocation of arrays can't be done without pointers? wow.. why doesn't someone just make a new langauge thats just as powerful but without the hassle lol.
    Pretty much every language since C was an attempt to do just that.

Page 1 of 2 12 LastLast

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