needs help with making hex from two ints?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know this is not much of a avr question, but I hoped someone could shed some light.

I want to get my two ints to be hex.

static void switchBit( int a, int b)
{
//add a and b for make a binary string.
//a=1 and b=0 should be 10 ( 0x2 )
bits = /* my result from above */
PORTC |= ~bits;
}

I tried using char and adding chars and I'm not find a simple clean way to do this. Maybe there is not one, not sure. Still new to all of this, but learning.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What do you actually want, a string or only a combined value?
The PORTC line tells me that you don't really want a string.

bits = (a<<1) + b;
// or
bits = (a<<1) | b;

PS: Are you sure you need/want to use "|=" together with "~"? That is quite unusual.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks to me as if you want to treat those two "ints" as bits.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
It looks to me as if you want to treat those two "ints" as bits.

Jim

- correct.

Quote:
Are you sure you need/want to use "|=" together with "~"? That is quite unusual.
- Sorry thats was a mistake.

bits = (a<<1) + b; 

is what I was looking for thx!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
a=1 and b=0 should be 10 ( 0x2 )
You can combine the least significant bits of the two values thusly:

unsigned char bits = ((a & 0x01) << 1) | (b & 0x01);

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net