![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 29
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Sep 2004
Posts: 29
Rep Power: 0
![]() |
Bah, I wrote ANSI, but I meant ASCII.
I want to change ASCII values to hex from a file. To darn many acronyms. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
#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;
}Sum of the file in hex 257 or 599 b0 de 0a a0 1f |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2004
Posts: 29
Rep Power: 0
![]() |
cool, thanks for the help.
|
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
0x10==16
__________________
PFO - My daily dose of technology. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|