Page 1 of 1

Convert Bin to ASCII

Posted: Thu Feb 01, 2018 11:29 am
by RoxxorXx
Hi, It's me again,
I search an solution to solve my problem. I need to convert somes vars (in byte) to ascii char and put these char into a string; have you a solution to do that ?

Re: Convert Bin to ASCII

Posted: Thu Feb 01, 2018 12:01 pm
by QMESAR
Hi,
You can look in the Flow Code wiki for help on such questions.

http://www.matrixtsl.com/wikiv7/index.p ... =Main_Page

If you Tipe in the search String functions you will see a few things that might help you
12.jpg
12.jpg (93.78 KiB) Viewed 11366 times
Then if you Search for Type casting and Number Conversions you will also see some valuable stuff
13.jpg
13.jpg (41.25 KiB) Viewed 11366 times
Hop this helps you

Re: Convert Bin to ASCII

Posted: Thu Feb 01, 2018 12:12 pm
by RoxxorXx
Hi,
Thanks for the answer but it's not clear for me, can you show me an exemple ?

Re: Convert Bin to ASCII

Posted: Thu Feb 01, 2018 12:20 pm
by RoxxorXx
I think you are not in the same ideo, i would like to convert for exemple 097 098 to a b

Re: Convert Bin to ASCII

Posted: Thu Feb 01, 2018 12:51 pm
by QMESAR
RoxxorXx wrote:I think you are not in the same ideo,
Well I am not sure what you mean by this!
i would like to convert for exemple 097 098 to a b
What you want to do is in the wiki which I gave you the link to any case in basic
the Binary value of 0b01000001 = 65 in decimal in both cases the compiler does not care if you give a bin or a dec number for the compiler it is a number and will automatically covert it with that said 0b01000001 = 65 when you use the ToStr$(number) you will have the A as you see 097 is not A in ASCII ,A is 0x41 in HEX
can you show me an exemple ?
may be you should show us what you want to do ? then people can help you better

Re: Convert Bin to ASCII

Posted: Thu Feb 01, 2018 2:10 pm
by Benj
Hello,

If you are trying to convert an ASCII string e.g. "097" into a numeric variable then you would do so like this.

Here is some basic pseudocode.

Code: Select all

index = 0
str = "097"
length = Length$(str)
number = 0
while (index < length)
{
 if (str[index] < '0' || str[index] > '9')  //Check next character is numeric
 {
   index = length     //bail out if not
 }
 else
 {
   number = number * 10
   number = number + (str[index] - '0')
   index = index + 1
 }
}
The variable number should now be equal to 97, if you call the LCD function with a PrintASCII then you will get an 'a'.

Re: Convert Bin to ASCII

Posted: Tue Feb 20, 2018 2:32 pm
by RoxxorXx
Hi, I'm back;
So I don't know i'f i'm understand; I would like to convert a number as an ascii char c: so if I have :

Code: Select all

Int data_number = 97; 
I would like to convert that to

Code: Select all

Char (String) data_string = "h";
so do a sort of

Code: Select all

Char (String) data_string = toAscii(data_number); //convert 97 to "h"
Have you a solution to do that ?

Thanks if you answer :D

Re: Convert Bin to ASCII

Posted: Tue Feb 20, 2018 6:10 pm
by Benj
Hello,

97 is equal to 'a' they are the same to the processor.

To make 97 equal to 'h' then simply add 7 to the value?

Re: Convert Bin to ASCII

Posted: Fri Feb 23, 2018 8:54 pm
by RoxxorXx
Oh yes, my bad it's a for 97 but this number is just an exemple.
So Can i convert the number into a char ?

Re: Convert Bin to ASCII

Posted: Mon Feb 26, 2018 9:45 am
by LeighM
Hi,
Characters are numbers, the number defines the character that the display device prints.
Have a look at this table
http://www.asciitable.com/

So

Code: Select all

my_string = "hi"
is the same as

Code: Select all

my_string[0] = 104;
my_string[1] = 105;
my_string[2] = 0;
strings end with a 0 (null)

Re: Convert Bin to ASCII

Posted: Tue Mar 20, 2018 4:40 pm
by RoxxorXx
I'm back and I have à big question : WHY
It's possible to use the ascii extend table ? because i would like encode data between 0 and 255. Have you a solution to do that ?

Re: Convert Bin to ASCII

Posted: Tue Mar 20, 2018 7:09 pm
by Benj
Hello,
I'm back and I have à big question : WHY
I think your going to have to give us more to go on? WHY what?
It's possible to use the ascii extend table ?
To do what?
because i would like encode data between 0 and 255.
to go into a file? to be sent over a serial connection? to be stored in a program? Can you not just use the numeric values 0 to 255?

Re: Convert Bin to ASCII

Posted: Wed Mar 21, 2018 7:30 pm
by RoxxorXx
So I have 2 modules one master and one slave. The communication between these module is create with zigbee. I have 6 data to transmit so between the modules its easy but after that I must transmit these data with the serial usb.

Re: Convert Bin to ASCII

Posted: Thu Mar 22, 2018 9:11 am
by LeighM
One possible solution would be to send the byte values as a pair of 2 hexadecimal characters,
take the most significant 4 bits (high nibble) and convert to a hexadecimal character,
take the least significant 4 bits (low nibble) and convert to a hexadecimal character.

Re: Convert Bin to ASCII

Posted: Thu Mar 22, 2018 10:07 am
by RoxxorXx
Have you an exeple to do that ? How can I check the seperation of the data ? I have a start byte and a end byte but between that if I have a 1 data ?

my data is :

start
U
I
state 1
state 2
id
stop

so it's can be 128,55,78,45,15,25,127, or 128,48,78,245,51,52,127

Re: Convert Bin to ASCII

Posted: Thu Mar 22, 2018 11:07 am
by LeighM
Right, now you are starting to give us more of the full picture :)
Does the data at the USB receiving end need to be machine or human readable?
The start (128) and stop (127) values seem to imply that the data bytes can never be those values, is that correct?
If the data is to be human readable you could simply convert the 128 to say a < character and 127 to > character and then use ToString to convert the byte data values to numeric strings, with a comma between. Just one idea. Hence you would see :
<55,78,45,15,25>

Re: Convert Bin to ASCII

Posted: Thu Mar 22, 2018 11:46 am
by RoxxorXx
Yes it's a posibility thks c: