# Compare INT and Hex

#### FroceMaster

Joined Jan 28, 2012
683
Hi.
Have this peace who is bugging me,

if ((first_BT_data==UART_Buffer0) || (!RC1IF))

As i asume, first_BT_data is integer,, and need to compare ti UART_Buffer0 that is in HEX, recieved from serial data.
first_BT_data is normal from 0 to 9 and UART_buffer0 is from 0x00 to 0x09 and never higher,

But 5 is not equal to 0x05
How to fix ?

#### MrChips

Joined Oct 2, 2009
27,715
Hi.
Have this peace who is bugging me,

if ((first_BT_data==UART_Buffer0) || (!RC1IF))

As i asume, first_BT_data is integer,, and need to compare ti UART_Buffer0 that is in HEX, recieved from serial data.
first_BT_data is normal from 0 to 9 and UART_buffer0 is from 0x00 to 0x09 and never higher,

But 5 is not equal to 0x05
How to fix ?
This is a common misunderstanting.

Integer 5 and hexadecimal 0x05 are the same where the computer is concerned.
Binary, hexadecimal, decimal, etc. are different representations of numerical values that humans have adopted to represent the same value to suit the application. In the computer, all numbers are zeros and ones.

#### MrChips

Joined Oct 2, 2009
27,715
ASCII = American Standard Code for Information Interchange

is a long established standard for transmitting letters, numbers, punctuation marks and control commands.
It has been extended to also represent graphical symbols and other things.

ASCII is binary.

The graphic symbol for [5] is represented by 00110101 in 8-bit binary.
If you want to convert this to 00000101 you need to remove the 00110000 part.
This is commonly achieved by subtracting 00110000 from the received UART data.

In other words (UART_data - 48) will revert to original numeric values of the numerals 0-9.

#### djsfantasi

Joined Apr 11, 2010
8,682
But 5 is not equal to 0x05
How to fix ?
How do you know that 5 is not equal to 0x05? I ask, because it is… they appear to be different because of the way they are displayed. But mathematically, they represent the same value. As does each of these: five, 5, 0x05, 00000101b, etc!

So it appears as if there is nothing to fix!

#### FroceMaster

Joined Jan 28, 2012
683
i know, i must have been focused on it, but the error is somewhere else,

#### djsfantasi

Joined Apr 11, 2010
8,682
i know, i must have been focused on it, but the error is somewhere else,
So where (and what) is the error?

We really need more information. What is the code containing the two values you are comparing? What is telling you that there IS an error?

#### MrSalts

Joined Apr 2, 2020
2,625
The only reason a 5 does not equal 0x05 is if it is really a "5" (char) So I assume your uart vahex is ascii and your 5 is a string,
If you transferred a 5 from another device, and converted to string, it was converted to an ascii 0x53.

You can either...
a) int newInt = UART_Buffer0 - 48;

#### FroceMaster

Joined Jan 28, 2012
683
Ok, here is more of what i am working on
Code:
void __interrupt() myISR(void)
{
if (RC1IF==1) // indata from BT.
{
RC4=!RC4; // test only
while(RC1IF==1)
{
if(first_BT_data==0) // first part of data
{
first_BT_data++; // add 1 to counting

}
else // if not first data.
{
if (first_BT_data==0x01)UART_Buffer1=RC1REG;
if (first_BT_data==0x02)UART_Buffer2=RC1REG;
if (first_BT_data==0x03)UART_Buffer3=RC1REG;
if (first_BT_data==0x04)UART_Buffer4=RC1REG;
if (first_BT_data==0x05)UART_Buffer5=RC1REG;
if (first_BT_data==0x06)UART_Buffer6=RC1REG;
if (first_BT_data==0x07)UART_Buffer7=RC1REG;
if (first_BT_data==0x08)UART_Buffer8=RC1REG;
if (first_BT_data==0x09)UART_Buffer9=RC1REG;
first_BT_data++; // add 1 to counting
}

} // end while in recieve buffer

RC1STAbits.SPEN=0;// clearing errors
first_BT_data=0; // so we are stopping reading DATA
bt_data_income=1; // setting flag.
RC1STAbits.SPEN=1; // clearing error

} // end RC1 interrupt BT Ergo
}
I send from mobilephone over BlueTooth,
ex. 05-00-11-55-FF

Meaning 05= 5 parts of DATA
00 = First part
11 = Next part,
55 = Next part
FF = Last part.

Sometime i will send just 3 or 8 depending of what the MCU should do .

#### MrChips

Joined Oct 2, 2009
27,715
Ok, here is more of what i am working on
Code:
void __interrupt() myISR(void)
{
if (RC1IF==1) // indata from BT.
{
RC4=!RC4; // test only
while(RC1IF==1)
{
if(first_BT_data==0) // first part of data
{
first_BT_data++; // add 1 to counting

}
else // if not first data.
{
if (first_BT_data==0x01)UART_Buffer1=RC1REG;
if (first_BT_data==0x02)UART_Buffer2=RC1REG;
if (first_BT_data==0x03)UART_Buffer3=RC1REG;
if (first_BT_data==0x04)UART_Buffer4=RC1REG;
if (first_BT_data==0x05)UART_Buffer5=RC1REG;
if (first_BT_data==0x06)UART_Buffer6=RC1REG;
if (first_BT_data==0x07)UART_Buffer7=RC1REG;
if (first_BT_data==0x08)UART_Buffer8=RC1REG;
if (first_BT_data==0x09)UART_Buffer9=RC1REG;
first_BT_data++; // add 1 to counting
}

} // end while in recieve buffer

RC1STAbits.SPEN=0;// clearing errors
first_BT_data=0; // so we are stopping reading DATA
bt_data_income=1; // setting flag.
RC1STAbits.SPEN=1; // clearing error

} // end RC1 interrupt BT Ergo
}
I send from mobilephone over BlueTooth,
ex. 05-00-11-55-FF

Meaning 05= 5 parts of DATA
00 = First part
11 = Next part,
55 = Next part
FF = Last part.

Sometime i will send just 3 or 8 depending of what the MCU should do .
You need to determine how 05-00-11-55-FF is sent from mobile phone ove BlueTooth, in other words you need to find out how the data is encoded.

One way to find out is to examine the actual binary data that is received by the UART.
If the data is encoded in ASCII then here is what is received:
[0] = 48 = 00110000
[5] = 53 = 00110101
[-] = 45 = 00101101
[F] = 70 = 01000110

You can find this by looking at the ASCII table.

#### FroceMaster

Joined Jan 28, 2012
683
The data is sent in hex, have figured it out now. Thanx.modified the routine a bit and now i works.

#### MrChips

Joined Oct 2, 2009
27,715
The data is sent in hex, have figured it out now. Thanx.modified the routine a bit and now i works.
No. This could be very confusing to new comers.

Data is sent as 8-bit binary. This is certain (without getting into the discussion of 7, 8, 9 bits, parity, start and stop bits).

The binary code is a coded representation of what is actually meant to convey. For example the property sent could be a colour or a portion of a 64-bit signed integer, for example.

If the data is sent in ASCII hexadecimal notation, this is one method of encoding binary data. There is an infinite number of ways to encode data.
For example, suppose we wanted to transmit 1111-0101, we can send this as two ASCII characters [F][5].
The bytes transmitted for [F][5] would be 70 followed by 53, or it could be 53 followed by 70, depending on the order of transmission that you choose (often referred to as big endian vs little endian).

#### djsfantasi

Joined Apr 11, 2010
8,682
The data is sent in hex, have figured it out now. Thanx.modified the routine a bit and now i works.
I’m glad you figured it out. But your statement shows that you are still confused. ALL DATA IS “SENT” IN “. “HEX”. Hex is merely the name of one way of looking at data. It’s the same as binary, just looking at 4 bit chunks
of the binary bits of data. Here’s a table of VALUES showing equivalent hex, binary and decimal

0x0 = 0000b = 0 decimal
0x1 = 0001b = 1 decimal
0x2 = 0010b = 2 decimal

0x9 = 1001b = 9 decimal
0xA = 1010b = 10 decimal
0xB = 1011b = 11decimal

0xF= 1111b = 15 decimal

Sometimes the values are mapped to something else. Like characters…

An ASCII character “4” is represented by the decimal value 52, or the hex value 0x34 or the binary value 00110100. Again, the character “4” is represented by 0x34 = 00110100 = 52 decimal!

Edit: somehow I used a 2 in a binary value
Edit: fixed some typo errors