Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] String to hex representation

Status
Not open for further replies.

ponnus

Full Member level 2
Joined
Mar 17, 2011
Messages
142
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,298
Location
Cochin, INDIA
Activity points
2,226
Hi friends,
I want to convert a string number to hex. For example, I need to convert the string "4321" to integer, but in hexadecimal form.i.e, it should be like 0x4321. I tried the strtol(), but it produces the result in decimal form. Can anybody help me,,,,,,,,Please..........
 

Hai can you tell us what is the purpose and where you are going to use this? and from where you are going to get the string 4321. ?

I mean if you are going to use this in a microcontroller from UART, i think you can just minus with 0x30 for every digit

Did you meant something like this?
 

Hi,
yes, I need this for UART data and Now I am doing it by subtracting 0x30 from each character. But is there any function in C to do this? It can be helpful, if we can convert 1234 in decimal form to 0x1234.

Thank you
 

Hi,
yes, I need this for UART data and Now I am doing it by subtracting 0x30 from each character. But is there any function in C to do this? It can be helpful, if we can convert 1234 in decimal form to 0x1234.

Thank you

Hai

I think in C, you can modifiy the char to integer but not hex.

may be you can use some user defined macro as follows

Code:
#define Lo(param) (((char *)&param)[0] - 0x30)
#define Hi(param) (((char *)&param)[1] - 0x30)
#define Higher(param) (((char *)&param)[2] - 0x30)
#define Highest(param) (((char *)&param)[3]- 0x30)

This method of macro is used in mikroC compiler

All the best
 
  • Like
Reactions: ponnus

    ponnus

    Points: 2
    Helpful Answer Positive Rating
ponnus said:
I need this for UART data and Now I am doing it by subtracting 0x30 from each character.
This is like acquiring data out of a string. No hex involved, you are acquiring decimal values. All values having 0x30 substracted from them, are between 0 and 9.


ponnus said:
It can be helpful, if we can convert 1234 in decimal form to 0x1234.
This is a completely different task. This is decimal to bcd convertion.


Please elaborate on what you need. It is better if you do it with an example to make it absolutely clear.

Alexandros
 

Hi,
As I said earlier, I want to convert a string number,such as "1234" to 0x1234. I tried strtol(), but it gives 1234d. I need to simply consider the same string numeral in hexa decimal form. So, I've done it like this:

unsigned short res = 0;
char *buf = "1234";
while(buf)
{
res = (res << 4)+ (*buf - 0x30);
buf++;
}

And,it's working.
Thank You
 

If I understand correctly what you want is

char my_array[4]={'1','2','3','4'};
unsigned int my_int;

my_int = ((my_array[0]-0x30)*4096) | ((my_array[1]-0x30)*256) | ((my_array[2]-0x30)*16) | (my_array[3]-0x30);

// my_int will be 0x1234

This will only work correctly for arrays that contain numbers, if you want to be able to input a..f characters too then the above code needs to be modified but it can still be done.

Alex
 
  • Like
Reactions: ponnus

    ponnus

    Points: 2
    Helpful Answer Positive Rating
Hi,
Yes, that's what I needed. But I am reading the number from a string containing both numbers and alphabets,separated by commas. I've done it as I said it before, and it's working now. Thank you for all your answers
 

I think the following will work for arrays that contain a..f or A..F characters

Code:
unsigned char substr_value;
unsigned int my_int=0;
unsigned char i=0;

for (i=0; i<4; i++)
{
    if ((my_array[i]>64) & (my_array[i]<71))  substr_value=55;  // A..F characters (decimal 65-70) , 'A' becomes 0xA
    else if ((my_array[i]>96) & (my_array[i]<103))  substr_value=87;  // a..f characters (decimal 97-102), 'a' becomes 0xA
    else substr_value=48;  // 0..9 characters


    my_int = my_int | ((my_array[i]-substr_value)<<(4*(3-i)));

}

Alex
 
Last edited:
  • Like
Reactions: ponnus

    ponnus

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top