How convert ASCII to char?

# Thread: How convert ASCII to char? – 876 days old

1. 0

## How convert ASCII to char?

Les, it would be nice if Proton PIC Basic will get the instruction 'ASC' (like other Basic compilers) to convert the ASCII value from a character into a BYTE value.
All other threads I've read here, none of them have a solution for this problem.

VAL is not the solution, because it only can convert the characters 0...9 and a...f

It works when I write directly:

DIM
Character AS BYTE
Character = "M"
PRINT Character

The value 77 will be displayed, indeed the ASCII code from character 'M'.
But I want to make an automatic routine for my application where I can put in a text and then do something like:

DIM
Text AS STRING * 15
DIM Character AS BYTE
DIM
x AS BYTE

Text = "Hello World"
FOR x = 1 TO 11
Character = MID\$(Text, x, 1)
PRINT AT 1, 1, DEC Character, " "
DELAYMS 1000
NEXT

The example above doesn't work.
It only gives '0' (zero) as result.
That's why we need the instruction ASC, to solve the problem like this:

Character = ASC(MID\$(Text, x, 1))

Or ís there another solution on this further great compiler (love to work with! )

(I know the example hereabove is a useless example but the example must be small to make the point)

Regards Frizie.
picbasic.nl

Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

MID\$ will return a string, ASC cannot be applied to a string only to a character. When applied to a string it can only return the first character.

A much simpler solution

Code:
```Text = "Hello World"
FOR x = 1 TO 11
Character = Text[x]
PRINT AT 1, 1, DEC Character, " "
DELAYMS 1000
NEXT```

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

Better For x = 0 to 10

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

An ASCII character is a byte value. ASCII "0" is the value 48, ASCII "1" is the value 49 etc... Whenever a standard byte variable is transmitted or displayed, and it holds a valid ASCII character, that's what will be displayed. Using the Dec modifier will never give an ASCII character because it will convert the byte value to its set of ASCII characters that make up the value.

An ASC procedure isn't required when using a compiler that actually has real byte variables.

For example:
Code:
```    Include "Amicus18.inc"

Declare LCD_DTPin = PORTB.4
Declare LCD_RSPin = PORTB.2
Declare LCD_ENPin = PORTB.3
Declare LCD_Interface = 4
Declare LCD_Lines = 2
Declare LCD_Type = Alphanumeric

Dim Text As String * 15
Dim Character As Byte
Dim Index As Byte

Text = "Hello World"
For Index = 0 To Len(Text) - 1
Character = Text[Index]
Print At  1 ,  1 , Dec Character, " "
Print At  2 , Index + 1, Character
DelayMS   100
Next```
The above program will re-create the string on line 2 of the LCD, and display each character's decimal value on line 1. Remember, a string is, essentially, a byte array but with a null terminator.

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

Thanks Les,

I'm sleepy ánd stupid today, because I could think of that solution too.
But in all my craziness, I was completely forgotten to look or even think on arrays.

I will use this in my application to display alphabetic characters on 7-segment LED displays -as far it is possible- but most of the alphabet characters will, only "k", "m", "v", "w", and "x" are hard to do.

@ JohnGB.
Indeed it is not possible what I wrote in my example (the result was also '0' (zero)).
With MID\$ I filter out the characters one by one, after that it should be converted from one string character to a BYTE.
Thats why I asked for an instruction as ASC, which would do the job, but the solution that Les has given, is also a working solution.

** Speaking about sleepy, I have the title wrong, it should be "How to convert ASCII to BYTE" or something...
Maybe someone can improve that?

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

It is also the solution I gave you

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

• 0

## Re: How convert ASCII to char?

My apologies John.

Even your solution I've completely overlooked.
I don't know what I had yesterday.
Was I blind? Drunk without a drop alcohol? I don't know.

But many thanks for your quick response and solution too John!

Frizie.

• Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues