Friday 8 May 2015

Playing with Strings, HEX and Integer values.

In the past few days I have tough time converting Inter values into Hex values and how to store them in strings.
If once i stored them How to get their Hex representation Back..

first of all.. Int is a Data type where HEX is a Data representation
so if i wanted to convert Int To HEX and send the value through Serial Hardware
i can do something like this

int val = 100;
void setup(){
Serial.begin(9600);
Serial.print(val);
}
void loop(){
}


output:
3e8

ok HEX representation of 1000 is 3e8 and it is converted and send through serial hardware.
But what if we wanted to store that Equivalent Hex value into some variable or assign that value to another variable?

So we need a way to store that converted Hex value. but to which data type we need to store the HEX value?
like i said before HEX is not a datatype it is a data representation. even though it is holding a integer value we cannot assign it to the int datatype because of characters A to Z.

One possible solution is using strings.so we need to perform the conversion and store that value in string

hopefully there is a function call that will do the work for us it isutoa()
utoa --> unsigned int to String Base conversion
syntax : utoa((unsigned int) val,string,base)
where val is the val converted to Hex format
string is char string which holds output after conversion
base is radix for conversion 0,8,16

utoa()

Convert an unsigned integer into a string, using a given base

Synopsis:
#include

char* utoa( unsigned int value,
char* buffer,
int radix );
Arguments:
value
The value to convert into a string.
buffer
A buffer in which the function stores the string. The size of the buffer must be at least:
8 × sizeof( int ) + 1

bytes when converting values in base 2 (binary).

radix
The base to use when converting the number.



simple code to demonstarte this conversion is

int val = 1000;

void setup(){
char str[4];
utoa((unsigned int) val,str,16);
Serial.print("the val is : ");
Serial.println(val);
Serial.print("the HEX converted string is : ");
Serial.println(str);
}

void loop(){
}


output:
the val is : 1000
the HEX converted string is : 3e8

we commonly use 2 bytes or 4 bytes as HEX representation.. so if we wanted to represent 1000 in HEX 4 byte format we write something like 03e8. we add zero before MSB to make it 4 bytes But Arduino doesn't it do that automatically for us. so we somehow has to tell Arduino that we need to print fixed bytes representation because this will cause major problem if we wanted to send the hex value in the middle of string.

this is done using sprintf() function
sprinf() : string print function which is used to store the string in specified format

which dones same duty as printf but rather than printing to standard io it format the data and store it in string

syntax:sprintf(str,format,arguments)
where str is the string where formatted data is stored
format is used to specify the format
arguments is the arguments supplied to arguments

Here format is the key thing that do the job
that can be explained using the following c program

#include
int main() {
int data = 29;
printf("%x\n", data); // just print data
printf("%0x\n", data); // just print data ('0' on its own has no effect)
printf("%8x\n", data); // print in 8 width and pad with blank spaces
printf("%0.8x\n", data); // print in 8 width and pad with 0's
getch();

return 0;
}


and the result is
sprintf format

code to convert int to hex and store to the string with 4 bytes representation is

int a = 1000;
void setup(){
Serial.begin(9600);
char str[4];
sprintf(str,"%0.4x",a);
Serial.print("the HEX converted string is : ");
Serial.println("str");
}

void loop(){
}


those are used when we wanted to use functions like strcat() to concate strings

But what if we have a HEX string and wanted to get the int value back from the string. in this case atoi() won't help us because it only convert integer string not HEX string

in this code strtol() will be useful.
strtol --> string into long integer
convert a string into long integer according to the given Base.

syntax: strtol(str,end_ptr,base)
str : string which converted
end_ptr: end pointer
base : radix of values stored in string
it returns the long int value we need to cast to int if wanted to assign it to int variable
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.


char str[] = "03e8";
void setup(){
Serial.begin(9600);
int val = (int)strtol(str,NULL,16);
Serial.println(val);
}
void loop(){
}


Output:
1000

the functions do our bidding is:
utoa -- unsigned int to ascii string with base
utoa(int val, char* str, radix)

strtol -- string to long int
strtol(str,end_ptr,radix)

sprintf -- string printf
sprintf(string,format)

No comments:

Post a Comment