View previous topic :: View next topic |
Author |
Message |
Guest
|
string Hex into int |
Posted: Wed Nov 16, 2005 6:08 am |
|
|
Hi,
I'm trying to translate the following ASCII string: "RD 2B0 A3 58"
into: RD, 0x2b0, 0xA3, 0x58
What is the simples why to do it ? |
|
|
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Wed Nov 16, 2005 7:39 am |
|
|
I would write something like this: Code: | #define NUMBER_OF_VALUES 4
int i, k, value;
int16 values[NUMBER_OF_VALUES] = {0,0,0,0};
k = 0;
for (i = 0; i < length(ASCIIstring); i++)
{
if (i == ' ')
{
k++;
value = 0;
if (k == NUMBER_OF_VALUES)
{
i = 255;//to avoid array-overflow
}
}
else
{
value = ASCIIstring[i] - '0';
if (value > 9)
{
value = (value + '0') - 'A';
}
values[k] = (values[k] * 16) + value;
}
} |
this code might contain syntax errors and so you should check it first.
Off course, there are more ways to do it, and this is probably not the best... |
|
|
Guest
|
|
Posted: Thu Nov 17, 2005 2:01 am |
|
|
Thanks,
This example was waht I'm looking for .. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|