Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert a string from 1 character set to another

Status
Not open for further replies.

rambleon

Programmer
Mar 6, 2004
130
IL
Hi, I have a text file which I want to read and display in a browser, to display the text I have to convert the character set from hexadecimal values 80 - 9A to E0 - FA,
bascicaly adding 60 (hex) to each charater before displaying it in the browser, what would be the best way to convert the string? Code example appriciated.
Thanks for any help
 
Assuming the text is in String s try something like:
Code:
    Dim c As Char
    For i As Integer = 1 to Len(s)
        c = Mid(s, i, 1)
        If Asc(c) >= &H80 And Asc(c) <= &H9A Then
            Mid(s, i, 1) = Chr(Asc(c) + &H60)
        End If
    Next


Bob Boffin
 
thanks bboffin that's fine, but I was wondering if there was a way of proccessing the whole string using RegEx or StrConv to replace characters
 
The substitution features of RegEx don't lend themselves to this type of substitution and StrConv only offers a few predefined conversions.

What you really need is the Translate function available only for Windows CE!

It's rather odd, Intel processors have an opcode that will do exactly what you want but it doesn't appear to be mapped to a VB function or method.

Anyway you're stuck with something like what I suggested although there is a more efficient way of doing this that requires you to set up an array of 256 byte (or char) values representing the characters you wish to end up with.

You then index into this array using the ASCII value of the source character and return the value found in the array. Where no translation is required you must set the value in the array to be the same as that of the indexing character.

This is in fact what the opcode in the processor I referred to earlier does but I doubt if there will be a nice simple mapping to it!

Funnily enough COBOL has a TRANSLATE verb that works exactly like this but I don't advocate learning COBOL just for this.



Bob Boffin
 
Thanks Bob,
For the explanation, I'd been trying to find a solution using RegEx, now I understand why I didn't find one.
Cobol's my mother tongue, in the dialect I use the verb is TRANSFORM. Till now I've been using Cobol to generate web pages, this is my first foray into ASP and Visual Studio, maybe I should stick with Cobol.
As a matter of intrest I thought there was something like

Regex.Replace(string to be modified, [old char range], [new char range])

which would return the modified string, seems logical but I could'nt get it to work.
 
You are quite right the verb is TRANSFORM.

Sounds like you're an old stager like me. My first high level language was COBOL back in the 1960s

Staying with the COBOL theme Fujitsu have a COBOL.NET compiler that a friend of mine uses extensively to port COBOL applications.



Bob Boffin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top