Pages: [1] 2   Go Down
 Author Topic: hex/byte to int conversion  (Read 1498 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Full Member
Karma: 0
Posts: 124
Arduino rocks
 « on: June 07, 2012, 03:17:54 pm » Bigger Smaller Reset

Hey, quick question.  How do I convert both of these into an int (which should have a value of 123) -

- a char[2] containing "7B"   (ie as hex)

and

- a char[1] containing "{"   (ie as an ascii byte)

thanks!
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23679
I don't think you connected the grounds, Dave.
 « Reply #1 on: June 07, 2012, 03:23:24 pm » Bigger Smaller Reset

Quote
a char[1] containing "{"   (ie as an ascii byte)
Code:
int x = (int) char [1];
For brevity, you can omit the cast.
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester (England England)
Online
Brattain Member
Karma: 486
Posts: 30726
Solder is electric glue
 « Reply #2 on: June 07, 2012, 03:23:45 pm » Bigger Smaller Reset

Code:
int v1 = int(char[2] );
int v2 = int(char[1] );
 Logged

UK
Offline
Karma: 90
Posts: 3909
 « Reply #3 on: June 07, 2012, 03:25:14 pm » Bigger Smaller Reset

Code:
[quote]- a char[2] containing "7B"   (ie as hex)[/quote]

[code]
char *foo="{";

int bar = (int)char[0];

Quote
- a char[2] containing "7B"   (ie as hex)

Code:
unsigned char h2d(char hex)
{
switch(hex)
{
case '0':       return 0;
case '1':       return 1;
case '2':       return 2;
case '3':       return 3;
case '4':       return 4;
case '5':       return 5;
case '6':       return 6;
case '7':       return 7;
case '8':       return 8;
case '9':       return 9;
case 'a':       return 10;
case 'A':       return 10;
case 'b':       return 11;
case 'B':       return 11;
case 'c':       return 12;
case 'C':       return 12;
case 'd':       return 13;
case 'D':       return 13;
case 'e':       return 14;
case 'E':       return 14;
case 'f':       return 15;
case 'F':       return 15;
}
return 0;
}

unsigned char h2d2(char *hex)
{
return (h2d(hex[0])<<4) | h2d(hex[1]);
}

char *foo="7B";

int bar = h2d2(foo);

Or something similar.
[/code]
 « Last Edit: June 07, 2012, 04:14:14 pm by majenko » Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23679
I don't think you connected the grounds, Dave.
 « Reply #4 on: June 07, 2012, 03:41:47 pm » Bigger Smaller Reset

Quote
- a char[2] containing 0x7B   (ie as hex)
You don't need two chars to hold two hex digits, unless they're stored as their character representation
Code:
char x[0] = '7';
char x [1] = 'B';
 Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline
Karma: 82
Posts: 3123
 « Reply #5 on: June 07, 2012, 03:45:52 pm » Bigger Smaller Reset

Quote
You don't need two chars to hold two hex digits, unless they're stored as their character representation
I misinterpreted what he was doing, I thought he was talking about the hex value being in a single char.
 Logged

0
Offline
Full Member
Karma: 0
Posts: 124
Arduino rocks
 « Reply #6 on: June 07, 2012, 04:07:20 pm » Bigger Smaller Reset

Thanks guys, a stunning response!

Yes, "they're stored as their character representation" because they've just been read as characters from a text file

the ascii example works great.

However majenko's switch code to convert from hex gives me the following error -"invalid conversion from char* to unsigned char*"    Know what's up?

 Logged

UK
Offline
Karma: 90
Posts: 3909
 « Reply #7 on: June 07, 2012, 04:12:14 pm » Bigger Smaller Reset

I copied it from another one of my projects and modified it to suit on the fly.  Experiment with it.  It'll be the passing of the char * variable.  I had it working with two plain char variables originally.

Just remove the "unsigned" bits from the function definitions.  Those were left over from my other project.
 Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Manchester (England England)
Online
Brattain Member
Karma: 486
Posts: 30726
Solder is electric glue
 « Reply #8 on: June 07, 2012, 04:26:51 pm » Bigger Smaller Reset

majenko - I thought you were having a joke with that code. But if you were serious then this is much better than using case statements.

Code:
int convertFromHex(int ascii){
if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
return(ascii & 0xf);
}
 Logged

Yorkshire England
Offline
Sr. Member
Karma: 2
Posts: 266
Arduino good init
 « Reply #9 on: June 07, 2012, 04:33:09 pm » Bigger Smaller Reset

Mike why is that last method better than using casting? Is their a difference in how it complies?
 Logged

UK
Offline
Karma: 90
Posts: 3909
 « Reply #10 on: June 07, 2012, 04:33:21 pm » Bigger Smaller Reset

I am sure it is, but at 3AM in the morning my brain wasn't up to counting the characters in between the numbers and the letters.  And as I was up at 4AM this morning to drive across the country and back, my brain hasn't yet been up to the task of re-writing it better.

Mind if I steal your little snippet there?  Although, from a pure processing point of view, which is better - a lookup table, or a condition and a mathematical calculation?  (If I were doing it in PIC ASM it would be a lookup table with RETLW's offset by the PC + the W register).
 Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

UK
Offline
Karma: 90
Posts: 3909
 « Reply #11 on: June 07, 2012, 04:36:18 pm » Bigger Smaller Reset

Oh... and integers to store a single byte...? On an 8-bit processor...?!

How wasteful...
 Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Manchester (England England)
Online
Brattain Member
Karma: 486
Posts: 30726
Solder is electric glue
 « Reply #12 on: June 07, 2012, 04:39:33 pm » Bigger Smaller Reset

Quote
Mind if I steal your little snippet there?
Be my guest.

Quote
If I were doing it in PIC ASM it would be a lookup table with RETLW's offset by the PC + the W register)
If it was in assembler my method would be shorter to code, and not take up as much space.  I haven't analised the code to see if it would run faster.

Quote
but at 3AM in the morning
Get some sleep.

Quote
Oh... and integers to store a single byte
Well in the original code I had it return long ints but this was because it was to do with RFID token codes.
 Logged

0
Offline
Full Member
Karma: 0
Posts: 124
Arduino rocks
 « Reply #13 on: June 07, 2012, 05:17:06 pm » Bigger Smaller Reset

Code:
int convertFromHex(int ascii){
if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
return(ascii & 0xf);
}

but I'm trying to convert from a char array, of size 2, containing a hex in string form.... I don't get it
 Logged

UK
Offline
Karma: 90
Posts: 3909