News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Character to ascii value procedure.

Started by david, Apr 19, 2021, 12:53 PM

Previous topic - Next topic

david

Hi All,

Question 1
Is there a relatively simple way to take a character from a string and calculate its ascii value?
I think at one time there was the reverse process which took an ascii value and converted it to a character - using Char command but this seems to have gone from the documentation but is still recognized by the compiler.

Question 2
In the Cread8 command - is there a way to make a variable for the name of the table or some similar approach?

For an example if I have -
Dim Table1 As Code=As Byte   values......
Dim Table2 As Code=As Byte   values......
Dim Table3 As Code=As Byte   values......

String assignments for "Table" and "1","2","3"
newstring="Table"+"2"   (example)
value = Cread8  newstring [index]   Gives error.
Any ideas appreciated.

Cheers,
David


RGV250

Hi,
I have not tried it but I think for question 1 I would look at Mid$ to extract the character and Val for the value.

Haven't a clue for Q2

Bob

david

Hi,
Thanks for the reply.
Mid$ is certainly one way to extract characters from a string but I think Val only converts between different number systems (decimal, binary, hex) and not ascii.  I think it may require a look up table or several calculations based on the ordinal number of the letters which in itself I think involves a look up table.

Question 2 is a long shot but would be very useful.

Cheers,
David

top204

#3
Add the value 48 to a integer value to get the ASCII character value, and subtract 48 from an ASCII digit character value to get its integer value.

With the Cread8, CRead16, CRead24 and CRead32 commands, an integer variable can be used to hold the address of the Flash memory table.

    Dim FlashMemTable As Flash8 = 1,2,3,4,5,6
    Dim wAddress As Word
    Dim bIndex As Byte = 0
    Dim MyByte As Byte

    wAddress = AddressOf(FlashMemTable)
    MyByte = CRead8 wAddress[bIndex]

david

Wow - quicker service than McDonald's.
OK so I need to set up an ordinal table for the letter characters and add the offsets to arrive at the ascii value.

That's good news on the Cread8 command.  I shall try that and hopefully I may not need the first approach after all.
Many thanks.

Best regards,
David

tumbleweed

QuoteIs there a relatively simple way to take a character from a string and calculate its ascii value?
If you take a character from a string, you get its "ASCII value". For example, the character "A" has a byte value of 0x41 (dec 65).

top204

Tumbleweed is correct.

Remember, a String variable is, essentially, a Byte array with a null terminator, and the compler can slice a String variable as it does with an array. See the "Slicing a String" section on page 56 of the compiler's manual.

So a piece of code such as:

Dim MyString as String * 20
Dim MyByte as Byte

MyByte = MyString[0]

Is perfectly valid, and will extract ASCII character elements from the String variable, just like a Byte array.


david

Doh.  Of course.  I'm getting too old for this stuff -  I can tell.
Special apologies to Bob too.

Off subject - this new website is fantastic.  Everything works so well - adding pictures, code etc all just works.  Top marks!

Best regards,
David

david

Hi,
Just in case anyone is interested this is the result of the above help which seems to be working well.

mystring="Configuration123"   'print input
     HBusOut OLED_W,[$00,$21,$00,$7F]  'Reset column range and pointer
     HBusOut OLED_W,[$00,$22,$00,$07]   'Reset Page range and pointer
     
     For Chr=0 To Len (mystring)-1
     ascii=mystring[Chr]-46
       For Col=0 To 4               '5 bytes per character
       num= CRead8 Fonts [ascii*5+Col]
        HBusOut OLED_W,[$40,num]     'print 5 values into columns 0 to 4 for each character
       Next Col                   
       HBusOut OLED_W,[$40,$00]    'print blank spacer column in to 6th column
      Next Chr
       
      DelayMS 5000
      GoTo Wipe

Thanks for the replies and help to get over this bump in the road.

Cheers,
David