Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

ascii to number

Hi,

 

I am having trouble to get the correct integer values from the serial read string that i receive. E.g. If I send 093 (type in hype terminal) I get 48,57,51 as array elements. I have used the string to byte array function to convert the string I read from the serial port. The values are the ascii equivalents. How can I get 0,9,3 back? I need these as the program further performs a length and crc check which go wrong as  0 is now taken as 48.

 

Tthe serial port is reading from a gamma meter. The gamma meter sends its data in a kind of BCD format (where the each byte is 0-255).

 

E.g. The packet received has the length field of the data which is 2 bytes long (byte 2 and 3). For me to get the value of the length I have to do the following

 

length = data(2)*256 + data(3)  -- where data is the array of the data read from the serial port.

 

Using the string to byte array gives the ascii equivalents. Therefore if the value received was 5 then labview would give me 53 and 53*256 would be completely off.

 

Hope this clarifies. The overall scenario is that i am trying to convert matlab code into labview so that the gamma meter could be used with the clients system which runs labview.

I have also attached the matlab file as a txt file but you can change the extension to a .m file.

 

regards

Harshil 

0 Kudos
Message 1 of 4
(3,834 Views)
If you are dealing strictly with integers you could just subtract 48 after converting to a byte array?
0 Kudos
Message 2 of 4
(3,828 Views)

Hi,

 

Thanks for the reply,

 

I did think of that but what stopped me was i would end up having different digits which i shall have to join to make a number. e.g if the length is 2 bytes long representing the number 12, then I would hhave to join the 1 and 2 together to get the right length. Is there any way to do it directly? 

 

regards

Harshil

0 Kudos
Message 3 of 4
(3,826 Views)
It is not the most elegant solution but you could do something like the attachement shows.  End up splitting the ASCII string to parse your message header then using scan from string to convert the value to a U8
0 Kudos
Message 4 of 4
(3,822 Views)