Pages: [1] 2   Go Down
Author Topic: hex/byte to int conversion  (Read 1645 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Online
Brattain Member
*****
Karma: 509
Posts: 31471
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Full Member
***
Karma: 0
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Online
Brattain Member
*****
Karma: 509
Posts: 31471
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Online
Brattain Member
*****
Karma: 509
Posts: 31471
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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.
Remember bed is your friend.  smiley

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.  smiley-razz
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This converts one character.  My post contained 2 functions.  The first one is the same as this function.  The second one takes a string of 2 characters and calls the first function for each character.

Keep my second function, but replace my first one (h2d) with this one.
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.

Pages: [1] 2   Go Up
Jump to: