0

How to convert arrays like this one to a string or at least to letters? And what does 0x00, 0xCA and the like mean?

unsigned char classLoaderClass[] = {
    0xCA, 0xFE, 0xBA, 0xBE, 0x00, 0x00, 0x00, 0x34... };

actually this array is ~400 rows long. If necessary, i can give full version.

UPD: i looked at some parts of java code encoded in this array and realized that i already have this class in decompiled form... how to turn .java into similar array? (I wanna change the code a little)

  • Those are numbers, written in hexadecimal notation. The full version isn't necessary. Where does this data come from, and what do you want the resulting string to contain? – Pete Becker Aug 01 '23 at 13:02
  • 6
    These are numbers not even in the "letter" range, the 0x00 would be interpreted as an end of string. The meaning could be anything from code to pixels that all depends on how this data is used. – Pepijn Kramer Aug 01 '23 at 13:11
  • I am exploring the code of one project from github related to JNI. I think that this array of numbers is a java class, somehow converted into such an array, and the resulting string should contain its code – 16audiopunks_ Aug 01 '23 at 13:17
  • Looks like someone's lazy byte array to me. `0xTT` is hexidecimal (base 16, so `0-9` followed by `A-F`) notation. If this is a compiled Java class (it looks like one! It's notable by the leading `0xCAFEBABE` bytes), then you're already done: you have an array of the raw bytes of the file. Java class files are _not_ text, they're binary, and that's how they should be interpreted. – Rogue Aug 01 '23 at 13:17
  • 2
    CAFEBABE is indeed the magic for a java class. This doesn't work well with `std::string`. The best you can do with these bytes is write them to a file and feed that file to a java executor. – stefaanv Aug 01 '23 at 13:24
  • its possible to get .class file from this? – 16audiopunks_ Aug 01 '23 at 13:31
  • @16audiopunks_ Write it to a file. – molbdnilo Aug 01 '23 at 14:00
  • @16audiopunks_ it is, in fact, the `.class` file right there - in byte form. If you wrote that data byte-for-byte into a file, it would (potentially) be a valid `.class` file. The library may be doing something fancy storing it like this (e.g. `mmap`/`memcpy`) to quickly offload a file, but otherwise that's the heart of it. – Rogue Aug 01 '23 at 14:00
  • that`s exactly what i did FileOutputStream out = new FileOutputStream(new File("C:\\Users\\User\\Desktop\\ClassLoader.class")); out.write(toBytes(classLoaderClass)); out.close(); but file came out corrupted – 16audiopunks_ Aug 01 '23 at 14:23
  • The posted code is C or C++, and your comment's code is Java. If you inspect the output file with HxD (or some other byte viewer) and don't see the same bytes as defined in the question, that's likely the reason why. – Rogue Aug 01 '23 at 15:42
  • Does this answer your question? [Good methods for converting char array buffers to strings?](https://stackoverflow.com/questions/896103/good-methods-for-converting-char-array-buffers-to-strings) – Nimantha Aug 02 '23 at 01:46

1 Answers1

0

You can try like this:

unsigned char classLoaderClass[] = { 0x41, 0x42, 0x43, ... };
std::string str(reinterpret_cast<char*>(&classLoaderClass[0]), std::size(classLoaderClass));
Flaviu_
  • 1,285
  • 17
  • 33