Jump to content

Logic error in C hex parse program

- - - - -

  • Please log in to reply
4 replies to this topic

#1
indigo

indigo

    Newbie

  • Members
  • Pip
  • 3 posts
I just wrote this extremely simple C program to parse pairs of characters from hex form to decimal form, but I am getting a logic error, resulting in a much larger value than expected. I went over and over the data types, since I have the feeling that somewhere a data type conversion is throwing the result off, but I haven't been able to spot it yet.

When the program runs you can simply enter two characters representative of a hexadecimal byte to see what it spits out. The intent is to eventually parse short strings of hex characters (thus the size of the array), but one thing at a time.

I know, I'm missing documentation, but it's a simple program (I'm still a novice) and it's being designed strictly as a tool for my personal use (and an exercise) so I didn't feel the need for it in this instance. My apologies if that makes the code significantly harder to read.

I've attached the code. Any help for a C newbie would be greatly appreciated. Thanks!

Attached Files



#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
At first sight there are two problems:

  • for loop does not update the counter variable

    for ( i = 0; i < 400; i+2 )
    The correct way to update the variable i is:

    for ( i = 0; i < 400; i = i+2 )
    You need to assign the result of i+2 to somewhere or it is simply lost.


  • the case when the parsed hex digit is a decimal number is not correct

    The function hexparse takes a character. A character is a number that represents the ASCII code of a letter, digit or sign. The character '0' is not the same than the number 0. Character '0' has the ASCII code 48, so if you simply convert the char to an int, you get a bad result.

    The correct way to convert a decimal digit from character to number is:

    y = x - '0'
    This way you substract the ASCII code of '0' from the digit you have. Example: if x = '4', its ASCII code is 52, so '4' - '0' is the same than 52 - 48 = 4, that is what you want.
It would also be recomendable to add some verifications to avoid errors. For example the user could write 'GG', that is an invalid hex number.

#3
indigo

indigo

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks. The program runs like a charm now. Thanks for the tip about why it was infinite looping, too!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you truly want to use scanf, but avoid overflow on your ciphertext[400], you must use this:
scanf("%400s", ciphertext);

Although I would recommend fgets():
ReturnCode=fgets(ciphertext, sizeof(ciphertext), stdin);

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
indigo

indigo

    Newbie

  • Members
  • Pip
  • 3 posts
Good eye for the overflow trap. I wasn't as concerned about it since I picked 400 to be vastly greater than any number of characters I'd be parsing, but better to get into good habits now.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users