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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:24:05+00:00 2026-05-14T05:24:05+00:00

I’m trying to do a simple caesarian shift on a binary string, and it

  • 0

I’m trying to do a simple caesarian shift on a binary string, and it needs to be reversable. I’ve done this with this method..

public static String cShift(String ptxt, int addFactor)
    {
        String ascii = "";
        for (int i = 0; i < ptxt.length(); i+=8)
        {
            int character = Integer.parseInt(ptxt.substring(i, i+8), 2);
            byte sum = (byte) (character + addFactor);
            ascii += (char)sum;
        }
        String returnToBinary = convertToBinary(ascii);
        return returnToBinary;
    }

This works fine in some cases. However, I think when it rolls over being representable by one byte it’s irreversable. On the test string "test!22*F ", with an addFactor of 12, the string becomes irreversible. Why is that and how can I stop it?

edit: For clarification sake, the test string is converted to binary before being passed in. Here is convertToBinary

public static String convertToBinary(String str)
    {
        char [] array = str.toCharArray();
        String binaryToBeReturned = "";

        for (int i = 0; i < str.length(); i++)
        {
            String binary = Integer.toBinaryString((int)array[i]);
            binary = padZeroes(binary);
            binaryToBeReturned += binary;
        }
        return binaryToBeReturned;
    }

When I run this with a cShift of 12, followed by a cShift of -12 to reverse, I get this…

01110100011001010111001101110100001000010011001000110010010001100010101000100000
111111111000000001110001011111111111111110000000001011010011111000111110010100100011011000101100
ÿ?qÿ?->>R6,
ÿótesÿót!22F*

The first string is just converting the test string to binary. The second string is the result of the cShift in binary. The third string is the result of converting this to ascii, and the fourth string is the result of reversing with -12 on cShift and converting to ascii.

It’s pretty clear to me that somehow there are extra bits being added from the roll over and I’m not totally sure how to deal with it. Thanks.

  • 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-14T05:24:06+00:00Added an answer on May 14, 2026 at 5:24 am

    You need to mask the byte when widening to char, because otherwise the sign bit will be extended.

    ascii += (char)(sum & 0xFF)
    

    This masking pattern applies when widening a signed numeric type if you don’t want the sign extension.

    anInt = aByte & 0xFF;
    anInt = aShort & 0xFFFF;
    aLong = anInt & 0xFFFFFFFFL; // notice the L
    

    Here’s an example to illustrate:

    byte b = -1; // 0xFF
    char ch = (char) b; // 0xFFFF
    int i = ch;
    System.out.println(i); // prints "65535", which is 0xFFFF
    
    byte b = -1; // 0xFF
    char ch = (char) (b & 0xFF); // 0xFF
    int i = ch;
    System.out.println(i); // prints "255", which is 0xFF
    

    There is a lesson to be had here. If you’ve read Java Puzzlers, you’ll see a few that revolves around sign extension hooplas. This puzzle from the book is essentially the same as the one I had above, but perhaps more confusing:

    // Java Puzzlers, Puzzle 6: Multicast
    System.out.println((int) (char) (byte) -1); // prints 65535
    

    There are two ways to remedy this:

    • Avoid working with byte and short. You rarely need to.
    • If you are working with them, always be wary of the need to mask.
    • byte to char is always tricky because:
      • Although char is wider than byte…
      • char is unsigned while byte is!!!
      • Therefore, it’s not a straightforward widening conversion, but a widening-narrowing conversion!

    JLS 5.1.4 Widening and Narrowing Primitive Conversions

    The following conversion combines both widening and narrowing primitive conversions:

    • byte to char.

    First, the byte is converted to an int via widening primitive conversion, and then the resulting int is converted to a char by narrowing primitive conversion.


    Additional references

    • 5.1.2 Widening Primitive Conversion
    • 5.1.3 Narrowing Primitive Conversions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Types like uint16_t or uint32_t exist so that you can… May 14, 2026 at 7:29 pm
  • Editorial Team
    Editorial Team added an answer by checking if elememt exist on the page before execute… May 14, 2026 at 7:29 pm
  • Editorial Team
    Editorial Team added an answer When you use a member function pointer, you need to… May 14, 2026 at 7:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.