Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 7th, 2005, 10:53 AM   #1
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
Converting ANSI characters to hex for Checksum.

Ok, I've been working with embedded C for too long and am too accustomed to using hexidecimal by default.

I'm trying to write a small piece of code that will read a .hex file and compute a 16 bit checksum of the contents. The problem I'm having is that I can't seem to figure out how to get C to read the characters in the file as hex numbers and not ANSI characters.

Ultimately I want the program to grab a pair of characters (aka one byte) and then add all of them together and display the result as a hex number.

Basically how can I tell the program that "01" = 16, "FF" = 255, etc. ?

Thanks in advance for the help.
JawaKing00 is offline   Reply With Quote
Old Sep 7th, 2005, 1:10 PM   #2
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
Bah, I wrote ANSI, but I meant ASCII.

I want to change ASCII values to hex from a file.

To darn many acronyms.
JawaKing00 is offline   Reply With Quote
Old Sep 7th, 2005, 1:57 PM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
    FILE *in=fopen(argv[1],"r");
    char tmp[128]={0x0};
    int i=0;
    int sum=0;
    
    while(fscanf(in,"%2x",&i)!=EOF)
    {
        sum+=i;
    }
    printf("Sum of the file in hex %x or %d\n",sum,sum);
    fclose(in);
    return 0;    
}
output:
Sum of the file in hex 257 or 599
contents of file:
 
b0
de
0a
a0
1f
jim mcnamara is offline   Reply With Quote
Old Sep 7th, 2005, 3:06 PM   #4
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
cool, thanks for the help.
JawaKing00 is offline   Reply With Quote
Old Sep 9th, 2005, 5:07 AM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by JawaKing00
Basically how can I tell the program that "01" = 16, "FF" = 255, etc. ?
BTW 0x01 != 16.
0x10==16
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC