Sponsored Content
Top Forums Programming After converting the hexstr to Hex and storing the Hex in a char* Post 302342343 by rvan on Sunday 9th of August 2009 02:24:10 AM
Old 08-09-2009
CPU & Memory 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 another module.

I just want to print and see the values in the result char* to see if it is stored as 0x30, (say when the input char* a = "30" )

How can I verify the values stored in the char*...?

I cannot print it neither as %s nor %x both gives junk values.. %s gives junk because it is hex and %x gives junk because i have formatted with "0x30," i think.. But am not clear and don't know how to check if the values got stored in this format. 0x30,


- Thanks






Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int hexstr2bin(const char *hex_input, char *buf_output, size_t len);
static int hex2byte(const char *hex);
static int hex2num(char c);

main()
{
	int len;
	char* buf;

	const char* hex ="abcd1234567890";

	buf = (char*)malloc(100);

	len = strlen(hex);

	hexstr2bin(hex,buf,len);

	printf("Values in hex %s",buf);
}


int hexstr2bin(const char *hex_input, char *buf_output, size_t len)
{
	size_t i;
	int a;
	const char *ipos = hex;
	char *opos = buf;
	char *prefix = "0x";

	for (i = 0; i < len; i++) {
		a = hex2byte(ipos);
		if (a < 0)
			return -1;
		*opos++ = prefix[0];
		*opos++ = prefix[1];

			
		*opos++ = a;

		
		*opos++ = ',';
		ipos += 2;
	}
	return 0;
}


static int hex2byte(const char *hex)
{
	int a, b;
	a = hex2num(*hex++);
	if (a < 0)
		return -1;
	b = hex2num(*hex++);
	if (b < 0)
		return -1;
	return (a << 4) | b;
}


static int hex2num(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	return -1;
}

 

10 More Discussions You Might Find Interesting

1. Programming

converting hex to ascii

Hi everyone! I was wondering if anyone knows how to change hex code back into ascii. when i process a form: " / " turn to " %2F " " @ " turns to " %40 " " ' " turns to " %27 " " ( " turns to " %28 " " ) " turns to " %29 " this is my code so far: order.txt thanks, primal p.s.... (1 Reply)
Discussion started by: primal
1 Replies

2. Shell Programming and Scripting

converting hex to dec

Hi Experts, I have a file called "hex" which contains info like below How do i convert everything in this file to decimal value? Please advice. Thanks (4 Replies)
Discussion started by: aismann
4 Replies

3. UNIX for Dummies Questions & Answers

ANSI C, char to hex conversion

