0

So I have a java server running that is expecting data from a client in my arduino. The original type of my data is uint8_t, but i want it as a String. This is how I'm doing my conversion:

String stringData = (char*) data;

where 'data' is an array of type uint8_t. I can print the 'stringData' value and it looks exactly how I want it, but when it gets to my java server it shows up as a little question mark inside of a box. Does anyone know why?

P.S. It works just fine when create a normal string (String stringdata = "123456") So I'm assuming it has to do with the conversion.

Here's the code that pertains:

Arduino:

void arduinoClient(String accountID) {
  if (client.connect(ip, 9876)) {
    client.println(accountID);
    Serial.println("Message sent");
  } else {
    Serial.println("connection failed");
  }
}

Java:

BufferedReader buff = new BufferedReader(new InputStreamReader (socket.getInputStream()));       
String message = buff.readLine();

I'd also be fine with doing the conversion on the java side if it is more convenient (or possible) to do so

dks209
  • 57
  • 11
  • Please specify the type of `data`, and where you get the `String` type from, and give an example of typical `data`. – Cheers and hth. - Alf Apr 15 '15 at 04:09
  • 'data' is an array of type uint8_t. I don't know what you mean by "where you get the String type from". 'data' is initialized by reading an NFC tag – dks209 Apr 15 '15 at 04:33
  • Do you know the character set and encoding of `data`? (Or, are you going to punt with "platform default"?) – Tom Blodget Apr 15 '15 at 04:40
  • `data` gets it's value as so: `memcpy(data, (const uint8_t[]){ 1, 2, 3, 4, 4, 7, 3, 1 }, sizeof data);` if that's what you're asking – dks209 Apr 15 '15 at 04:44
  • @dks209: That doesn't look very zero-terminated to me. Also, these values are usually not graphical characters. In particular, ASCII 7 is the bel character that on output to a terminal produces a sound. – Cheers and hth. - Alf Apr 15 '15 at 07:16

2 Answers2

1

I think the issue has to do with what uint8_t really is. When you convert it to a char* you're changing it to the ACII value of the number, not the actually character that represents the number. This thread explains how to do the conversion. The question mark is Java trying to print an ASCII value that doesn't exist.

Community
  • 1
  • 1
  • Thank you for directing me to that, but i'm having some issues getting it to work. I tried both options listed there. It may be my inexperience with C that is the real issue. I get an error from the first code listed and binary numbers from the second. There was an error with the 'malloc' lines so i had to cast it using `static_cast(malloc( buffer_size ));` I don't know if that caused a problem or not. Is there no simpler way? – dks209 Apr 15 '15 at 04:38
  • I think that's the simplest way, using bit shifting is really the best way to get single digits out of binary numbers. What error are you getting? – The Disco Spider Apr 15 '15 at 04:46
  • `error: cannot convert 'uint8_t (*)[16] {aka unsigned char (*)[16]}' to 'uint8_t* {aka unsigned char*}' for argument '1' to 'char* convert(uint8_t*)'` Which occurs when i try to call the function (this pertains to the first answer's posted code in that thread) `char *theString; theString = convert(&data);` – dks209 Apr 15 '15 at 04:50
  • I believe it's because you are trying to convert an array. Which is a pointer to a series of uint8_t variables. You should try looping through the array and converting it number by number to a char*. – The Disco Spider Apr 15 '15 at 05:00
  • I printed it out in a for loop with this `Serial.print(convert(&data[i]));` so i can see what it's returning, and what i'm getting is a binary representation of the numbers contained in `data` – dks209 Apr 15 '15 at 05:12
  • Could it be that you're using serial.print and not printf("%s", convert(&data))? – The Disco Spider Apr 15 '15 at 14:00
  • It looks like printf() is not a function in arduino, although after following some steps here: http://playground.arduino.cc/Main/Printf I added it and tried as you said, using `Serial.printf("%s", convert(&data[i]));` in a for loop, but it is still printing in binary – dks209 Apr 15 '15 at 20:18
  • Are you sure it's not a string that represents the binary number? – The Disco Spider Apr 15 '15 at 21:23
  • 1
    Right you are! My bad! I created a String in the for loop `message += convert(&data[i]);` and that gave me a string representation of the number i'm wanting in binary, and My java server can understand that. So i'll worry about converting the binary back to a number on that side. Thanks for your help! – dks209 Apr 16 '15 at 03:21
0

see this code:

byte buf[31]; String myString = "MD111111111111111111111111111111111#(40)'"; uint8_t data[] = myString.StringToCharArray(buf,30);

by this code we can convert String to uint_8 in your case you must do:

String stringData = (char*) data; byte buf[256]; uint_8 result = stringOut.StringToCharArray(buf,256);

Mohsen Bahaloo
  • 257
  • 2
  • 2