• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xor ^ on two byte arrays

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. If you have a minute, why does this throw a compile lossy conversion from int to byte error on the xor carrot? It's two byte arrays (1s and 0s, right?) thank you.


 
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I see you fixed several other errors I saw when I used the first version of code you posted.  The remaining issue is that most (I think all) binary operators upcast their result to at least int, unless one of the operands is long or double so the larger return type is required.  In this case, the operands are both bytes, but the result is an int.  I'm not sure why they did it that way, but they did.  int is basically the default type that Java uses whenever it can.  So you just have to get used to downcasting to the smaller type if that's what you need.
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize, I was typing the code from one pc to the other instead of just copying/pasting so I noticed some typos initially.

yeah, I saw examples around where they cast the resultant as a byte despite the byte operands but can't figure out why. It sounds like it's just a thing they did...ill mess around with that some more.

One thing I wanted to ask about, after the XOR carrot operation, I convert the resultant byte[] xor_guy to a String...

<br /> <br /> However, xor_guy_string comes out really weird, with non standard characters, like upside down triangles, deck of cards stuff like clubs, hearts, etc and really long white space which doesn't appear to be counted in the length. Is there an encode/decode needed to get this to a "usable" string , with standard keyboard chars, in a text editor (and back)? <br /> >
 
Mike Simmons
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends what you want to use the string for.  The byte[] array is binary data, and after these XOR operations it's no probably longer following the UTF-8 or any other standard character encoding.  Is there any reason to think that this string would mean something?  If you just want to render some sort of printable string from the byte[], you can use something like Base64 encoding:

This is potentially useful if you need to pass binary data in a text-based format and decode it later.  Don't know if that's helpful for you though...
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there an encode/decode needed to get this to a "usable" string


Try this:
 
Thomas Griffith
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:This is potentially useful if you need to pass binary data in a text-based format and decode it later.  Don't know if that's helpful for you though...



yes, it is. I've been messing around with obfuscation approaches and I actually did some base64 encoding/decoding on text before the xor carrot stuff, just throwing one on top of the other, I guess and following your adivce, I was able to encode and decode the resultant xor_guy_string in base64 (getting a UTF-8 string after the encode then the ugly queen of hears, clubs stuff again after the decode). thank you again.

Norm Radder wrote:

Is there an encode/decode needed to get this to a "usable" string


Try this:



that's interestng, it returns an array of ints with the number of array elements matching the xor_guy_string length. So each weird char, like the club or diamond, matches up to an int somehow?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 So each weird char, like the club or diamond, matches up to an int somehow?


Everything stored in a computer's memory is contained in a byte (8 bits).  How those bytes are represented on the screen depends on the software presenting them.
 
Mike Simmons
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The details depend on the system you're on, and what character encoding it uses by default.  Try this:

That defaultCharset() is what's used implicitly by your new String(xor_guy) call.  That will determine the rules of how the bytes in the array correspond to characters.  It may be one byte per character, or it may be something more complicated.  Often it's a mix where standard Roman characters and Arabic numerals are  rendered as you would expect, but more "exotic" characters (from a US / European perspective) are one character per two or three bytes.
 
Marshal
Posts: 28260
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, producing basically random bytes and trying to figure out what you're seeing when you pretend they are chars and display them as such, that doesn't have much point to it. Normally when you see those characters it means you have done something wrong. (Like misinterpreting bytes as chars.)
 
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . binary operators upcast their result to at least int. . . .

I think the idea is that you don't as a routine use bytes or chars or shorts for arithmetic. You use ints, longs, doubles, and floats. Though, if I had my way, I would consign the float datatype to some sort of cyber‑Hades.
So the JVM automatically promotes the operands to whichever of the latter four datatypes is wide enough to contain the result. [edit] This promotion (or upcast) is done before the arithmetic. I went through the JLS (=Java® Language Specification) starting here, and it appears that such promotion is applied for every operator specifically taking (primitive) numbers. There are different promotion rules for ?:.
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic