Reputation: 39
For my project I need to read some single line text from a SD card, then get the hex or dec value of each character within the string and group those values in an array.
There are no whitespaces in the text and the lines end with \n
I'm using this code to read all the content into a single string!
String line = "";
while (dataFile.available() != 0)
{
line = dataFile.readStringUntil('\n');
if (line == "")
break;
}
For later use I need to calculate the hex values of each character, this code should iterate over the String and group it in an array.
int lineSize = line.length();
uint8_t data[lineSize];
for (int i = 0; i < lineSize; i++)
{
data[i] = line.charAt(i);
}
I really don't know wether this works or not, but I doubt that I will get the actual hex values...
The values are somewhere but I really don't know how to access them!
The result should look like this:
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
Upvotes: 0
Views: 1972
Reputation: 23226
Think of Hexadecimal as just another format to display any data type (uint8_t, or char or int...) stored in memory. In memory, its all binary, or hexadecimal. Just depends on how you want to look at it.
For example: the following statements:
long int A = 34;
uint8_t B = 34;
char C = 34;
int D = 34;
printf("0x%02x\n", 'A'); // surrounded with '' gives ASCII value of 65, then displayed in Hex
printf("0x%02x\n", A);
printf("0x%02x\n", B);
printf("0x%02x\n", C);
printf("0x%02x\n", D);
Results in:
Breaking any string into its fundamental elements, (char, or uint8_t) and printing them as shown above will yield similar results for you.
Edit:
For this input file (call it in.txt, in the executable directory):
lkjahldfkjghlskjhlskjhlakdjgglsjkahlkj4hl5k6jh67=83kjhlkjshdf8f7s698s7dfgbslfkjbg
And using this code:
int main(void)
{
FILE *fp;
char filename[]=".\\in.txt";
uint8_t c;
int length=0, i=0;
uint8_t *array;
//Get number of entries in file:
fp=fopen(filename, "r");
c= fgetc(fp);
while(c<255)
{
length++;
c= fgetc(fp);
}
fclose(fp);
//give array sufficient space
array = malloc(sizeof(uint8_t)*length);
fp=fopen(filename, "r");
//read file into array, and display as hexadecimal
c = fgetc(fp);
while(c<255)
{
array[i++]= c;
printf("0x%02x\n", c);
c = fgetc(fp);
}
fclose(fp);
getchar();//stop execution to view files (hit any key to exit)
return 0
}
You should see this output: (only first 20 or so values shown...)
Upvotes: 1
Reputation: 955
Well, if by getting the hex code you mean getting the hex representation of those characters, you can do this -
const String hexDigits = "0123456789abcdef";
char hex[2] = "";
hex[0] = hexDigits[ (int)line.at(i) / 16 ];
hex[1] = hexDigits[ (int)line.at(i) % 16 ];
For example, if line.at(i) = A
, then hex
will be "41"
.
Upvotes: 0
Reputation: 17258
Strings in C/C++ are already arrays (even when abstracted by a higher level class like std::string
). The array elements are the character values of each character. Are you sure you can't just grab the string's .c_str() (or .data()) and use that, possibly with a cast?
Upvotes: 0