Converting int array to 8bit int

Hello
I have two 32 bit int arrays and i need to convert them in 8 8bit int's.

11111111110111110111111111111101 11111110111111111111101110111111
So this is for example the current value of my two arrays. what i want is this:

int one = 11111111
int two = 11011111
int three = 01111111
int four = 11111101

This is the first 32 bit array (left one) cut into 4 bytes and then stored them in an int. (Same needed for array two).

The names of my two 32 bit arrays are shiftoutone[32] and shiftouttwo[32].
I know its easy to print them using a for loop and i can see that the values are correct by using the serial monitor. I just can't put them into 4 int's.

Hope someone can help me :slight_smile:
Thank You

If you know how to print each element of the array then take a look at the bitWrite() function. Pick each element out of the array and set the corresponding bit in a variable.

byte getByte(int index)
{
  byte result = 0;
  index *= 8;  // Change byte index to bit index

  if (index >= 64)
    return 0;

  for (byte i= index; i<index+8; i++)
  {
    result <<= 1;
    result +=  (i < 32) ? shiftoutone[i] : shiftouttwo[i-32];
  }
  return result;
}

void setup()
{
int one =    getByte(0);   
int two =    getByte(1);   
int three =  getByte(2);   
int four =    getByte(3);   
int five =    getByte(4);   
int six =    getByte(5);   
int seven =  getByte(6);   
int eight =    getByte(7);
}

By using this code i get an error.

a function-definition is not allowed here before '{' token. Its the first { right after byte getByte(int index)

Mithrintia:
I have two 32 bit int arrays and i need to convert them in 8 8bit int's.

Could this possibly be for sending to another device?

Yes its for a 32 bit shift register and im using the shiftOut function.

Just need some rearranging, putting the function after loop(), and declaring two arrays

byte shiftoutone[32];
byte shiftouttwo[32];


void setup()
{
int one =    getByte(0);   
int two =    getByte(1);   
int three =  getByte(2);   
int four =    getByte(3);   
int five =    getByte(4);   
int six =    getByte(5);   
int seven =  getByte(6);   
int eight =    getByte(7);
}
void loop(){}


byte getByte(int index)
{
  byte result = 0;
  index *= 8;  // Change byte index to bit index


  if (index >= 64)
    return 0;


  for (byte i= index; i<index+8; i++)
  {
    result <<= 1;
    result +=  (i < 32) ? shiftoutone[i] : shiftouttwo[i-32];
  }
  return result;
}

Being a hardware engineer, I'd have been a bit more brute-fortish

#include <SPI.h>


unsigned long element1; // 4bytes, 32 bit number
unsigned long element2; // 4 byte


byte x;
byte SSpin = 10;


void setup() {
  SPI.begin();
}


void loop() {
  digitalWrite (SSpin, LOW);
  x = element1 & 0x000f;
  SPI.transfer(x);
  x = element1 & (0x00f0 >> 8);
  SPI.transfer(x);
  x = element1 & (0x0f00 >> 16);
  SPI.transfer(x);
  x = element1 & (0xf000 >> 24);
  SPI.transfer(x);
  digitalWrite (SSpin,  HIGH);
  // repeat for element 2
}

Your shift values are wrong.

So i've put everything in the right order. how can i use my 8 ints in the loop now?

Is there a way to do this inside the loop?

Yeah, sometimes it takes a couple tries

#include <SPI.h>


unsigned long element1 = 0x11223344; // 4bytes, 32 bit number
unsigned long element2; // 4 byte




byte x;
byte SSpin = 10;


void setup() {
  SPI.begin();
  Serial.begin(9600);
}


void loop() {
  digitalWrite (SSpin, LOW);
  x = (element1 & 0xff000000) >> 24;
  Serial.print(x, HEX);
  Serial.print(" ");
  SPI.transfer(x);
  x = (element1 & 0x00ff0000) >> 16;
  Serial.print(x, HEX);
  Serial.print(" ");
  SPI.transfer(x);
  x = (element1 & 0x0000ff00) >> 8;
  Serial.print(x, HEX);
  Serial.print(" ");
  SPI.transfer(x);
  x = element1 & 0x000000ff;
  Serial.print(x, HEX);
  Serial.print(" ");
  SPI.transfer(x);
  digitalWrite (SSpin,  HIGH);
  // repeat for element 2
  while(1);
}

Result

11 22 33 44

Use the results where ever you want, it's your code/project.

Mithrintia:
By using this code i get an error.
a function-definition is not allowed here before '{' token. Its the first { right after byte getByte(int index)

That is usually caused by a missing '}' to end the previous function. You can't define a function inside another function so the compiler complains.

Ok
I didn't say this but im driving 6 nixie tubes with the shift register.
Everything works so far. The Shown time is correct but everytime the 2 second digit is changing (every second...) the tubes start to flicker. I guess this is because of the bits shifting through rhe register? Im using a NEO 6M GPS Chip to get the time. This time is stored in an DS1307 RTC. I simple read out the rtc and convert the given time to the 8 bytes which i need for the shift register. While the Tube flickers the current time is displayed correctly... but all other cathodes which should be off in this moment have a very dark glowing flicker...

It's hard to describe, sorry

I'll post my code so maybe someone can help :slight_smile:

By the way... I used the bitWrite methode to convert my arrays into bytes. UKHeliBob came up with this and it was the easiest way for me... maybe not the best way but im new to C and arduino.

#define RXPin 3
#define TXPin 2

#define DATA 6
#define CLOCK 4
#define LATCH 5
#define BLANK 7

#define DATATWO 11
#define CLOCKTWO 10
#define LATCHTWO 8
#define BLANKTWO 9

#include <Wire.h>
#include <RTClib.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

RTC_DS1307 RTC;
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);

void setup() {

Wire.begin();
RTC.begin();
Serial.begin(9600);
gpsSerial.begin(9600);

pinMode(DATA, OUTPUT); // Data is pin 11
pinMode(LATCH, OUTPUT); // Latch is pin 10, FALLING edge copies shift register to latches
pinMode(CLOCK, OUTPUT); // Clock is pin 13, FALLING edge clocks in a bit from DATA
pinMode(BLANK, OUTPUT); // BLANK (Blank) is pin 9

pinMode(DATATWO, OUTPUT); // Data is pin 11
pinMode(LATCHTWO, OUTPUT); // Latch is pin 10, FALLING edge copies shift register to latches
pinMode(CLOCKTWO, OUTPUT); // Clock is pin 13, FALLING edge clocks in a bit from DATA
pinMode(BLANKTWO, OUTPUT); // BLANK (Blank) is pin 9

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(F(DATE), F(TIME)));
}
}

