![]() |
Convert the Ascii to Hex in C
I am trying to convert the input ascii string to hex format using the format specifier "%.2X" . I have pasted the program at the end which does the same. But, this program works fine in AIX server and in Linux server its giving improper hex output.
Below I have mentioned the output of the program in both the environments. The AIX env gives the equalent hex value for the given input and this works well. The Linux env returns the equalent hex value for the normal ascii characters but not for the extended ascii (i.e. Latin characters - character code 128-255). Output in AIX --------------- INput chunk = [<8f><80>âY^SZÃpS ðï^Y] Read buf size = [30] ls_input_chunk_raw = [3C38663E3C38303EC383C2A2595E535AC38370532020C3B0C3 83C2AF5E59] Output in Linux ----------------- INput chunk = [<8f><80>âY^SZÃpS ðï^Y] Read buf size = [30] ls_input_chunk_raw = [3C38663E3C38303EFFFFFFFF595E535AFFFF70532020FFFFFF FFFFFF5E59] I suspect the Linux processor (which I am using) doesn't support the extended ascii chars (I am not sure about this). Server Information: ---------------------- AIX server: ------------ $uname -a AIX serv01 3 5 00CBCEFC4C00 Linux server: -------------- $uname -a Linux serv02 2.6.9-42.0.0.0.1.ELsmp #1 SMP Sun Oct 15 15:13:57 PDT 2006 x86_64 x86_64 x86_64 GNU/Linux I am expecting the same output in Linux env as the output in AIX env. Please let me know your suggestion to resolve this issue to make it work in both the environments. Code:
/***********C Program ***************************/ |
Re: Convert the Ascii to Hex in C
Please read the "Before you make a query" thread and learn how to use code tags. It's definitely easier than programming.
ASCII only defines 128 characters, 0-127. The remaining will depend on the system, its locale settings, or its "code page." Any "defined" behavior you achieve will be "defined" only for complying implementations. |
Re: Convert the Ascii to Hex in C
Thanks for your advice.
So, you mean to say the Sun & Linux servers will not support the 256 ASCII character set and it support only 0-127 standard ASCII character set. Whereas, the IBM AIX server supports the standard and extended ASCII character set i.e. 0-255. Please let me know if I my understanding is wrong. Also, could you please suggest the method/command to identify the character set supported by the server? |
Re: Convert the Ascii to Hex in C
They will support values from 0-255. There is no standard definition for the symbolic representations in the upper half of the set. Some might want some line-drawing characters, some might want more accented characters, some might want to provide switchable choices. None of your systems is "wrong", they have just made different choices.
|
Re: Convert the Ascii to Hex in C
Could you please suggest me the way, by which I can get the equal hex value for the given extended ascii character without using sprintf(...,"%.2X", ....).
I don't think any built-in function is available in C for hex conversion. If so, could you please suggest? |
Re: Convert the Ascii to Hex in C
I think you're still missing the point that a binary value and a representation of that value are two different things. For instance, when you print the value 1, you are not printing the value, 1, but the character, '1'. This has (in ASCII) a value of 49 or 0x31. The ASCII symbol for the representation of the value, 1, is clearly defined. The ASCII symbol for the representation of the value, 200, is not clearly defined. It might be ╚ and it might be something else.
It is relatively trivial to come up with the hexadecimal symbolic representation for actual values. Here is one method, for the values 0 through 255. Note that this method extracts the least significant digit first. The resulting string representation is then reversed to get the MSD-first order. I think you can see how this could be adapted for binary, octal, or any other base. All you need are the symbols for that base and a suitable divisor. Personally, I'd use sprintf. In other words, the wheel has already been invented. Code:
int main()Quote:
|
Re: Convert the Ascii to Hex in C
use sscanf(); thats it
ex: int i; char str[]="abc"; scanf(str,"%x",&i); print("%x",i); |
| All times are GMT +5.5. The time now is 11:22. |