Hi, I have a char buf,ch; and the buf is filled with the result from MySQL server which I get like this numbytes = recv(sock, buf, 1024, 0));I have the followingcode to display the results printf("received %ld bytes:\n",numbytes); for(c=0;c<numbytes;c++){ ch = (char)buf; ... (2 Replies)
Discussion started by: alikims
2 Replies

4. UNIX for Advanced & Expert Users

converting openssl hex dump or PEM format to integer array

Hello. I'm working on a project that involves creating public/private keys server-side using openssl and using the public key in a Javascript application to encrypt sensitive data in form fields before transmission to the server. Using an SSL https server connection was not an option in this... (1 Reply)
Discussion started by: jhopper
1 Replies

5. Shell Programming and Scripting

Converting hex value 7C (for pipe) to CRLF in Unix

I am trying to convert a txt file that includes one long string of data. The lines are separated with hex value 7C (for pipe). I am trying to process this file using SQR (Peoplesoft) so I thought the easiest thing to do would be to replace the eol char with a CRLF in unix so I can just... (4 Replies)
Discussion started by: sfedak
4 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. Programming

What is the difference between ios::hex and std::hex?

Hi, Is there really a difference between these two, std::hex and ios::hex?? I stumbled upon reading a line, "std::ios::hex is a bitmask (8 on gcc) and works with setf(). std::hex is the operator". Is this true? Thanks (0 Replies)
Discussion started by: royalibrahim
0 Replies

8. Shell Programming and Scripting

Converting hex to ascii/decimal

I am writing a bash script to do some parsing on a log and I am running into a problem when it comes to converting only certain sections of the file from hex to ascii or hex to decimal. Data Example: The hex values after Hardware and SW Version I need to convert from Hex to ASCII and the... (16 Replies)
Discussion started by: Shiftkey
16 Replies

9. Shell Programming and Scripting

Converting parts of a string to "Hex"

Hi Guys, writing a small shell script, i need to convert parts of a string to "Hex". The problem is that it is not the full string that needs to be converted. I think it's best to show an example: $astring = "xxxxxx ABC+10+##########+DEF xxxx" This is only an example to show how the... (9 Replies)
Discussion started by: HansHansen
9 Replies

10. Shell Programming and Scripting

Converting decimal to hex

How to convert decimal value to hex and than take 1st digits as variable sample data 84844294,5,6 51291736,2,3 84844294,5,6 51291736,2,3 i can use {printf "%x,%d\n",$1,$2} but than i want to filter base on 1st hex digit 1st recrd (1 Reply)
Discussion started by: before4
1 Replies
ExperimentFile(3)						  Staden Package						 ExperimentFile(3)

NAME
exp_read_info, exp_fread_info, exp_create_info, exp_destroy_info, exp_create_range, exp_extract_range, exp_get_feature_index, exp_get_int, exp_get_rng, exp_get_str, exp_put_int, exp_put_rng, exp_put_str, exp_print_line, exp_print_seq, exp_print_file, opos2str, str2opos, conf2str, str2conf - Manipulations of the Experiment File format SYNOPSIS
#include <expFileIO.h> Exp_info *exp_read_info( char *file); Exp_info *exp_fread_info( FILE *fp); Exp_info *exp_create_info(); void exp_destroy_info( Exp_info *e); char *exp_create_range( char *str, int start, int end); int exp_extract_range( char *str, int *start, int *end); int exp_get_feature_index( char *e); int exp_get_int( Exp_info *e, int id, int *val); int exp_get_rng( Exp_info *e, int id, int *from, int *to); int exp_get_str( Exp_info *e, int id, char *s, f_implicit s_l); int exp_put_int( Exp_info *e, int id, int *val); int exp_put_rng( Exp_info *e, int id, int *from, int *to); int exp_put_str( Exp_info *e, int id, char *s, f_implicit s_l); int exp_print_line( FILE *fp, Exp_info *e, int eflt, int i); int exp_print_seq( FILE *fp, Exp_info *e, int eflt, int i); void exp_print_file( FILE *fp, Exp_info *e); char *opos2str( int2 *opos, int len, char *buf); int str2opos( int2 *opos, char *buf); char *conf2str( int1 *conf, int len, char *buf); int str2conf( int1 *conf, char *buf); DESCRIPTION
These functions manipulate Experiment Files. They include functions for reading, writing, and editing the files. The principle structure used by the routines is the Exp_info structure. This is as follows. #define MAXIMUM_EFLT_LENGTH 4 #define MAXIMUM_EFLTS 48 #define EXP_FILE_LINE_LENGTH 128 typedef Array Exp_entries; typedef struct { Array entries[MAXIMUM_EFLTS]; /* array of array of entries */ int Nentries[MAXIMUM_EFLTS]; /* array of number of entries */ FILE *fp; } Exp_info; #define NULL_Exp_info ( (Exp_info *) NULL ) For the purposes of simple and efficient coding, each line on an experiment file must be smaller than the defined EXP_FILE_LINE_LENGTH, which is 128 characters. Many functions take an experiment file line type identifier as an argument. These functions are listed in the prototypes as taking int id. Here id should be specified using one of the macros defining in the header file. They take the form of EFLT_XX where XX is the line type. For instance, the ID line type identifier should be written as EFLT_ID. The C functions available follow. Some FORTRAN interfaces are also available, but these are not documented. See the include file for their prototypes. exp_read_info and exp_fread_info read an experiment file into an allocated Exp_info structure. If successful, the structure pointer is return. Otherwise the null pointer is returned. exp_create_info allocates and initialises a new, blank, Exp_info structure. If successful, the structure pointer is return. Otherwise the null pointer is returned. exp_destroy_info deallocates an Exp_info structure. exp_create_range creates a string using the experiment file range format of "start..stop". This format is used by the AQ, ON, TG and TC line types. The buffer specified should be large enough to hold the string. The function returns the str pointer supplied as an argument. exp_extract_range extracts the start and end values from the string representation of a range; "start..stop". The values are stored in the integer pointers sent as arguments. If successful, the function returns 0. Otherwise -1 is returned. exp_get_feature_index converts from a string representation of an experiment file line type to the integer value. For example, exp_get_fea- ture_index("SQ") returns the integer 22, which is the same as the EFLT_SQ definition. exp_get_int obtains the integer value held in a specific line type. The value is stored in the integer pointer val. If successful, the function returns 0. Otherwise 1 is returned. exp_get_rng obtains the range values held in a specific line type. The values are stored in the integer pointers from and to. If success- ful, the function returns 0. Otherwise 1 is returned. exp_get_str obtains the string value held in a specific line type. The value is stored in the string s. At most s_l characters are copied. If successful, the function returns 0. Otherwise 1 is returned. exp_put_int writes the integer pointed to by val to the specified line type. If successful, the function returns 0. Otherwise 1 is returned. exp_put_rng writes the range pointed to by from and to to the specified line type. If successful, the function returns 0. Otherwise 1 is returned. exp_put_str writes the string s of length s_l to the specified line type. If successful, the function returns 0. Otherwise 1 is returned. exp_print_line outputs all entries of the specified type to the specified file pointer. exp_print_seq exp_print_file outputs all entries of all line types to the specified file pointer. opos2str converts an array of original positions held in opos with len elements to a string suitable for use in the ON line type. The buf should be large enough to hold the string, which in the worst case will be 4 * sequence length. Returns buf. str2opos converts the experiment file original position string held in buf to an array of original positions to be stored in opos. opos must be large enough to hold the data, hence it should be of the same length as the sequence. Returns the number of elements written to the opos array. conf2str converts an array of confidence values helf in confR with len elements to a string suitable for use in the AV line type. The buf should be large enough to hold the string, which in the worst case will be 4 * sequence length. Returns buf. str2conf convers the experiment file confidence values string held in buf to an array of confidence values to be stored in conf. conf must be large enough to hold the data, hence it should be of the same length as the sequence. Returns the number of elements written to the conf array. SEE ALSO
ExperimentFile(4) ExperimentFile(3)
All times are GMT -4. The time now is 02:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy