-1

I've found a few answers about this but none of them seem to apply to my issue.

I'm using the NDK and C++ is expecting an unsigned char array of 1024 elements, so I need to create this in java to pass it as a parameter.

The unsigned char array is expected to contain both numbers and characters.

I have tried this:

byte[] lMessage = new byte[1024];
lMessage[4] = 'a'; 

The problem is that then the 4th element gets added as a numerical value instead of maintaining the 'a' character.

I have also tried

char[] lMessage = new char[1024];
lMessage[4] = 'a'; 

While this retains the character, it does duplicate the amount of bytes in the array from 8 to 16.

I need the output to be a 8 bit ASCII unsigned array.

Any suggestions? Thanks.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • 1
    Generally, how you have to do this in Java is treat signed values as unsigned, and do the conversion yourself. `byte[]` is correct; you will just have to do conversion to treat that byte as a character. – Louis Wasserman Feb 19 '15 at 20:23
  • Where are you seeing that your char is converted to a numeric value? In your Java code or on the C++ side? – MarsAtomic Feb 19 '15 at 20:23
  • 1
    `lMessage[4]` is the `5th` item in the array, not the 4th item. – PaulMcKenzie Feb 19 '15 at 20:23
  • @MarsAtomic I see this in the java side in the debugger – TooManyEduardos Feb 19 '15 at 20:24
  • @Wasserman how would I do this? – TooManyEduardos Feb 19 '15 at 20:24
  • 2
    `The problem is that then the 4th element gets added as a numberical value instead of maintaining the 'a' character.` But 'a' *is* numeric. It's ASCII value is 97. So I don't understand what the problem is. What you're seeing in the debugger is the debugger's interpretation of the data. The 'a' is still there. Heck, maybe flip a switch in the debugger, and magically the 'a' will be shown, who knows. But it isn't an issue that the 'a' (97) is changed to some other value. – PaulMcKenzie Feb 19 '15 at 20:30
  • This may help: http://www.asciitable.com/ – Zéychin Feb 19 '15 at 21:02

2 Answers2

2

It is wrong to say that the element "gets added as a numerical value". The only thing that you can say for sure is that it gets added as electrostatic charges in eight cells of your RAM.

How you choose to represent those eight bits (01100001) in order to visualize them has little to do with what they really are, so if you choose to see them as a numerical value, then you might be tricked into believing that they are in fact a numerical value. (Kind of like a self-fulfilling prophecy (wikipedia).)

But in fact they are nothing but 8 electrostatic charges, interpretable in whatever way we like. We can choose to interpret them as a two's complement number (97), we can choose to interpret them as a binary-coded decimal number (61), we can choose to interpret them as an ASCII character ('a'), we can choose to interpret them as an x86 instruction opcode (popa), the list goes on.

The closest thing to an unsigned char in C++ is a byte in java. That's because the fundamental characteristic of these small data types is how many bits long they are. Chars in C++ are 8-bit long, and the only type in java which is also 8-bits long is the byte.

Unfortunately, a byte in java tends to be thought of as a numerical quantity rather than as a character, so tools (such as debuggers) that display bytes will display them as little numbers. But this is just an arbitrary convention: they could have just as easily chosen to display bytes as ASCII (8-bit) characters, and then you would be seeing an actual 'a' in byte[] lMessage[4].

So, don't be fooled by what the tools are showing, all that counts is that it is an 8-bit quantity. And if the tools are showing 97 (0x61), then you know that the bit pattern stored in those 8 memory cells can just as legitimately be thought of as an 'a', because the ASCII code of 'a' is 97.

So, finally, to answer your question, what you need to do is find a way to convert a java string, which consists of 16-bit unicode characters, to an array of ASCII characters, which would be bytes in java. You can try this:

String s = "TooManyEduardos";
byte[] bytes = s.getBytes("US-ASCII");

Or you can read the answers to this question: Convert character to ASCII numeric value in java for more ideas.

Community
  • 1
  • 1
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

Will work for ASCII chars

lMessage[4] = new String('a').getBytes()[0];
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35