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 6119837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:35:57+00:00 2026-05-23T15:35:57+00:00

I want to use sed to take any arbitrary stream and append a null

  • 0

I want to use sed to take any arbitrary stream and append a null byte to each byte.

I’ve tried a number of things, but have trouble with:

  1. matching any byte – . seems to be a subset, i.e. any character, not any byte.
  2. adding a null byte – I thought it should be \0, but that doesn’t work.
  • 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-23T15:35:58+00:00Added an answer on May 23, 2026 at 3:35 pm

    Answer for original question

    I suggest using Perl or Python; here’s a (verbose) Perl solution:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    while (<>)
    {
        s/./$&\0/gs;
        print;
    }
    

    For ASCII text input, this gives you UTF-16LE output (without a BOM). Given that it is Perl, TMTOWTDI, and it can be reduced to a one-line; see the answer by paxdiablo.

    Given this explicit loop structure, the easiest way to print the BOM is to add a print statement before the loop:

    printf "%c%c", 0xFF, 0xFE;
    

    Given a one-liner, you need a BEGIN block:

    perl -pe 'BEGIN{printf "%c%c", 0xFF, 0xFE;} s/(.)/\1\0/gs;' "$@"
    

    There are at least 4, arguably 5, superfluous spaces in that script.


    Answer for revised then reverted question

    The modified question was:

    I want to use sed to take any arbitrary a UTF-8 stream and append a null byte convert it to each byte UTF-16. What’s the magic sauce to make this happen?

    The revised question is a very different proposition from the original. Converting UTF-8 to UTF-16 is, in general, moderately complex; you have to read 1-4 bytes of input, and generate 2 or 4 bytes of output, worrying about surrogates and malformed input, etc. The original question – how to add a NUL (or zero) byte after each character in the input – is much, much, much simpler. (It remains true that if the input is ASCII – 7-bit byte values between 0 and 127 – then the ‘add a NUL afterwards’ gives you UTF-16LE. But only if the UTF-8 data is in the ASCII subset.)

    However, for accurate translation, the tool of choice should be iconv:

    Usage: iconv [OPTION...] [-f ENCODING] [-t ENCODING] [INPUTFILE...]
    or:    iconv -l
    
    Converts text from one encoding to another encoding.
    
    Options controlling the input and output format:
      -f ENCODING, --from-code=ENCODING
                                  the encoding of the input
      -t ENCODING, --to-code=ENCODING
                                  the encoding of the output
    
    Options controlling conversion problems:
      -c                          discard unconvertible characters
      --unicode-subst=FORMATSTRING
                                  substitution for unconvertible Unicode characters
      --byte-subst=FORMATSTRING   substitution for unconvertible bytes
      --widechar-subst=FORMATSTRING
                                  substitution for unconvertible wide characters
    
    Options controlling error output:
      -s, --silent                suppress error messages about conversion problems
    
    Informative output:
      -l, --list                  list the supported encodings
      --help                      display this help and exit
      --version                   output version information and exit
    

    Hence, to convert from UTF-8 to UTF-16LE:

    iconv -f UTF-8 -t UTF-16LE  input > output
    

    Interestingly, I don’t see an option to add a BOM to the output, at least not with iconv version 1.11 from 2007 on RHEL 5 (nor the same version on MacOS X, dated 2006 — don’t ask, I don’t know!).

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

Sidebar

Related Questions

I want to escape a DOS filename so I can use it with sed.
I am writing script, I need to use sed for changing somefiles, I want
I want to use sed to modify my file named baz. When i search
I want to use sed to replace all ' with \' and all with
Hello I want to use sed to search a file for 3-5 alpha chars
I have this line that I want to use sed on: --> ASD =
This is a test file: xxx hello hello others I want to use sed:
I'm trying to use sed for a repetitive task at hand. What I want
In BASH I thought to use sed , but can't figure how to extract
I want to use MPC (CLI interface to MPD) but unfortunately to me it

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.