Sponsored Content
Top Forums UNIX for Dummies Questions & Answers ANSI C, char to hex conversion Post 302242433 by alikims on Thursday 2nd of October 2008 02:07:18 AM
Old 10-02-2008
ANSI C, char to hex conversion

Hi,

I have a
Code:
char buf[1024],ch;

and the buf is filled with the result from MySQL server which I get like this
Code:
numbytes = recv(sock, buf, 1024, 0));

I have the followingcode to display the results

Code:
printf("received %ld bytes:\n",numbytes);
    for(c=0;c<numbytes;c++){
        ch = (char)buf[c];
        printf("%c_",ch);
    }
    printf("\n\n");
    for(c=0;c<numbytes;c++){
        printf("%02X_",(int)*(buf+c));
    }
    printf("\n\n");
    for(c=0;c<numbytes;c++){
        printf("%c_",(int)*(buf+c));
    }

and the output is like this:

Code:
received 65 bytes:
=____
_5_._0_._2_7_-_s_t_a_n_d_a_r_d__╚____a_k_u_,_w_6_C_0__,_╒________________f_2_f_+_A_F_y_P_a_z_`_U__

3D_00_00_00_0A_35_2E_30_2E_32_37_2D_73_74_61_6E_64_61_72_64_00_FFFFFFAB_00_00_00_
61_6B_75_2C_77_36_43_30_00_2C_FFFFFFA2_08_02_00_00_00_00_00_00_00_00_00_00_00_00_
00_00_66_32_66_2B_41_46_79_50_61_7A_60_55_00_

61_0_0_0_10_53_46_48_46_50_55_45_115_116_97_110_100_97_114_100_0_-85_0_0_0_97_107_117_
44_119_54_67_48_0_44_-94_8_2_0_0_0_0_0_0_0_0_0_0_0_0_0_0_102_50_102_43_65_70_121_80_97_122_96_85_0_

so for some reason two bytes are converted into a negative integer or a huge hex number.

The right values must be just AB and A2... I'm totally lost here..
Can anyone please explain me why it happens so?

Thank you!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hex Conversion

I need to have my scripts import volume groups and mknod devices files. I have most of the script working but the device file needs to be in the format 0x??0000 (where the question marks are my HEX representations of the volume group number. I have the code below and the output it produces which... (2 Replies)
Discussion started by: insania
2 Replies

2. UNIX for Dummies Questions & Answers

File conversion from Unix to ANSI

1. I have a shell script which creates a file using cat command. How can i find what encoding the file follows (e.g. UTF8, ANSI)? 2. I want to convert that file to PC-ANSI format. How can i achieve that? I tried using the echo $LANG command to find the default encoding. It says parameter not... (2 Replies)
Discussion started by: ssmallya
2 Replies

3. UNIX for Dummies Questions & Answers

Conversion from EBCDIC to HEX

Hello, Is there any utility around able to deal with the conversion of some EBCDIC coded string into an hexadecimal value? Thanks (1 Reply)
Discussion started by: Indalecio
1 Replies

4. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

5. Shell Programming and Scripting

Linux to ansi pc conversion

Hi All, I checked the old posts here. But could not find a solution for my question. I have a file created by one application in HP-UX. My client wants it to be converted into ANSI PC version. I have heard about unixtodos and have worked with it also. But I am totally unaware of of this ANSI... (0 Replies)
Discussion started by: Tuxidow
0 Replies

6. Programming

how to use hex escape char with string in C?

I want it to ouput "abcd", but it dosen't. 1 #include<stdio.h> 2 int main() 3 { 4 printf("a\x62cd"); 5 } 6 gcc alarm.c -o alarm alarm.c: In function 'main': alarm.c:4:9: warning: hex escape sequence out of range It seems that the complier joint "cd" as part of... (8 Replies)
Discussion started by: vistastar
8 Replies

7. Shell Programming and Scripting

HEX to DEC Conversion

I'm trying to convert hex to dec and with the help of output i need to do the process. If i execute the below code assetValue=8f assetNavigation=$(echo "ibase=16; "$assetValue"" | bc) echo $assetNavigation i'm getting the error below $ sh script.sh (standard_in) 1: syntax error... (2 Replies)
Discussion started by: Amutha
2 Replies

8. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

9. Shell Programming and Scripting

How add 0 in hex conversion?

Hi, Got the below code in this forum, for i in `cat test`; do printf "%16s\n" $(echo "ibase=10;obase=16;$i" | bc) done Understand, this will not give output with '0'. Need help to get the output with '0'. Regards, Siva (2 Replies)
Discussion started by: ksgnathan
2 Replies

10. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies
uconvert(3alleg4)						  Allegro manual						 uconvert(3alleg4)

NAME
uconvert - High level string encoding conversion wrapper. Allegro game programming library. SYNOPSIS
#include <allegro.h> char *uconvert(const char *s, int type, char *buf, int newtype, int size); DESCRIPTION
Higher level function running on top of do_uconvert(). This function converts the specified string `s' from `type' to `newtype', storing at most `size' bytes into the output `buf' (including the terminating null character), but it checks before doing the conversion, and doesn't bother if the string formats are already the same (either both types are equal, or one is ASCII, the other is UTF-8, and the string con- tains only 7-bit ASCII characters). As a convenience, if `buf' is NULL it will convert the string into an internal static buffer and the `size' parameter will be ignored. You should be wary of using this feature, though, because that buffer will be overwritten the next time this routine is called, so don't expect the data to persist across any other library calls. The static buffer may hold less than 1024 characters, so you won't be able to convert large chunks of text. Example: char *p = uconvert(input_string, U_CURRENT, buffer, U_ASCII, 256); RETURN VALUE
Returns a pointer to `buf' (or the static buffer if you used NULL) if a conversion was performed. Otherwise returns a copy of `s'. In any cases, you should use the return value rather than assuming that the string will always be moved to `buf'. SEE ALSO
set_uformat(3alleg4), need_uconvert(3alleg4), uconvert(3alleg4), uconvert_ascii(3alleg4), uconvert_toascii(3alleg4), do_uconvert(3alleg4) Allegro version 4.4.2 uconvert(3alleg4)
All times are GMT -4. The time now is 02:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy