0

I spent some considerable time on the below example. I will just writing a sample code and noticed it was never giving me the desired output.

Finally it worked but I am not able to explain as to why it happened. The commented lines are those which had the issue. Can someone tell me what I am missing here ?

#include<stdio.h>
#include<string.h>

void Ascii2Hex( char* originalByte, unsigned char *ConvertedByte )
{
        while( *originalByte )
        {
                //if( *originalByte >= 48 && *originalByte <= 57 ) 
                if( *(originalByte) >= 48 && *(originalByte) <= 57 )
                        *ConvertedByte = *originalByte - 48;
                //else if( *(originalByte) >= 65 && *(originalByte) <= 70 ) 
                else if( *originalByte >= 65 && *originalByte <= 70 )
                        *ConvertedByte = *originalByte - 55;
                //else if( *originalByte >= 97 && *originalByte <= 102 ) 
                else if( *(originalByte) >= 97 && *(originalByte) <= 102 )
                        *ConvertedByte = *originalByte - 87;
                else
                {       printf( "Not a valid alpha numeric char[%d][%c][%x]\n", *originalByte, *originalByte, *originalByte );
                        ConvertedByte--;
                }
                ConvertedByte++;
                originalByte++;
        }
}

void main()
{
        char *OrigStr = "0123G456A";    
        unsigned char Result[100] = {0};
        int i;

        for( i=0; i<strlen( OrigStr ); i++ )
                printf(" Orig[%02x][%c]\n", OrigStr[i], OrigStr[i] );
        Ascii2Hex( OrigStr, Result );
        for( i=0; i<strlen( OrigStr ); i++ )
                printf(" Result[%02X]\n", Result[i] );
}
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
Adarsh Nanu
  • 2,133
  • 1
  • 13
  • 18
  • 4
    *Please* read this: ["What is a magic number and why is it bad?"](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). Regarding telling you what you're missing, you told us you're not getting "the desired output", yet oddly neglected to divulge what that output *is*, nor did you say what you're *actually* getting. – WhozCraig Aug 27 '16 at 07:00
  • @WhozCraig went through this. but could not get any relation with my question – Adarsh Nanu Aug 27 '16 at 07:03
  • 1
    Seriously? You see no "magic numbers" on those lines? Okay then ... So about that expected and actual output. You said, "Finally it worked." So is it working *now*? – WhozCraig Aug 27 '16 at 07:08
  • output desired is like char 1 to 0x01, 'A' to 0x0A. I have already got the desired output. but can't explain why the commented lines did not work – Adarsh Nanu Aug 27 '16 at 07:12
  • 3
    Your two codes show exactly the same behaviour. Don't you have other lines changed when it has the issue? – OOPer Aug 27 '16 at 07:13
  • @OOPer no this is the only place where I changed. and that's what confused me too. may I ask you to give it a try at your end. – Adarsh Nanu Aug 27 '16 at 07:15
  • 1
    If those are the only changes you made, then it must be a bug in the compiler, because those parentheses make no difference – Drew McGowen Aug 27 '16 at 07:16
  • @WhozCraig yes. it is working. the line with issue is commented. line exactly below each line is what changed and giving expected output – Adarsh Nanu Aug 27 '16 at 07:18
  • 1
    @libadarsh.so.1.0.1 both sets give me *identical* output, in concurrence with OOPer and Drew. – WhozCraig Aug 27 '16 at 07:19
  • 1
    Does the commented out version of your code causes the same issue still now? – OOPer Aug 27 '16 at 07:21
  • @WhozCraig you are right. looks like in the course of fixing I made some other change also which is not present in the code I posted. let me try to simulate this again. – Adarsh Nanu Aug 27 '16 at 07:29
  • 1
    @libadarsh.so.1.0.1 Regardless, make sure to do what I described at the top of this thing. If you're not getting what you're expecting, you can't just ask "why" ? We have to *know* what you're expecting, what you're *actually* getting, and what steps you've already taken to identify the potential problems. And *all of that* should be *in your question*. – WhozCraig Aug 27 '16 at 07:32
  • OT: `void main()` should be `int main(void)`. – alk Aug 27 '16 at 16:13

1 Answers1

0

You are decoding each hexadecimal digit into a byte. Bytes of eight bits are represented by two hexdigits.

Regarding the magic numbers: What is 48 or 65 standing for? Not everyone remembers the ASCII codes. Just write '0' or 'A'.

user5329483
  • 1,260
  • 7
  • 11