Converting serial characters into integers

Hi there,
I’m trying to read data from an arduino microprocessor which is coming in to my ubuntu computer via usb. I have written a simple program that seems to be working somewhat. I’m using the serialport ruby library and have managed to get information coming in on the terminal. my problem now is that the readings are in characters (possibly extended ascii). I’m wondering how I can convert them to integer.
here is my code: -

require ‘serialport’

ser = SerialPort.new("/dev/ttyUSB0", 9600, 8, 1, SerialPort::NONE)

loop do
puts ser.read(8)
end

and here is an screenshot of the characters: -


thank you very much

Are you looking for String#ord ? https://docs.ruby-lang.org/en/2.7.0/String.html#method-i-ord

Ruby doesn’t have a character class as such: characters are just strings of length 1.
.chr converts an integer to a character, and .ord converts a character to an integer.

2.7.0 :023 > "a".ord
 => 97
2.7.0 :024 > "abc".split(//).collect &:ord
 => [97, 98, 99]
2.7.0 :025 > 10.chr
 => "\n"

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs