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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:52:34+00:00 2026-06-05T15:52:34+00:00

I got the following code, which converts a char[] to a byte[]: char[] cPwd

  • 0

I got the following code, which converts a char[] to a byte[]:

char[] cPwd = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
byte[] bPwd = new byte[cPwd.length * 2];
ByteBuffer.wrap(bPwd).asCharBuffer().put(cPwd);

What I guessing is that the code above would use the default platform encoding (UTF-8 or 16, i suppose). Right?

How would I make sure the chars from the char[] above use a specific encoding, say UTF-16, as opposed to relying on the default platform encoding?

ps – There is a constraintin my scenario: I am NOT ALLOWED to convert this char[] to a string like in

String str = new String(cPwd, "UTF-16");

Thank you in advance.

Alex.

  • 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-06-05T15:52:36+00:00Added an answer on June 5, 2026 at 3:52 pm

    By the time the VM sees them, the chars that you specify as literals in your code will be 16-bit Unicode characters. The asCharBuffer() method isn’t specified as performing any encoding. In other words, you should effectively get UTF-16, either little or big endian depending on the endianness of your ByteBuffer.

    If you want another encoding without going via the String constructor, you can use the CharSet.encode() method:

            CharBuffer cb = CharBuffer.wrap(your chars);
            Charset cs = Charset.forName("UTF-8");
            ByteBuffer bb = cs.encode(cb);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following code which should put an image in the top section/cell
Ive got the following code which I hacked together from website examples. It works
i've got the following code which runs a bat file. the bat file then
I've got the following code in my Services project, which is trying to grab
I've got the following java code, which is giving the error below: import java.io.File;
I've got the following javascript code, which enables a bunch of validators. ValidatorEnable(document.getElementById(<%=AddressValidator.ClientID %>),
The following code should display the id out of the JSON-object, which I got
I've got some basic questions about C++. Consider the following code in which I
How would dynamically draw markers on a Mapview? I've got the following code which
I am trying to run the following code (which I got from here ).

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.