0
Offline
Full Member
Karma: 0
Posts: 120
Arduino rocks
|
 |
« on: June 07, 2012, 03:17:54 pm » |
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
Online
Brattain Member
Karma: 137
Posts: 19024
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: June 07, 2012, 03:23:24 pm » |
a char[1] containing "{" (ie as an ascii byte) 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.
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 274
Posts: 25481
Solder is electric glue
|
 |
« Reply #2 on: June 07, 2012, 03:23:45 pm » |
int v1 = int(char[2] ); int v2 = int(char[1] );
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #3 on: June 07, 2012, 03:25:14 pm » |
[quote]- a char[2] containing "7B" (ie as hex)[/quote]
[code] char *foo="{";
int bar = (int)char[0];
- a char[2] containing "7B" (ie as hex) 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
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19024
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: June 07, 2012, 03:41:47 pm » |
- 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 char x[0] = '7'; char x [1] = 'B';
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
California
Offline
Edison Member
Karma: 38
Posts: 1850
|
 |
« Reply #5 on: June 07, 2012, 03:45:52 pm » |
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: 120
Arduino rocks
|
 |
« Reply #6 on: June 07, 2012, 04:07:20 pm » |
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
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #7 on: June 07, 2012, 04:12:14 pm » |
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
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 274
Posts: 25481
Solder is electric glue
|
 |
« Reply #8 on: June 07, 2012, 04:26:51 pm » |
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. 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: 1
Posts: 253
Arduino good init
|
 |
« Reply #9 on: June 07, 2012, 04:33:09 pm » |
Mike why is that last method better than using casting? Is their a difference in how it complies?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #10 on: June 07, 2012, 04:33:21 pm » |
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
|
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #11 on: June 07, 2012, 04:36:18 pm » |
Oh... and integers to store a single byte...? On an 8-bit processor...?!
How wasteful...
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 274
Posts: 25481
Solder is electric glue
|
 |
« Reply #12 on: June 07, 2012, 04:39:33 pm » |
Mind if I steal your little snippet there? Be my guest. 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. but at 3AM in the morning Get some sleep. Remember bed is your friend. 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: 120
Arduino rocks
|
 |
« Reply #13 on: June 07, 2012, 05:17:06 pm » |
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
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #14 on: June 07, 2012, 05:21:29 pm » |
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
|
|
|
|
|
|