You need to build a number in base10, considering you have all the digits stored in char*buf like so:
//assumtions: n - number of digits
// digits are ordered MSD first -> buff[0] contains the most significant digit.
for(i=0;i<n;i++)
{
number += buf[n-i-1]*pow(10,i); //number+=digit*10^i
}
printf("%x\n", number);
Answer from Pandrei on Stack OverflowYou need to build a number in base10, considering you have all the digits stored in char*buf like so:
//assumtions: n - number of digits
// digits are ordered MSD first -> buff[0] contains the most significant digit.
for(i=0;i<n;i++)
{
number += buf[n-i-1]*pow(10,i); //number+=digit*10^i
}
printf("%x\n", number);
you can use stdlib's atoi() function to convert a string to an integer:
char str[] = {0x32, 0x35, 0x34, 0x00};
int integer = atoi(str);
printf("%x\n", integer);
you can then printf() that integer as hex/dec or whatever. If this is too simple for your needs, then a more powerful alternative is to use sscanf()
If you are getting these integer strings from a comms buffer then you will likely want to split the buffer up into separate numbers first (perhaps they are separated by spaces for example). A simple way of doing this in C would be to use strtok().
EDIT: this edit is in response to the code that you have added to your question. I cannot comment on your question as I do not have enough reputation! The code you have added is broken. It will only work if a three digit string is provided, eg "001", "255". If a different number of digits are provided, it will read beyond the end of the string and produce garbage output, try "1" or "14", or "12314".
Here's a simplistic function to convert one character to a hexadecimal string.
char hexDigit(unsigned n)
{
if (n < 10) {
return n + '0';
} else {
return (n - 10) + 'A';
}
}
void charToHex(char c, char hex[3])
{
hex[0] = hexDigit(c / 0x10);
hex[1] = hexDigit(c % 0x10);
hex[2] = '\0';
}
Its pretty easy. Scan through character by character ... best to start from the end. If the character is a number between 0 and 9 or a letter between a and f then place it in the correct position by left shifting it by the number of digits you've found so far.
For converting to a string then you do similar but first you mask and right shift the values. You then convert them to the character and place them in the string.
c - Hex to ascii string conversion - Stack Overflow
Convert the Ascii to Hex in C | Go4Expert
hex - Convert ascii char[] to hexadecimal char[] in C - Stack Overflow
Simple Ascii to Hex Conversion -
you need to take 2 (hex) chars at the same time... then calculate the int value and after that make the char conversion like...
char d = (char)intValue;
do this for every 2chars in the hex string
this works if the string chars are only 0-9A-F:
#include <stdio.h>
#include <string.h>
int hex_to_int(char c){
int first = c / 16 - 3;
int second = c % 16;
int result = first*10 + second;
if(result > 9) result--;
return result;
}
int hex_to_ascii(char c, char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
int main(){
const char* st = "48656C6C6F3B";
int length = strlen(st);
int i;
char buf = 0;
for(i = 0; i < length; i++){
if(i % 2 != 0){
printf("%c", hex_to_ascii(buf, st[i]));
}else{
buf = st[i];
}
}
}
Few characters like alphabets i-o couldn't be converted into respective ASCII chars . like in string '6631653064316f30723161' corresponds to fedora . but it gives fedra
Just modify hex_to_int() function a little and it will work for all characters. modified function is
int hex_to_int(char c)
{
if (c >= 97)
c = c - 32;
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if (result > 9) result--;
return result;
}
Now try it will work for all characters.
#include <stdio.h>
#include <string.h>
int main(void){
char word[17], outword[33];//17:16+1, 33:16*2+1
int i, len;
printf("Intro word:");
fgets(word, sizeof(word), stdin);
len = strlen(word);
if(word[len-1]=='\n')
word[--len] = '\0';
for(i = 0; i<len; i++){
sprintf(outword+i*2, "%02X", word[i]);
}
printf("%s\n", outword);
return 0;
}
replace this
printf("%c",word[i]);
by
printf("%02X",word[i]);
I am performing a checksum check on GPS packets.
Take the following GPS packets as an example,
"$GPGGA,002153.000,3342.6618,N,11751.3858,W,1,10,1.2,27.0,M,-34.2,M,,0000*5E\r\n"
Now to make sure the packet is valid; I need to compute the checksum of the packet and compare it with the checksum value already present in the incoming packet.
The problem I am facing is that the computed checksum (0x5E) has the type uint8_t and the checksum already present in the packet has the type of char. '5' has a char type and 'E' has a char.
If I typecast the char '5' with uint8_t I get '0x35'.
What is the simplest solution to this problem?