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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:19:59+00:00 2026-06-01T11:19:59+00:00

I have created two functions that sum and subtract the numeric values of two

  • 0

I have created two functions that sum and subtract the numeric values of two chars and return the char of the result in PHP. It’s something like this:

function sumchars($c1, $c2) {
    return chr(ord($c1) + ord($c2));
}

function subchars($c1, $c2) {
    return chr(ord($c1) - ord($c2));
}

Well, I also created these two function in Java and they are supposed to work as the PHP ones.

public static String sumchars(char c1, char c2) {
    return (char)((int)c1 + int(c2));
}

public static String subchars(char c1, char c2) {
    return (char)((int)c1 -+ int(c2));
}

It seems like the PHP function chr() knows how to treat results that are outside the ASCII range (greater than 255) and also those that are negative (less than 0). Not the same happens in Java (where instead of a function I just cast the integer result). I know that I could create a simple function for that but I wonder if there’s already one. I want these functions to be very fast and I would prefer to use something already implemented rather than a function created by me.

Thanks in advance for your recommendations!

  • 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-01T11:20:00+00:00Added an answer on June 1, 2026 at 11:20 am

    Just add 256 to make sure you don’t have negative values and then mod that result by 256 to make sure it’s not larger than 256. ( alternatively, use if else statements ).

    public static String sumchars(char c1, char c2) {
        return (char)((((int)c1 + int(c2)) + 256) % 256);
    }
    
    public static String subchars(char c1, char c2) {
        return (char)(((int)c1 - int(c2)) + 256) % 256);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery delay between animations I have created two functions: One that animates
I have created two viewController classes such that one is superclass of another.i have
I have a simple PHP script with a form that has two select fields,
In my controller I have two functions that pull all records from the db
I have two functions to add and remove table rows that contain a form
I have two JS functions that preforms AJAX calls, I've wanted to test them
I have two functions: one that creates a new <textarea> when a button is
I have created two forms in my Windows Application. One Form acts as a
I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive();
I have created two JavaScript files. One file is "validators.js" and the other is

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.