none
reading a string one character at a time RRS feed

  • Question

  • Hi,

    I'm hoping you use SmallBasic for year 10 exam students at my school.  But, I have found a problem I cannot solve.

    I need to be able to read one character at a time from a string (txt file) and convert each char to its ACSII code.

    How do to I read one char at a time from a string to enable processing?

    Thanks for your help.

    Baz 

    Thursday, April 23, 2015 12:24 PM

Answers

All replies

  • Look at the command Text.GetSubText(longstring,N,1) which gives you the Nth character from longstring.

    Jan [ WhTurner ] The Netherlands

    Thursday, April 23, 2015 12:48 PM
    Answerer
  • That did the trick.

    Many thanks

    Baz

    Thursday, April 23, 2015 3:00 PM
  • Here is an over the top solution that will display the Hex value of the character codes of every character in the file.
    TextWindow.Write("Enter full file name: ")
    filnam = TextWindow.Read()
    
    contents = File.ReadContents(filnam)  'read the entire file
    
    TextWindow.Clear()
    TextWindow.WriteLine("File Name:  " + filnam)
    TextWindow.WriteLine("Offset: 0")
    
    col = 0
    row = 5
    TextWindow.CursorLeft = col
    TextWindow.CursorTop = row
    For i= 1 To Text.GetLength(contents)
      ch = Text.GetSubText(contents, i,1)
      chVal = Text.GetCharacterCode(ch)
      ConvertToHex()
      TextWindow.CursorLeft = col
      If chVal < 32 Then
        TextWindow.Write(".")
      Else
        TextWindow.Write(ch)
      EndIf
      TextWindow.CursorLeft = 20 + 2 + (col * 3)
      TextWindow.Write(Text.GetSubText(hexstr,1,2))
      col = col + 1
      If col = 8 Then
        col = col + 1
      EndIf
      If col > 16 Then
        col = 0
        row = row + 1
        If row > 20 then 
          TextWindow.CursorTop = 23
          TextWindow.CursorLeft = 25
          TextWindow.Write("< < < Press ENTER to Continue > > >")
          TextWindow.Read()
          TextWindow.Clear()
          TextWindow.WriteLine("File Name:  " + filnam)
          TextWindow.WriteLine("Offset: " + i)
          row = 5
        EndIf
        TextWindow.CursorTop = row
      EndIf
    EndFor
    
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    
    Sub ConvertToHex
      HexValue[0] = "0"
      HexValue[1] = "1"
      HexValue[2] = "2"
      HexValue[3] = "3"
      HexValue[4] = "4"
      HexValue[5] = "5"
      HexValue[6] = "6"
      HexValue[7] = "7"
      HexValue[8] = "8"
      HexValue[9] = "9"
      HexValue[11] = "A"
      HexValue[12] = "B"
      HexValue[13] = "C"
      HexValue[14] = "D"
      HexValue[15] = "E"
      
      val = chVal
      hexstr = "h" 'Need to force Small basic to concatenate rather than add
      While val > 0
        hexPos = Math.Remainder(val, 16)
        hexstr = HexValue[hexPos] + hexstr
        val = Math.Floor(val / 16)
      EndWhile
      For hi = Text.GetLength(hexstr) To 2 
        hexstr = "0" + hexstr
      EndFor
    EndSub
    
    

    Enjoy!
    Friday, April 24, 2015 1:59 PM
    Answerer
  • Hi Baz

    You can "Mark as answer" to update this threads status.

    • Marked as answer by Baz Coop Monday, April 27, 2015 1:51 PM
    Friday, April 24, 2015 2:14 PM
    Moderator