[futurebasic] Re: [FB] Detect the encoding of a file

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2016 : Group Archive : Group : All Groups

From: Brian S <fblistserve@...>
Date: Fri, 11 Nov 2016 09:58:46 -0700
> Eugen wrote:
> 
> How can I detect the encoding of a text file after I have opened it ? In FB, of course.
It would be better if you provided a code snippet showing how you currently open and read the file. This will allow responders to rule out various options( such as reading a file on another server versus a local file ).

AFAIK, there are no FB commands/verbs to accomplish what I think you’re asking. My approach would be:

Use this NSString method to read the file into an NS/CFString:

+ stringWithContentsOfFile:usedEncoding:error:

That method returns the encoding. This Objective-C could be wrapped in an FB function which returns a CFString and the interpreted encoding. Your code could then either use the CFString directly, change its encoding and/or convert it to some other data type ( bytes, NSData etc. ) for processing. 

An advantage of NSString: it’s guaranteed to represent whatever is put into it using the encoding determined when it was created.

This should be a good coding challenge. Only a few lines of code are required.


Brian