Convert Hex to bin?

Denzel

Senior Member
The bluetooth module I'm currently working with seems to convert binary to hex when I use serrout with it;

I.e. sending 255 is received at the pc terminal side as 323535 ('2' being 32, 5 being 35 ASCII)

How can I convert it back programatically? I am trying to send values between 0-255 to the hex terminal I'm using, which doesn't seem to have any Rx settings. I later want to use VB or Python to interpret the input so simply finding another terminal that can convert it won't cut it.

The Picaxe 18X code I am using is as follows:

Main:

setfreq M8

Serin 6,T9600_8,b1
Sertxd (#b1)
Serout 7,T9600_8,(#b1)

goto main


The 'Sertxd' command is recieved by the picaxe terminal as 255, just the serrout thats the issue.
Is there something I can do here?
 

inglewoodpete

Senior Member
Receiving 255 is fairly easy: 6 characters, of which characters 2, 4 and 6 are the binary values of the 3 digits. Eg Result = (Ch2 x 100) + (Ch4 x 10) + Ch6

My question is: How are values < 100 and values < 10 sent? As 303939 for 99 and 303038 for 8? This will determine how hard your task is.
 
Last edited:

westaust55

Moderator
I am not sure that I fully understand the question, but can offer the following comments:
1. SEROUT . . . . (b0) will send the binary value in the range %00000000 to %1111111 (ie decimal 0 to 255). Can your PC program accept the number directly like this ?

2. SEROUT . . . . (#b0) will send an ASCII character string to the PC with one character per digit of the decimal number. So a value of 123 is send as the characters &#8220;1&#8221;, &#8220;2&#8221; and &#8220;3&#8221; as you are finding.
Where you are receivig "values such as the quoted "323535" is this received as six separate values 3, 2, 3, 5 3, 5 or as three values 32, 35, 35 ?


3. If you are receiving the three ASCII characters then to recombine at the PC into a single value you need to:
(a) Subtract 48 ($30) from each value
(b) Recreate the value as 100 * first value + 10* second value + third value.
 

Denzel

Senior Member
Thanks for the info,

I am getting the numbers by themselves e.g. 3,2,3,5,2,5. I am a little bit perplexed, I tried to disregard the Hex terminal and use VB.net and i'm simply getting ?? or ? now on the input buffer. I think this question is more appropriate for a VB forum, I just wanted to ensure that I wasn't taking the wrong approach to transmitting data.
 

inglewoodpete

Senior Member
The following code is what I use in VB 2008, to receive serial data (from a PICAXE).

Code:
    Dim bRXByte As Byte
    Dim RXArray(2047) As Char
    Dim I As Integer = 0
    '
    Do
       bRXByte = SerialPort.ReadByte
       RXArray(I) = Chr(bRXByte)
       I = I + 1
    Loop Until (SerialPort.BytesToRead = 0)
 

MartinM57

Moderator
Still confused :(

in post #1 you received three bytes representing 255 (32, 35, 35, being the ASCII codes for 2, 5 and 5) and now you are saying you are getting 3,2,3,5,2(? - should be 3?),5 - so six bytes?

How many bytes will the bluetooth module send when the transmitted value represents:
- 0-9
- 10-99
- 100-255?

As always, a link to the datasheet will allow people to understand, and assist, further.
 

hippy

Technical Support
Staff member
I am getting the numbers by themselves e.g. 3,2,3,5,2,5.
It's almost certain that you are seeing six numbers but only three bytes are sent as described by westaust55; it's the "hex terminal" displaying each byte received as two digits. SEROUT and SERTXD will be sending the same output.
 

Denzel

Senior Member
OK, first of all, sorry for being so vague, the truth is I can't really explain whats wrong because i'm not entirely sure what I am after.

Update:

I have been playing around with this source:

http://home.comcast.net/~hardandsoftware/DesktopSerialIO.htm

which is a fantastic tool.

With your help I am now receiving values as 3 Hex values and I am able to convert this into a standard decimal I can work with. So thank you.

One quick question,

No matter if I send 003 or 3 or 10000 etc, repeatedly and not in any order I get the information split up like so:

Tx:
b1 = 133
Serout 7,T9600_8,(#b1)

Rx comes out as:

133
133
1
33
133
133
133
13
3
133
1
33
133
133
133
133
133

etc etc, do you know what could cause this?
 

lbenson

Senior Member
I remain uncertain what the hardware links are in all of this.

A diagram would help, with actual device models/description.

What???-> bluetooth sender >> bluetooth receiver>PC?

What is doing the displaying on the PC side? What to you think is converting ascii characters from the picaxe into hex (e.g., "32" as the hex representation of ascii "2")?
 

westaust55

Moderator
What is doing the displaying on the PC side? What to you think is converting ascii characters from the picaxe into hex (e.g., "32" as the hex representation of ascii "2")?
The use of the hash (#) symbol in front of the variables in the
Serout and the Sertxd commands as per the program example in post 1 (and others) is the reason for ASCII characater values being sent to the PC
 

lbenson

Senior Member
@westaust55--yes, I understood about the ascii, my question was about what was presenting that as hex.
 
Top