Need help to dissection a error code...

This is a discussion on Need help to dissection a error code... within the C++ Programming forums, part of the General Programming Boards category; Hey, Good day. two lines of my error code. Currently, when i debug to here. the "temp" value is 19. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    Need help to dissection a error code...

    Hey,

    Good day.

    two lines of my error code.
    Currently, when i debug to here. the "temp" value is 19.

    Could you give me some dissection about "%02X".
    How to modify it and then "temp" value could be set to 25.

    Any help will be appreciated.

    Code:
    
    		char temp[4] = {0};
    		sprintf(temp, "%02X ", 25);
    --------------------------------------------------------
    Newbie
    C/C#/C++
    swimming/cycling/running
    There is nothing more powerful than an idea.
    Except for an idea put into action.
    --------------------------------------------------------

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Could you give me some dissection about "%02X".
    %x represents hex values, which will give you 19.
    if you want to store the decimal value 25 you need to use %d, and the 02 is not required.
    Spidey out!

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Original function.
    Code:
    void Binary2HexString(const vector<unsigned char>& vec, string& result)
    {
    	if(vec.size() == 0)
    		return;
    
    	for(size_t i = 0; i < vec.size(); i++)
    	{
    		char temp[4] = {0};
    		sprintf(temp, "%02X ", vec[i]);
    		result.push_back(temp[0]);
    		result.push_back(temp[1]);
    		result.push_back(temp[2]);
    	}
    }

    Fixed function.
    Any good suggestion for this issue.
    I really don't like my fix like this hard-code!
    Code:
    void Binary2HexString(const vector<unsigned char>& vec, string& result)
    {
    	if(vec.size() == 0)
    		return;
    
    	for(size_t i = 0; i < vec.size(); i++)
    	{
    		char temp[4] = {0};
    		if (vec[i] < 10){
    			sprintf(temp, "%02X ", vec[i]);
    		}
    		// if the vec[i] value bigger than 10, we must take use Dec instead of Hex to handle.
    		else{	
    			sprintf(temp, "%02d ", vec[i]);
    		}
    		result.push_back(temp[0]);
    		result.push_back(temp[1]);
    		result.push_back(temp[2]);
    	}
    }
    --------------------------------------------------------
    Newbie
    C/C#/C++
    swimming/cycling/running
    There is nothing more powerful than an idea.
    Except for an idea put into action.
    --------------------------------------------------------

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, if you want hex, changing hex to dec is a really really really really really really really really really really really really really really bad idea.

    Also, given that a char is only going to have two hex digits in it, there's no need to put three characters in your string.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    >> I really don't like my fix like this hard-code!

    That doesn't make any sense. If your intent is to convert to hex then your original function was correct. And IMO, I wouldn't add the space padding. Or else name the function Binary2HexStringWithPadding or such (or at least make it a parameter).

    EDIT:
    Actually, concerning the padding I suppose it makes sense since you're not printing it as one large number.
    Last edited by Sebastiani; 07-30-2009 at 09:54 PM.



    ITSA
    Socket Library!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Sorry, i didn't report my problem clearly at the beginning. event i already posted twice.

    if you want hex....
    Yes. I really need hex to handle a lot of data.but i find the bug. that i can't convert "25口" to "25 " ....

    That doesn't make any sense....
    Sure. i also think so. :-)

    I attached the debuging screen copy pictures to tell more.

    Finially,

    I want to get the below format log.

    ------------------------------------

    < L [5]
    < U4 [/1] 1 >
    < A [/1] 'ProceedWithCarrier' >
    < A [/1] 'CID1' >
    < U1 [/1] 01 >
    < L [3]
    < L [2]
    < A [/1] 'Capacity' >
    < U1 [/1] 25 > // NOT 19 here
    >
    < L [2]
    < A [/1] 'SubstrateCount' >
    < U1 [/1] 25 > // NOT 19 here too.
    >
    < L [2]
    < A [/1] 'Usage' >
    < A [/1] 'PRODUCT' >
    >
    >
    >
    ------------------------------------

    and some time there are some log like these styles.

    < U1 [/1] 01 >
    ....
    < U1 [/1] 05 >
    ....

    That's why i must fix the bug to meet Value of 25.etc.

    Any guide. THANK YOU!!!
    :-)
    Attached Images Attached Images   
    Last edited by userpingz; 07-30-2009 at 11:07 PM. Reason: better style to report my problem
    --------------------------------------------------------
    Newbie
    C/C#/C++
    swimming/cycling/running
    There is nothing more powerful than an idea.
    Except for an idea put into action.
    --------------------------------------------------------

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay. So what you've got, then, is a single character with ASCII value 25. If you want it to print as 25, then don't print it as hex. The hex value of that very same ASCII character is 0x19.

    (In other words, the very idea of a "binaryToHex" function taking something with value 25 and printing anything other than "19" is just ludicrous. Don't use this function, if you don't want hex output.)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21