Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:59:03+00:00 2026-05-26T22:59:03+00:00

I am creating UTF16 text files with Matlab, which I am later reading in

  • 0

I am creating UTF16 text files with Matlab, which I am later reading in using Java. In Matlab, I open a file called fileName and write to it as follows:

fid = fopen(fileName, 'w','n','UTF16-LE');
fprintf(fid,"Some stuff.");

In Java, I can read the text file using the following code:

FileInputStream fileInputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(fileInputStream, "UTF-16LE"); 
String s = scanner.nextLine();

Here is the hex output:

Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13
00000000  73 00 6F 00 6D 00 65 00 20 00 73 00 74 00 75 00 66 00 66 00  s.o.m.e. .s.t.u.f.f.

The above approach works fine. But, I want to be able to write out the file using UTF16 with a BOM to give me more flexibility so that I don’t have to worry about big or little endian. In Matlab, I’ve coded:

fid = fopen(fileName, 'w','n','UTF16');
fprintf(fid,"Some stuff.");

In Java, I change the code to:

FileInputStream fileInputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(fileInputStream, "UTF-16");
String s = scanner.nextLine();

In this case, the string s is garbled, because Matlab is not writing the BOM. I can get the Java code to work just fine if I add the BOM manually. With the added BOM, the following file works fine.

Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15
00000000  FF FE 73 00 6F 00 6D 00 65 00 20 00 73 00 74 00 75 00 66 00 66 00  ÿþs.o.m.e. .s.t.u.f.f.

How can I get Matlab to write out the BOM? I know I could write the BOM out separately, but I’d rather have Matlab do it automatically.

Addendum

I selected the answer below from Amro because it exactly solves the question I posed.

One key discovery for me was the difference between the Unicode Standard and a UTF (Unicode transformation format) (see http://unicode.org/faq/utf_bom.html). The Unicode Standard provides unique identifiers (code points) for characters. UTFs provide mappings of every code point “to a unique byte sequence.” Since all but a handful of the characters I am using are in the first 128 code points, I’m going to switch to using UTF-8 as Romeo suggests. UTF-8 is supported by Matlab (The warning shown below won’t need to be suppressed.) and Java, and for my application will generate smaller text files.

I suppress the Matlab warning

Warning: The encoding 'UTF-16LE' is not supported.

with

warning off MATLAB:iofun:UnsupportedEncoding;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:59:03+00:00Added an answer on May 26, 2026 at 10:59 pm

    Try the following code (I am using UNICODE2NATIVE and NATIVE2UNICODE functions to do the conversions):

    %# convert string and write as bytes
    str = 'Some stuff.';
    b = unicode2native(str,'UTF-16');
    fid = fopen('utf16.txt','wb');
    fwrite(fid, b, '*uint8');
    fclose(fid);
    

    We can even check the hex values of the bytes written (first two being the BOM):

    >> cellstr(dec2hex(b))'
    ans = 
      Columns 1 through 10
        'FF'    'FE'    '53'    '00'    '6F'    '00'    '6D'    '00'    '65'    '00'
      Columns 11 through 20
        '20'    '00'    '73'    '00'    '74'    '00'    '75'    '00'    '66'    '00'
      Columns 21 through 24
        '66'    '00'    '2E'    '00'
    
    >> char(b)
    ans =
    ÿþS o m e   s t u f f . 
    

    Now we can read the created file using MATLAB’s own methods:

    %# read bytes and convert back to Unicode string
    fid = fopen('utf16.txt', 'rb');
    b = fread(fid, '*uint8')';          %'
    fclose(fid);
    str = native2unicode(b,'UTF-16')
    

    Or use Java methods directly if you prefer:

    scanner = java.util.Scanner(java.io.FileInputStream('utf16.txt'), 'UTF-16');
    str = scanner.nextLine()
    scanner.close()
    

    both should read the string correctly…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataTable that I'm creating an XML file from using .WriteXML(..), although
Creating a zip file using Send To -> Compressed folder excludes .hg folder on
Here is overall code, for taking a tab delimetted text file, and creating a
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating sprite sheets aka texture atlases rather than using many hundres of individual images
I am about to start working on something the requires reading bytes and creating
Creating a popup window from main page with window.open, the child/popup page uses the
When creating form elements with Zend (using Zend Studio for Eclipse), I'd like some
Creating a layout in Java as the number of TableLayouts required is not known

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.