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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:14:10+00:00 2026-06-08T20:14:10+00:00

TL;DR : In Java, will casting a character obtained from a String via the

  • 0

TL;DR: In Java, will casting a character obtained from a String via the charAt method to a byte always yield the same value?

I am reading files which are encoded with arbitrary (unknown to us) character encodings. I need to parse these files and look for certain words, e.g. “TAG”. I placed certain restrictions on the file contents, such as “when looking for a tag, the bytes for “TAG” must be the same as their ASCII representation”.

For example, suppose I have the following file:
0x00 0x11 0x22 0x33 0x54 0x41 0x47 0x77 0x88 0x99 0xaa 0xbb
Since the ASCII values for T, A and G are respectively 0x54, 0x41 and 0x47, I can find “TAG” in the file by parsing the bytes themselves.
0x00 0x11 0x22 0x330x54 0x41 0x470x77 0x88 0x99 0xaa 0xbb

However, I need to hard-code the value of the bytes I am looking for. To do this, I call String‘s charAt(int i) method and cast the char to a byte.

Here is, for example, how I would verify an arbitrary byte (called b) for the byte representation of ‘T’:
String tag = "TAG";
char t = tag.charAt(0);
if ((byte)t == b){
        //magic goes here, such as comparing the 'A' and the 'G'
}
Note: the code is not actually like that, and the verification algorithm is much more elegant.

This works fine on my local machine. However, this will be run on machines which may contain very strange encodings. What worries me is whether casting a character obtained with charAt to a byte might yield a different value depending on the machine. I know that Java always encodes chars with the UTF-16 character encoding, but I am worried that when converting from a String to a character and then to a byte might yield strange results.

So, in short, will casting a character obtained from a String via the charAt method to a byte always yield the same value? Or will it depend on an external factor?

Thanks for your help!

Note: I cannot hard-code the bytes themselves (in, for example, a byte array) since they can be very very long and may be changed very often in the future.

  • 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-08T20:14:12+00:00Added an answer on June 8, 2026 at 8:14 pm

    java.lang.string.charAt will always return a 16 bit UTF-16 character, which will always be the same when you cast it to a byte, though because char is a 16-bit unsigned data type, casting it as an 8-bit signed byte might give you unwanted behavior. However if your source data is ASCII, you will get exactly the type of behavior you expect.

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

Sidebar

Related Questions

Please understand firstly that I fully understand that Java will return a String when
Will Java code built and compiled against a 32-bit JDK into 32-bit byte code
As we all know Java program will start executing from the public static void
I need a mutable boolean field in Java (I will return this field via
Java will not allow me to extend from ByteBuffer because it implements abstract methods
It appears in java.lang.String.java, that Java will only generate the hashcode, and then store
I am trying to read a UTF8 string via a java.nio.ByteBuffer. The size is
I am unable to build my app because R.java will not update the ids.
Will Java 8 support pattern matching like Scala and other functional programs do? I'm
I have coded a server in Java that will have several clients connected to

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.