B4J Question [SOLVED] Convert/encode string StdOut (from jshell) cp737->utf8 [on-the-fly]

Magma

Expert
Licensed User
Longtime User
Hi there,

is it possible to convert cp737 -got it from StdOut (when returned from jshell)- to utf8 on the fly... without putting into file and reading with textreader...?

I thought that was possible to do with:

B4X:
Sub s2utf(a As String) As String
    Dim c() As Byte = a.getbytes("cp737")
    Dim utf As String =""
    Dim m As Int = c.Length-1
    For i=0 To m
        utf = utf & Chr(c(i))
    Next
    Return utf
End Sub

but getting ????? ???? questionmarks...

...Thanks
 

Magma

Expert
Licensed User
Longtime User
It works!

Created a simple routine to get the CP Encoding - mode:

B4X:
'call it, like that:
    'MODE CON CP
    Wait for (GetShellEncoding) complete (s As String)
    If s.Length>0 Then Main.ShellEncoding="cp" & s



Sub GetShellEncoding As ResumableSub
    Dim js As Shell
    Dim params As List
    Dim senc As String
    params.Initialize

    params.Add("CON")
    params.Add("CP")
    js.Initialize("js", "mode.com", params)
    js.WorkingDirectory="C:\Windows\System32" 'if needed...
    js.Run(-1)
    Wait for (js) js_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success=True Then
        Dim searchfor1 As String
        searchfor1="Code page:"
        Dim a As Int=StdOut.IndexOf(searchfor1)
        If a>0 Then
            Dim c As Int=StdOut.IndexOf2(Chr(13),a+searchfor1.Length)
            If c>a Then
                senc=StdOut.SubString2(a+searchfor1.Length,c).trim
                ' <yyy>
                '                437: United States
                '                850: Multilingual (Latin I)
                '                852: Slavic (Latin II)
                '                855: Cyrillic (Russian)
                '                857: Turkish
                '                860: Portuguese
                '                861: Icelandic
                '                863: Canadian-French
                '                865: Nordic
                '                866: Russian
                '                869: Modern Greek
                '                737: Greek ?
            End If
        End If
    End If
   
    Return senc
   
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
more identifiers for codepage (with a smell of 90's at begin)


Also you can get active Codepage (at cmd, internal dos command - almost forgot it... have to use it from 93) with:
B4X:
Chcp
*So you can change it at my jshell routine if you want... but also the the searchfor1 string to "Active code page:"
 
Upvote 0
Top