Help with Ascii string

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SonOfBc
Posts: 8
Joined: Mon Nov 12, 2012 10:39 pm

Help with Ascii string

Post by SonOfBc » Fri Nov 23, 2012 10:46 pm

Can anyone help me with the code to construct a ascii string?
As an example there are 10 consecutive bytes held in program memory as follows:

30 34 31 35 44 38 42 31 39 37
These in ASCii are: 0415D8B197

I would like to take these 10 Hexadecimal numbers and create an ASCii string like: Str_Var = "0415D8B197"
I have tried the Conversion module but there is no HexToAscii available.

Any help would be appreciated.

Thanks
SamB

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Sat Nov 24, 2012 10:09 am

There is no such conversion function because a hex number and ascii are the same, as the following shows:

Code: Select all

include "usart.bas"
dim str as string
str = char($30) + char($34) + char($31)
usart.SetBaudrate(br19200)
usart.write(str,13,10)
so if you are reading a byte, typecast to char for string manipulation.

SonOfBc
Posts: 8
Joined: Mon Nov 12, 2012 10:39 pm

Post by SonOfBc » Sat Nov 24, 2012 4:03 pm

Thank you Dave.. It does work but I am still a little confused.
After I asked for help in this forum I came up with a work-around because to continue with my code I needed this function.
You mentioned that "a hex number and ascii are the same' but then why did my work-around do the same thing as your 'Type Casting' solution?

Code: Select all

For Index = 0 To 9
            Byte_Var = Mem.ReadByte(TTA + (10 * Count) + Index)
            
            If Byte_Var >= $30 And Byte_Var <= $39 Then
                Byte_Var = Byte_Var - $30
            Else
                Byte_Var = Byte_Var - $37
            EndIf
            UserTag = UserTag + HexToStr(Byte_Var)
            
        Next 'Index 
            If UserTag = TagNum Then
                Result = errOK
Just wondering... :roll:

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Sat Nov 24, 2012 5:07 pm

You are subtracting a base offset, which is giving you the numeric value - you then convert this value to a string, which is adding the offset back on! Just typecast the byte value. Comparing the size of the generated program should convince you of the best method...

Post Reply