Abdulrehmangriffith
Posts: 5
Joined: Mon Apr 09, 2018 8:33 am

String Array convert into int array

Wed Apr 11, 2018 12:29 am

I’m doing serial communication with RPI to PC. I have couple of files contain integer data such as [ 37098, 34509, .... ] saved in PC. I succeed in transfer file data from pc to rpi but it transferred one by one like 3,7,0,9,8. I tried it doing first convert it into string then apply atoi( ) function but atoi function remove space and next line and convert it into 00. And the Rx pin is 8 bit. It will transfer just 255 and then again start with 0,1,2,3. So I want to get my data as I’m sending.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: String Array convert into int array

Wed Apr 11, 2018 1:20 am

Sounds like the issue is the method of transferring the data. If you just send it as raw data, directly from the source array (buffer) to the destination array then there would be no issue.

I am not sure what method you are using for serial communication, though for the purpose of example lets say you have a function void *SerSend(void *buff, int count) that sends data byte by byte on the source end, and a function void *SerReceive(void *buff) on the destination side. Also say that the send function sends the total byte count to be sent at the beginning so that the SerReceive() function knows how many bytes are coming (as this is how I would do it, yours may be a bit different).

Under the above parameters, if your source array of integers is defined int array[4096] and so is the destination, then on the source side you would do something like:

Code: Select all

//.... previous code ....
     SerSend(array, 4096 * sizeof(int));
//.... following code ....
and on the destination side:

Code: Select all

//... Previous code ...
  SerReceive(array);
// ... Following Code ...
and the result would be the same as the source, and as both platforms are the same endiannes all is well.

NOTE:
The above assumes that the data is in its raw integer form on the source side.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

Abdulrehmangriffith
Posts: 5
Joined: Mon Apr 09, 2018 8:33 am

Re: String Array convert into int array

Wed Apr 11, 2018 2:41 am

Thanks DavidS, I’m send data using Tera Term software from the PC. I’m using wiringSerial library to receive the data, by using this serialGetchar( ) function I’m receiving one byt at a time means just one char

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: String Array convert into int array

Wed Apr 11, 2018 3:16 am

Abdulrehmangriffith wrote:
Wed Apr 11, 2018 2:41 am
Thanks DavidS, I’m send data using Tera Term software from the PC. I’m using wiringSerial library to receive the data, by using this serialGetchar( ) function I’m receiving one byt at a time means just one char
Ok so you are sending it as ASCII text then?

May I ask why you did not write a simple program on the PC to send it as raw data instead of ASCII text? That would solve the problem.

You can break the string up into multiple strings, using a for loop like:

Code: Select all

  // ... ...
  unsigned char hugeString[65536];
  unsigned char smallStrings[8192][8];
  
  // ... ...
      for (int cnt = 0, sec = 0,dig = 0; cnt <= 65535; ++cnt)
      {
        if (hugeString[cnt] < 0x20)
        {
          ++sec; dig = 0;
        }else{
          smallStrings[sec][dig] = hugeString[cnt];
          ++dig;
  // ... ...
  
This can actually be done a lot more simply, with a single line for statement, if you do not mind using the ?: operator, though a lot of people do not like that so I avoid it in examples usually, though in this case it really does make it more readable:

Code: Select all

  // ... ...
  unsigned char hugeString[65536];
  unsigned char smallStrings[8192][8];
  
  // ... ...
      for (int cnt = 0, sec = 0,dig = 0; cnt <= 65535; ++cnt,
                                           (hugeString[cnt] <= 0x20) ? 
                                           (++sec, dig = 0) : 
                                           (smallString[sec][dig] = hugeString[cnt], ++dig));
  // ... ...
  
This code is completely untested, though should work ok (I am a bit tired at the moment though). This code is just meant to break the long string at white spaces to create a list of numbers in the two dimensional array. You will still need to convert to integer. It does not test for the case of comma, though that would be easy to add in to it, without a significant change.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

Return to “General discussion”