unsigned long int to hex problem

I'm trying to take and unsigned long int and turn it into hex form and print it out as a number value.
My function returns a byte data type. So I need larger numbers to be in hex form.

	int stringOne =  int(45, HEX);
	//stringOne = "0x" + stringOne;
	Serial.println(stringOne);

I know this code would give me the hex but the problem is that it is in string form.

Any help would be awesome.

My function returns a byte data type

I don't see any function.

I didn't do good job of explaining my problem, sorry.

I have a function(the one below) that returns a reference to a byte. I want to take a large value, like 2008, and have it printed out in the terminal as 2008. While I know how to get this number in hex form as a string I do not see how I could then print it out to terminal as 2008. To sum it up I want to return 0x7D8 not "0x7D8"

byte& VM::operator[] (const unsigned long address) {
// Serial.println("Inside operator");
// Serial.println(address);
// Serial.println("Inside operator end");
int page = address/PG_SIZE;
int offset = address%PG_SIZE;
//Serial.println("offset" );
//Serial.println(page);

int in_m = in_memory(page);
int room = is_room();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Serial.println("room: ");
// Serial.println(room);

if(in_m != -1){//table already in memory, write data
return main_mem[in_m * PG_SIZE + offset];
}else if(room != -1){//whoo hoo room in table, room is the index
pg_table[room] = page;//record new page in table
return main_mem[room * PG_SIZE + offset];
}else if(room == -1){
Serial.println("test nooooooooooo room");
//no room in table and page not in memory. switch out pages from SRAM
// Serial.println("page,oldest page");
// Serial.println(page);
// Serial.println(oldest_pg);
switch_pages(page,oldest_pg);
int old_temp = oldest_pg;//need to return value before incremented
//ensure that oldest page is the victim(FIFO)
if(oldest_pg < TABLE_SIZE - 1) {
oldest_pg++;
}else{
oldest_pg = 0;
}
//this should always be the first value in array
//Serial.println(main_mem[(oldest_pg * PG_SIZE + offset)]);
//Serial.println((oldest_pg * PG_SIZE + offset));
// Serial.println("old_temp****************************************");
// Serial.println(pg_table[old_temp]);
// Serial.println("old_temp end****************************************");
return main_mem[(old_temp * PG_SIZE + offset)];
}
}

shadow copies?

you should keep the variable just 2008 // int x = 2008

if you want to make a String out of it in HEX derive it from the original value // String s = ...(x)...?
if you want to print it // Serial.println(x, DEC);

The idea is to have one master value and create other representations when needed

(Or I just do not understand the problem)

The function takes in a unsigned long int and returns a byte. Returning 2008 as a byte will not print 2008 but returning it in hex form will print out 2008.

Returning 2008 as a byte will not print 2008

log22008 = 10.97.
Returning it in a byte simply isn't feasible

RetroCheck:
I didn't do good job of explaining my problem, sorry.

I have a function(the one below) that returns a reference to a byte. I want to take a large value, like 2008, and have it printed out in the terminal as 2008. While I know how to get this number in hex form as a string I do not see how I could then print it out to terminal as 2008. To sum it up I want to return 0x7D8 not "0x7D8"

You still haven't done a good job. Do you understand the difference between
an integer (mathematically), a computer variable representing an integer, a string
(mathematically) and a computer variable representing a string?

Not sure what your talking about. For example, 257 will print 1 due to the limits of the byte data type. If 257 was in hex form Serial.println would print 257 and not 1. I have tried both ways and it works.

I have programmed in C for some time and strtol() should be a simple fix but it is not working here under the arduino umbrella.

I have programmed in C for some time

But do you understand data representations?

apparently not, what am I missing here?

MarkT:
You still haven't done a good job. Do you understand the difference between
an integer (mathematically), a computer variable representing an integer, a string
(mathematically) and a computer variable representing a string?

Actually I think I have. If I have a misunderstanding about something then it should be clear by now. Your comment was not helpful at all.

RetroCheck:
Not sure what your talking about. For example, 257 will print 1 due to the limits of the byte data type. If 257 was in hex form Serial.println would print 257 and not 1. I have tried both ways and it works.

A byte cannot hold the value 257. It doesn't matter if it's hex or not. There's no way of magically cramming 257 things into a space that can only hold 256 things.

In your program code, you can write constants in decimal, octal or hexadecimal. This is just a convenience for the programmer. It doesn't change the value which is stored.

int a;
void setup() { 
  Serial.begin(9600);
  a = 257;
  Serial.print(a); //prints 257
  Serial.print(a, HEX), //prints 101

  a = 0x101;
  Serial.print(a); //prints 257
  Serial.print(a, HEX), //prints 101
}

Formatting something as hex is only for input or output, where a person has to read it. The computer has its own internal representation of numbers which you don't normally need to worry about, except when you choose a datatype too small to store the numbers you want.

I see. Thanks that helped a lot. I appreciate it.

RetroCheck:
Not sure what your talking about. For example, 257 will print 1 due to the limits of the byte data type. If 257 was in hex form Serial.println would print 257 and not 1. I have tried both ways and it works.

While all of that is true, readers should also be aware that the valid decimal range for byte is 0 to 255 inclusive!
Which is 256 unique values. Assigning decimal 256 to a byte rolls over to 0.

Instead of wasting time writing random garbage, you should find a book or online tutorial and get some clu7e about what you are doing.

Almost everything you have said in your thread here is complete nonsense.

Not sure what your talking about. For example, 257 will print 1 due to the limits of the byte data type. If 257 was in hex form Serial.println would print 257 and not 1. I have tried both ways and it works.

I am forced to disagree with reply #15's "while all of that is true, .l.. "

None of that is true.

For example, 257 will print 1 due to the limits of the byte data type.

A byte cannot have 257 put into it. You might have tried to put 257 into it, but there will only be a 1 in that byte. And what is in there, is what gets printed. The "limitation" of the byte data type occurs when you put the value in, not when you tried to print it out. There was never 257 in there at all.

If 257 was in hex form Serial.println would print 257 and not 1.

What do you even mean by "in hex form". If you mean, a binary number, 257 still won't fit in a byte. You can call it 101 hex, if you like, and it still won't fit in a byte. If you mean, the ascii characters '1' '0' and '1', which would represent a printable representation of the number 257, or 101 "in hex form", it still won't fit in a byte.

And if you had the characters '1' '0' and '1' in hex form, then Serial.print( ) is not going to convert that back to 257 for you.

I have tried both ways and it works.

Yeah right.

wrote this one some time ago, to print hex with leading zeros

void printHex(uint32_t v, uint8_t digits)
{
  Serial.print("0x");
  for (int i = digits-1; i >= 0; --i)
  {
    Serial.print( (v >> (i*4)) & 0xF, HEX);
  }
}

RetroCheck:
Not sure what your talking about. For example, 257 will print 1 due to the limits of the byte data type. If 257 was in hex form Serial.println would print 257 and not 1. I have tried both ways and it works.

I have programmed in C for some time and strtol() should be a simple fix but it is not working here under the arduino umbrella.

You still are not making any sense to me... What on earth does strtol have to do with
this thread?

The integer 257 is not in any form, its just a mathematical number, it cannot be
in decimal, hex, or any form, its abstract. The representation of the integers
0..255 can be held in a byte. Pass a byte to println and only one of the representable
integers can be printed in whatever base because deep down in the hardware a byte
is 8 SRAM cells in the processor's register file or RAM block.

To be slightly more correct we don't actually really know what the representation
is, just that the processor acts as if 8 bits are used (the &, |, << and >> operators
make this explicit). So its basically right to say C's integer variables are binary.
Decimal and Hexadecimal representations are strings of ASCII characters on
The Arduino, and can represent much larger values than byte, int, long variables,
but converting a byte to a string cannot overcome the fact that the byte's value
represents one of the integers in the range 0..255

The C language will simply truncate when moving an int value to a byte variable
for instance, its an operation that loses information.