void loop() {
displayClock();
}

void displayClock() {
DateTime now = RTC.now();

int GMT_OFFSET = 2; //ADD BUTTON HERE
int hours = now.hour() + GMT_OFFSET;
int hourone = hours /10;
int hourtwo = hours %10 ;
int minuteone = now.minute() /10;
int minutetwo = now.minute() %10;
int secondone = now.second() /10;
int secondtwo = now.second() %10;

int colonOneUp = 1;
int colonOneDown = 1;
int colonTwoUp = 1;
int colonTwoDown = 1;

int shiftone[10] = {1,1,1,1,1,1,1,1,1,1};
shiftone[9-secondtwo] = 0;

int shifttwo[10] = {1,1,1,1,1,1,1,1,1,1};
shifttwo[9-secondone] = 0;

int shiftthree[10] = {1,1,1,1,1,1,1,1,1,1};
shiftthree[9-minutetwo] = 0;

int shiftfour[10] = {1,1,1,1,1,1,1,1,1,1};
shiftfour[9-minuteone] = 0;

int shiftfive[10] = {1,1,1,1,1,1,1,1,1,1};
shiftfive[9-hourtwo] = 0;

int shiftsix[10] = {1,1,1,1,1,1,1,1,1,1};
shiftsix[9-hourone] = 0;

int shiftoutone[32] = {colonOneUp,colonOneDown,shiftsix[0],shiftsix[1],shiftsix[2],shiftsix[3],shiftsix[4],shiftsix[5],shiftsix[6],shiftsix[7],shiftsix[8],shiftsix[9],shiftfive[0],shiftfive[1],shiftfive[2],shiftfive[3],shiftfive[4],shiftfive[5],shiftfive[6],shiftfive[7],shiftfive[8],shiftfive[9],shiftfour[0],shiftfour[1],shiftfour[2],shiftfour[3],shiftfour[4],shiftfour[5],shiftfour[6],shiftfour[7],shiftfour[8],shiftfour[9]};
int shiftouttwo[32] = {colonTwoUp,colonTwoDown,shiftthree[0],shiftthree[1],shiftthree[2],shiftthree[3],shiftthree[4],shiftthree[5],shiftthree[6],shiftthree[7],shiftthree[8],shiftthree[9],shifttwo[0],shifttwo[1],shifttwo[2],shifttwo[3],shifttwo[4],shifttwo[5],shifttwo[6],shifttwo[7],shifttwo[8],shifttwo[9],shiftone[0],shiftone[1],shiftone[2],shiftone[3],shiftone[4],shiftone[5],shiftone[6],shiftone[7],shiftone[8],shiftone[9]};

byte sone = 0b11111111;

bitWrite(sone, 0, shiftouttwo[31]);
bitWrite(sone, 1, shiftouttwo[30]);
bitWrite(sone, 2, shiftouttwo[29]);
bitWrite(sone, 3, shiftouttwo[28]);
bitWrite(sone, 4, shiftouttwo[27]);
bitWrite(sone, 5, shiftouttwo[26]);
bitWrite(sone, 6, shiftouttwo[25]);
bitWrite(sone, 7, shiftouttwo[24]);

byte stwo = 0b11111111;

bitWrite(stwo, 0, shiftouttwo[23]);
bitWrite(stwo, 1, shiftouttwo[22]);
bitWrite(stwo, 2, shiftouttwo[21]);
bitWrite(stwo, 3, shiftouttwo[20]);
bitWrite(stwo, 4, shiftouttwo[19]);
bitWrite(stwo, 5, shiftouttwo[18]);
bitWrite(stwo, 6, shiftouttwo[17]);
bitWrite(stwo, 7, shiftouttwo[16]);

byte sthree = 0b11111111;

bitWrite(sthree, 0, shiftouttwo[15]);
bitWrite(sthree, 1, shiftouttwo[14]);
bitWrite(sthree, 2, shiftouttwo[13]);
bitWrite(sthree, 3, shiftouttwo[12]);
bitWrite(sthree, 4, shiftouttwo[11]);
bitWrite(sthree, 5, shiftouttwo[10]);
bitWrite(sthree, 6, shiftouttwo[9]);
bitWrite(sthree, 7, shiftouttwo[8]);

byte sfour = 0b11111111;

bitWrite(sfour, 0, shiftouttwo[7]);
bitWrite(sfour, 1, shiftouttwo[6]);
bitWrite(sfour, 2, shiftouttwo[5]);
bitWrite(sfour, 3, shiftouttwo[4]);
bitWrite(sfour, 4, shiftouttwo[3]);
bitWrite(sfour, 5, shiftouttwo[2]);
bitWrite(sfour, 6, shiftouttwo[1]);
bitWrite(sfour, 7, shiftouttwo[0]);

byte sfive = 0b11111111;

bitWrite(sfive, 0, shiftoutone[31]);
bitWrite(sfive, 1, shiftoutone[30]);
bitWrite(sfive, 2, shiftoutone[29]);
bitWrite(sfive, 3, shiftoutone[28]);
bitWrite(sfive, 4, shiftoutone[27]);
bitWrite(sfive, 5, shiftoutone[26]);
bitWrite(sfive, 6, shiftoutone[25]);
bitWrite(sfive, 7, shiftoutone[24]);

byte ssix = 0b11111111;

bitWrite(ssix, 0, shiftoutone[23]);
bitWrite(ssix, 1, shiftoutone[22]);
bitWrite(ssix, 2, shiftoutone[21]);
bitWrite(ssix, 3, shiftoutone[20]);
bitWrite(ssix, 4, shiftoutone[19]);
bitWrite(ssix, 5, shiftoutone[18]);
bitWrite(ssix, 6, shiftoutone[17]);
bitWrite(ssix, 7, shiftoutone[16]);

byte sseven = 0b11111111;

bitWrite(sseven, 0, shiftoutone[15]);
bitWrite(sseven, 1, shiftoutone[14]);
bitWrite(sseven, 2, shiftoutone[13]);
bitWrite(sseven, 3, shiftoutone[12]);
bitWrite(sseven, 4, shiftoutone[11]);
bitWrite(sseven, 5, shiftoutone[10]);
bitWrite(sseven, 6, shiftoutone[9]);
bitWrite(sseven, 7, shiftoutone[8]);

byte seight = 0b11111111;

bitWrite(seight, 0, shiftoutone[7]);
bitWrite(seight, 1, shiftoutone[6]);
bitWrite(seight, 2, shiftoutone[5]);
bitWrite(seight, 3, shiftoutone[4]);
bitWrite(seight, 4, shiftoutone[3]);
bitWrite(seight, 5, shiftoutone[2]);
bitWrite(seight, 6, shiftoutone[1]);
bitWrite(seight, 7, shiftoutone[0]);

digitalWrite(BLANK, LOW);
digitalWrite(BLANK, HIGH);
digitalWrite(BLANKTWO, LOW);
digitalWrite(BLANKTWO, HIGH);

shiftOut(DATA, CLOCK, MSBFIRST, sfour); //Byte erste 0 wird weggekürzt flackern?
shiftOut(DATA, CLOCK, MSBFIRST, sthree);
shiftOut(DATA, CLOCK, MSBFIRST, stwo);
shiftOut(DATA, CLOCK, MSBFIRST, sone);

shiftOut(DATATWO, CLOCKTWO, MSBFIRST, seight); //Byte erste 0 wird weggekürzt flackern?
shiftOut(DATATWO, CLOCKTWO, MSBFIRST, sseven);
shiftOut(DATATWO, CLOCKTWO, MSBFIRST, ssix);
shiftOut(DATATWO, CLOCKTWO, MSBFIRST, sfive);

digitalWrite(LATCH, LOW);
digitalWrite(LATCHTWO, LOW);
digitalWrite(LATCH, HIGH);
digitalWrite(LATCHTWO, HIGH);

updateTime();
}

void updateTime() {

while (gpsSerial.available() > 0) {
if (gps.encode(gpsSerial.read())) {

DateTime now = RTC.now();
RTC.adjust(DateTime(gps.date.year(),gps.date.month(),gps.date.day(),gps.time.hour(),gps.time.minute(),gps.time.second()));

/*
if (gps.date.isValid()) {
if (gps.date.day() < 10) Serial.print(F("0"));
Serial.print(gps.date.day());
Serial.print("/");
if (gps.date.month() < 10) Serial.print(F("0"));
Serial.print(gps.date.month());
Serial.print("/");
Serial.println(gps.date.year());
} else {
Serial.println("Not Available");
}

if (gps.time.isValid()) {
int currenthour = gps.time.hour() + 2;
if (currenthour < 10) Serial.print(F("0"));
Serial.print(currenthour);
Serial.print(":");
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
} else {
Serial.println("Not Available");
}
Serial.println();
*/
}
}
}

Please read the how to use this forum sticky post it will tell you about posting code.

That code is quite bad. Just on the surface take this

bitWrite(sone, 0, shiftouttwo[31]);
    bitWrite(sone, 1, shiftouttwo[30]);
    bitWrite(sone, 2, shiftouttwo[29]);
    bitWrite(sone, 3, shiftouttwo[28]);
    bitWrite(sone, 4, shiftouttwo[27]);
    bitWrite(sone, 5, shiftouttwo[26]);
    bitWrite(sone, 6, shiftouttwo[25]);
    bitWrite(sone, 7, shiftouttwo[24]);

You can replace it with

 for(int i = 0; i < 8; i++) {
   BitWrite(some, i, shiftouttwo[31-i];
}

Same sort of thing for the other great blacks of repetition.

I guess this is because of the bits shifting through rhe register?

You should not see any, I can’t see where you change the latch pin. From the sound of that error you are not closing the latch, then doing the shift and then opening the latch.

I knew its not good i said im new to this. I open and close the latch behind the shiftout functions. The latch is inverted.

digitalWrite(BLANK, LOW);
    digitalWrite(BLANK, HIGH);
    digitalWrite(BLANKTWO, LOW);
    digitalWrite(BLANKTWO, HIGH);
      
    shiftOut(DATA, CLOCK, MSBFIRST, sfour);         
    shiftOut(DATA, CLOCK, MSBFIRST, sthree);
    shiftOut(DATA, CLOCK, MSBFIRST, stwo);
    shiftOut(DATA, CLOCK, MSBFIRST, sone);
 
    shiftOut(DATATWO, CLOCKTWO, MSBFIRST, seight);       
    shiftOut(DATATWO, CLOCKTWO, MSBFIRST, sseven);
    shiftOut(DATATWO, CLOCKTWO, MSBFIRST, ssix);
    shiftOut(DATATWO, CLOCKTWO, MSBFIRST, sfive);

    digitalWrite(LATCH, LOW);
    digitalWrite(LATCHTWO, LOW);
    digitalWrite(LATCH, HIGH);
    digitalWrite(LATCHTWO, HIGH);

I gess it has something to do with the shiftout because the tube flickers 4 times (4 bytes shifted out) and stops flickering for half a second and then it repeats.

The latch is inverted.

What do you mean by that?

What shift register are you using and how is it wired up. Do you have decoupling capacitors on every shift register chip?

It's the HV5622 shift register and yes i have decoupling capacitors on every shift register.
You can find the datasheet here: