Solved

Convert ASCII to String

Posted on 2007-12-04
17
5,520 Views
Last Modified: 2013-11-27
I have a table containing a lot of text, contained within the text is ASCII coding, I am looking for a way to update the ASCII code to its string value
E.g. change [ to [
0
Comment
Question by:daverichardson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
17 Comments
 
LVL 75
ID: 20404292
Well, Chr(91) will return [

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20404297
you will need an update query

update NameOftable
set [fieldName]=replace([fieldName],"#91",chr(91))

0
 
LVL 75
ID: 20404307
Chr(Replace("&#91","&#",""))  

returns

[
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:daverichardson
ID: 20404417
Thanks for the replies, I should have made myself a bit clearer [ isnt the only ASCII coding that was just an example there are lots of different characters. I was looking for a way to convert all ASCII coding to string.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20404493
well, you have to give more info regarding your table.

you  will need a function to do that.
*open the table as recordset
*locate and replace all occurence of the ASCII characters


if you can zip your db and upload (Now, i'll be busy later on) here www.ee-stuff.com 
i can write you a function to do that..


0
 
LVL 15

Expert Comment

by:JimFive
ID: 20404560
Create a Function as below.  Use the Function in your query.

--Jimfive
Function htmlUnFormat(s As String) As String
Dim done As Boolean
Dim LocStart, LocEnd As Integer
Dim char As String
 
done = False
While Not done
    LocStart = InStr(1, s, "&#")
    If LocStart = 0 Then
        done = True
    Else
        LocEnd = InStr(LocStart, s, ";")
        If LocEnd > LocStart Then
            char = Mid(s, LocStart + 2, LocEnd - LocStart - 2)
            If IsNumeric(char) Then
                s = Replace(s, Mid$(s, LocStart, LocEnd - LocStart + 1), Chr(CInt(char)))
            End If
        Else
            done = True
        End If
    End If
Wend
htmlUnFormat = s
End Function

Open in new window

0
 

Author Comment

by:daverichardson
ID: 20404733
JimFive i get invalid procedure call or argument on this line
s = Replace(s, Mid$(s, LocStart, LocEnd - LocStart + 1), Chr(CInt(char)))
0
 
LVL 15

Expert Comment

by:JimFive
ID: 20405217
Hmm...Try Mid instead of Mid$
--
JimFive
0
 
LVL 5

Expert Comment

by:TheGD
ID: 20405267
Your problem is very simple and can be solved by the following code.

Follow the following instructions:
1. Create a New Module in your Database.
2. Copy Paste the following code into that newly created Module that I have written just for your custom need
3. Press F5 and then Press Enter.
4. Enter Table Name to FIX, i.e. to replace ALL &#code; patterns with their character equivalents

I have tested it on your Uploaded Table and found it fully functionaly and bugs free, so Enjoy!

Option Compare Database
 
Function ConvertASCII(s As String) As String
    Dim str As String, st As Integer, prevst As Integer, i As Integer
    str = ""
    st = 1
    i = 0
    Do
        i = InStr(st, s, "&#")
        If i <> 0 Then
            If i <> 1 Then
                str = str + Mid(s, st, i - st)
            End If
            st = InStr(i, s, ";") + 1
            str = str + ChrW(Int(Mid(s, i + 2, st - 1 - i - 2)))
        Else
            str = str + Mid(s, st)
        End If
    Loop While i <> 0
    
    ConvertASCII = str
End Function
 
Sub FixTable()
    Dim r As Recordset
    Dim TableName As String
    
    TableName = InputBox("Enter Table Name to Fix ASCIIs", "Table Name", "tmp")
    'On Error GoTo Err
    Set rs = CurrentDb.OpenRecordset("Select * From " & TableName)
    Do While Not rs.EOF
        rs.Edit
        
        For i = 0 To rs.Fields.Count - 1
            rs.Fields(i) = ConvertASCII(rs.Fields(i))
        Next
        
        rs.Update
        rs.MoveNext
    Loop
    rs.Close
    
    MsgBox "Done!"
    
    Exit Sub
Err:
    If Err.Number = 3078 Then
        MsgBox "The table name is invalid. No such table in the current database.", vbCritical, "Table not exist"
    Else
        MsgBox "Error: " & Err.Description
    End If
End Sub

Open in new window

0
 

Author Comment

by:daverichardson
ID: 20405310
That didnt work either, Its throwing the error on this value &#8482;
0
 
LVL 75
ID: 20405353
I believe 8482 is in the extended ASCII set ...


"The ChrW function returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function"


mx
0
 

Author Comment

by:daverichardson
ID: 20405472
TheGD that works great, except if it comes across a null field then I get invalid use of null error
0
 
LVL 15

Expert Comment

by:JimFive
ID: 20405569
As DatabaseMX suggested.  Change the Chr to ChrW
--
JimFive
0
 
LVL 15

Accepted Solution

by:
JimFive earned 125 total points
ID: 20405604
It also appears that my code will have a problem if the string contains a &# that does not indicate a valid character. e.g. a string like: "What the &#!*; is this"

Try this revision:
Function htmlUnFormat(s As String) As String
Dim done As Boolean
Dim LocStart, LocEnd As Integer
Dim char As String
 
done = False
While Not done
    LocStart = InStr(1, s, "&#")
    If LocStart = 0 Then
        done = True
    Else
        LocEnd = InStr(LocStart, s, ";")
        If LocEnd > LocStart Then
            char = Mid(s, LocStart + 2, LocEnd - LocStart - 2)
            If IsNumeric(char) Then
                s = Replace(s, Mid(s, LocStart, LocEnd - LocStart + 1), ChrW(CInt(char)))
            Else
                done = True
            End If
        Else
            done = True
        End If
    End If
Wend
htmlUnFormat = s
End Function

Open in new window

0
 
LVL 5

Assisted Solution

by:TheGD
TheGD earned 125 total points
ID: 20409379
O Yes, the Null field was not not in your uploaded database so this error didnt came across me. Here is the revision of my previous code in which All the Null Fields would be ignored:
Option Compare Database
 
Function ConvertASCII(s As String) As String
    Dim str As String, st As Integer, prevst As Integer, i As Integer
    str = ""
    st = 1
    i = 0
    Do
        i = InStr(st, s, "&#")
        If i <> 0 Then
            If i <> 1 Then
                str = str + Mid(s, st, i - st)
            End If
            st = InStr(i, s, ";") + 1
            str = str + ChrW(Int(Mid(s, i + 2, st - 1 - i - 2)))
        Else
            str = str + Mid(s, st)
        End If
    Loop While i <> 0
    
    ConvertASCII = str
End Function
 
Sub FixTable()
    Dim r As Recordset
    Dim TableName As String
    
    TableName = InputBox("Enter Table Name to Fix ASCIIs", "Table Name", "tmp")
    On Error GoTo Err
    Set rs = CurrentDb.OpenRecordset("Select * From " & TableName)
    Do While Not rs.EOF
        rs.Edit
        
        For i = 0 To rs.Fields.Count - 1
            rs.Fields(i) = ConvertASCII(rs.Fields(i))
        Next
        
        rs.Update
        rs.MoveNext
    Loop
    rs.Close
    
    MsgBox "Done!"
    
    Exit Sub
Err:
    If Err.Number = 3078 Then
        MsgBox "The table name is invalid. No such table in the current database.", vbCritical, "Table not exist"
    ElseIf Err.Number = 94 Then 'IGNORE NULL FIELD
        Resume Next
    Else
        MsgBox "Error: " & Err.Description
    End If
End Sub

Open in new window

0
 

Author Comment

by:daverichardson
ID: 20409669
Thanks Guys
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question